{
unsigned long long two16, two48 ;
two16 = 65536;
two48 = two16 * two16 * two16 ;
printf("2^48=%llX \n",two48 );
two48 = 1<<48 ;
printf("Shifted 1<<48=%llX \n",two48);
return 0 ;
}
Run Code Online (Sandbox Code Playgroud)
当在 64 位机器上编译时,字大小为 8,上面给出警告 2=1<<48 将溢出左移计数 >= 类型宽度。
程序的输出是:
2^48=1000000000000
Shifted 1<<48=0
Run Code Online (Sandbox Code Playgroud)
到底是怎么回事?为什么我不能将 64 位数量移至 48 位?
任何人都可以解释为什么会发生这种情况吗?:
int a = 2147483647;
cout <<"Product = " << a * a << endl; // output = 1 (why?)
int b = -2147483648;
cout <<"Product = " << b * b << endl; // output = 0 (why?)
Run Code Online (Sandbox Code Playgroud)
此外,当我们为 'short' 编写类似的内容时,编译器将乘积视为整数类型,尽管变量被初始化为短类型,例如:
short x = 32767;
cout <<"Product = " << x * x << endl; // Product = 1073676289
short y = -32768;
cout <<"Product = " << y * y << endl;// Product = 1073741824
Run Code Online (Sandbox Code Playgroud) c++ integer integer-overflow undefined-behavior modular-arithmetic
如何在C++中计算2^100的十位值?
我尝试过这个;
#include <cmath>
#include <iostream>
using namespace std;
int main(){
int answer;
answer = (unsigned long long int)pow(2, 100) % 100 / 10; //zero
cout << answer << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但由于溢出而打印出0。
Python 使用此代码正确打印答案;
print(2 ** 100 % 100 // 10)
Run Code Online (Sandbox Code Playgroud)
但是我如何在C++中计算它呢?
我从Integer Overflow Wiki读取以下行:
无符号整数溢出导致数量减去模2的幂,这意味着无符号整数在溢出时"环绕".
我有下面的代码,我试图创建一个哈希函数,并得到int溢出的情况.我试图通过使用缓解它,unsigned int但它没有工作,我能够看到负值.
我知道我可以通过其他方式处理它并且它可以工作,如我的代码注释所示 - Comment 2:.但这是正确的方式,为什么unsigned int没有环绕和溢出?
int hash(char *word) {
char *temp = word;
unsigned int hash = 0; // Comment 1: I tried to handle int overflow using "unsigned" int.
while (*word != '\0') {
// Comment 2: This works but I do not want to go this way.
//while ((hash * PRIME_MULTIPLIER) < 0) {
// hash = (hash * PRIME_MULTIPLIER) + 2147483647;
//}
hash = …Run Code Online (Sandbox Code Playgroud) 我真的很困惑.我有一个C服务器,它工作得很好,但后来我添加了一些代码,我认为它工作正常,直到我的程序随机开始将int的值更改为负值.
基本上我有我的服务器输出发送的总字节数和传输中途,总是大约2100000000字节,总字节变为负数.这是我输出文件的一个例子.如果你查看我的代码,这个值不能变成负数.所以我怀疑这是更奇怪的事情.
"345000","1470253912","59203","5592","2069901108"
"348000","1470253912","475539","4194","2092449162"
"351000","1470253912","830291","2796","2112043464"
"354000","1470253913","243217","1398","2133985176"
"357000","1470253913","708686","13980","-2135434834"
"360000","1470253914","173646","9786","-2109094024"
"363000","1470253914","514938","6990","-2089413400"
Run Code Online (Sandbox Code Playgroud)
无论如何我添加的东西,我评论了它,我仍然遇到了同样的错误.仅供参考,我的代码在"NEW STUFF ADDED"标签下注释掉了.(我之所以添加它是因为这篇文章:在命令行上终止C程序,但确保编写完成后写入 )
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/time.h>
int PORT_NUM = 0;
int RecordRate = 3000;
FILE *fp;
typedef struct timeval timeval;
timeval time_;
void error(const char *msg)
{
perror(msg);
exit(1);
}
//NEW STUFF ADDED
//void sig_handler(int signo)
//{
// if (signo == SIGINT) {
// printf("received SIGINT\n"); …Run Code Online (Sandbox Code Playgroud) 参考C11 草案,第 3.4.3 节和C11 草案,第 H.2.2 节,我正在寻找实现除有符号整数的模运算以外的行为的“C”实现。
具体来说,我正在寻找这是默认行为的实例,可能是由于底层机器架构。
这是一个代码示例和终端会话,说明了有符号整数的模算术行为:
overflow.c:
#include <stdio.h>
#include <limits.h>
int main(int argc, char *argv[])
{
int a, b;
printf ( "INT_MAX = %d\n", INT_MAX );
if ( argc == 2 && sscanf(argv[1], "%d,%d", &a, &b) == 2 ) {
int c = a + b;
printf ( "%d + %d = %d\n", a, b, c );
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
终端会话:
$ ./overflow 2000000000,2000000000
INT_MAX = 2147483647
2000000000 + 2000000000 …Run Code Online (Sandbox Code Playgroud) //filename:mt.c\n//filename is useful to understand the gcc command\n#include <stdio.h>\nint isTmax(int x);\n\nint main()\n{\n printf("wtf %d\\n", isTmax(0x7fffffff));\n return 1;\n}\n\nint isTmax(int x)\n{\n int y = ((x + x + 2) ^ 1);\n int z = (!(~(x + x + 2) + 1) ^ 0);\n printf("y = %d\\n", y);\n printf("z = %d\\n", z);\n return y & z;\n} \nRun Code Online (Sandbox Code Playgroud)\n代码很奇怪,因为它是一个 csapp 讲义解决方案(显然是错误的)。(x+x+2)当 x 等于 0x7fffffff 时,\n 等于 0。(~(x+x+2)+1)由于溢出所以等于0。所以(!(~(x+x+2)+1)^0)等于1。调试时观察验证了这一点。\n我认为,正常情况下,赋值后z应该为1。
环境\xef\xbc\x9a{系统\xef\xbc\x9awindows 10; 虚拟系统ubuntu 20.04 LTS;虚拟机软件:VirtualBox 6.1;GCC:gcc(Ubuntu 9.3.0-17ubuntu1~20.04)9.3.0}
\n
我们如何解决具有N的方程!其中的常量,其中N可以是范围1 <= N <= 10 ^ 6 BigInteger最多只能执行128位?
即使在双方都做对数时,它也会留下比BigInteger更大的值.
请考虑以下代码:
#include <iostream>
int main(int argc, char* argv[])
{
int i = /* something */;
for (std::size_t n = 0; n < 100; ++n) {
i *= 2;
std::cout << i << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
对有符号整数进行溢出是未定义的行为.但是,我不明白为什么这段代码似乎总是以0结尾.任何解释?
在我的cuda设备代码中,我正在检查,其中我减去线程的id和blockDim以查看天气与否,我可能想要使用的数据在范围内.但是当这个数字低于0时,它似乎又回到了最大值.
#include <iostream>
#include <cuda_runtime.h>
#include <device_launch_parameters.h>
float input[] =
{
1.5f, 2.5f, 3.5f,
4.5f, 5.5f, 6.5f,
7.5f, 8.5f, 9.5f,
};
__global__ void underflowCausingFunction(float* in, float* out)
{
int id = (blockDim.x * blockIdx.x) + threadIdx.x;
out[id] = id - blockDim.x;
}
int main()
{
float* in;
float* out;
cudaMalloc(&in, sizeof(float) * 9);
cudaMemcpy(in, input, sizeof(float) * 9, cudaMemcpyHostToDevice);
cudaMalloc(&out, sizeof(float) * 9);
underflowCausingFunction<<<3, 3>>>(in, out);
float recivedOut[9];
cudaMemcpy(recivedOut, out, sizeof(float) * 9, cudaMemcpyDeviceToHost);
cudaDeviceSynchronize();
std::cout << recivedOut[0] << " …Run Code Online (Sandbox Code Playgroud)