It seems that I found a bug when print value of (0xffffffff + 1)
with %llu
is 0
, see below code:
unsigned long long resTestBad = 0xffffffff + 1; // number of f is 8
printf("resTestBad is %llu, sizeof(unsigned long long) is %ld\n", resTestBad, sizeof(unsigned long long));
will output:
resTestBad is 0, sizeof(unsigned long long) is 8
I think that output is wrong, it should be output or say correct output:
resTestBad is 4294967296, sizeof(unsigned long long) is 8
And
unsigned long long resA = 4294967295 + 1;
printf("resA is %llu, sizeof(unsigned long long) is %zu\n", resA, sizeof(unsigned long long));
will output:
resA is 4294967296, sizeof(unsigned long long) is 8
But below code is no problem:
unsigned long long resTestAgain = 0xfffffffff + 1; // number of f is 9
printf("resTestAgain is %llu, sizeof(unsigned long long) is %zu\n", resTestAgain, sizeof(unsigned long long));
It will output(I think it right):
resTestAgain is 68719476736, sizeof(unsigned long long) is 8
My .c file is below:
#include <stdio.h>
#include <limits.h>
int main(void)
{
int s = 0xfffe;
printf("s is %d\n", s);
unsigned int ww = 0xfffe;
printf("ww is %d\n", ww);
unsigned long long resTestOk = 0xfffffffe + 1;
printf("resTestOk is %llu, sizeof(unsigned long long) is %zu\n", resTestOk, sizeof(unsigned long long));
unsigned long long resTestBad = 0xffffffff + 1;
printf("resTestBad is %llu, sizeof(unsigned long long) is %zu\n", resTestBad, sizeof(unsigned long long));
unsigned long long resTestAgain = 0xfffffffff + 1;
printf("resTestAgain is %llu, sizeof(unsigned long long) is %zu\n", resTestAgain, sizeof(unsigned long long));
unsigned long long resA = 4294967295 + 1;
printf("resA is %llu, sizeof(unsigned long long) is %zu\n", resA, sizeof(unsigned long long));
unsigned long long resTestUUU = 0xffffffffffffff + 1;
printf("resTestUUU is %llu, sizeof(unsigned long long) is %zu\n", resTestUUU, sizeof(unsigned long long));
unsigned long long resH = (0xffffffff+1) / 1024 / 1024 / 1024;
printf("%llu\n", resH);
unsigned long long resD = (4294967296) / 1024 / 1024 / 1024;
printf("%llu\n", resD);
return 0;
}
Run it will output:
s is 65534
ww is 65534
resTestOk is 4294967295, sizeof(unsigned long long) is 8
resTestBad is 0, sizeof(unsigned long long) is 8
resTestAgain is 68719476736, sizeof(unsigned long long) is 8
resA is 4294967296, sizeof(unsigned long long) is 8
resTestUUU is 72057594037927936, sizeof(unsigned long long) is 8
0
4
May be you don't believe that, I also don't believe, so I record a video that for prove I said.
See https://imgur.com/a/RAQVPxS
Is it a Bug of CPU or bug of VMWare Workstation? How can I solve it?
Update
Sorry, may be my question is not clear, or say confusion for people.
Problem origin from that I want to test how many memory a 32-bit computer can access(I know it is 4GB, address from 0x00000000 to 0xffffffff, total number is 0xffffffff + 1, unit is bytes, so (0xffffffff + 1)
/ 1024 /1024 / 1024 = 4GB), so I use below code:
unsigned long long resH = (0xffffffff + 1) / 1024 / 1024 / 1024;
printf("%llu\n", resH);
But it output number 0
, that is not what I want to get. I except output is 4. So I also use below code:
unsigned long long resQ = (4294967295 + 1) / 1024 / 1024 / 1024;
printf("%llu\n", resQ);
It output 4 that what is I except. So I want to find out what reason result to different of result.
Then I use below code:
unsigned long long resTestBad = 0xffffffff + 1;
printf("resTestBad is %llu, sizeof(unsigned long long) is %zu\n", resTestBad, sizeof(unsigned long long));
it output:
resTestBad is 0, sizeof(unsigned long long) is 8
That not is my except, so I test below code:
unsigned long long resTestAgain = 0xfffffffff + 1;
printf("resTestAgain is %llu, sizeof(unsigned long long) is %zu\n", resTestAgain, sizeof(unsigned long long));
it output:
resTestAgain is 68719476736, sizeof(unsigned long long) is 8
as below code:
unsigned long long resA = 4294967295 + 1;
printf("resA is %llu, sizeof(unsigned long long) is %zu\n", resA, sizeof(unsigned long long));
it output:
resA is 4294967296, sizeof(unsigned long long) is 8
By see answer, now I understand why 0xffffffff + 1 output 0.
But now I can't understand that value size of 0xffffffff and 4294967295 is the same, why 0xffffffff as unsigned int
type(occupy 32-bit), however 4294967295 may be as long
or long long
or other type(occupy 64-bit).