I found that LONG_MAX (9223372036854775807) is actually saved as below on the long type variable in the 64-bit machine.
Hex: 0xffff ffff ffff ffff ffff ffff ffff ffff
Bin: 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111
As I know, the most significant bit is used as a signed bit.
And the 9223372036854775807 is changed as below in the Windows calculator.
Hex: 0x7fff ffff ffff ffff ffff ffff ffff ffff
Bin: 01111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111
So, IMHO, the LONG_MAX should be saved as below
Hex: 0x7fff ffff ffff ffff ffff ffff ffff ffff
Bin: 01111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111
Is a long variable considered as an unsigned value?
I noticed somebody ask how I found that situation.
I found the situation in the below code.
#include <stdio.h>
#include <limits.h>
int main(int argc, char *argv[])
{
long a;
a = strtol(argv[1], NULL, 0);
printf("%ld\n", a);
return 0;
}
Actually, I didn't use the LONG_MAX macro, but when I execute the program, I used 9223372036854775807 value statically.
I checked the value is safe as a 0x7fff ... with gdb like below
Reading symbols from test...done.
(gdb) b main
Breakpoint 1 at 0x40055c: file test.c, line 8.
(gdb) r 9223372036854775807
Starting program: ./test 9223372036854775807
Breakpoint 1, main (argc=2, argv=0x7fffffffe308) at test.c:8
8 a = strtol(argv[1], NULL, 0);
(gdb) n
10 printf("%ld\n", a);
(gdb) x/gt &a
0x7fffffffe218: 1111111111111111111111111111111111111111111111111111111111111111
(gdb)
Even though I didn't use the LONG_MAX macro, the value 9223372036854775807 is the same as the LONG_MAX macro.
So, I think it should be saved as a 0x7fff ... like using LONG_MAX.