i don't understand output of code:
main() { int ret = ~(~0 <<5) << 2; printf("ret: %d, %u\n", ret, ret); } output: ret: 124, 124 if process mentally, this:
- resolving ~0 gives binary 1
- resolving ~0 << 5 gives binary 100000
- resolving ~(~0 << 5) gives binary 011111
- resolving ~(~0 << 5) << 2 gives binary 111100
- convert binary 111100 decimal gives 60
what did wrong ?
integral literals in c ints default, , int 4 bytes long (depending on compiler). means ~0 not 1, it's 32 1s.
~0 == 11111111111111111111111111111111 ~0 << 5 == 11111111111111111111111111100000 ~(~0 << 5) == 00000000000000000000000000011111 ~(~0 << 5) << 2 == 00000000000000000000000001111100
Comments
Post a Comment