Yes, i understand now.
I know a number wraps around at the boundary:
u8 255 + 1 = 0
u8 0 - 1 = 255
I didn't realize the carry rippler expects this, and that C implicitly allows it when using unsigned variables. Thanks for the help
Moderators: hgm, Rebel, chrisw
Yes, i understand now.
This is actually explicitly allowed by the C language specification. Section 6.2.5.9 of the C99 standard says:
If you use a correctly implemented C compiler, it will make unsigned arithmetic behave as specified, regardless of what hardware the compiled program will run on. If the hardware cannot perform the required modulo arithmetic in one machine instruction, the compiler would have to generate extra instructions to produce the correct result.The range of nonnegative values of a signed integer type is a subrange of the
corresponding unsigned integer type, and the representation of the same value in each
type is the same.31) A computation involving unsigned operands can never overflow,
because a result that cannot be represented by the resulting unsigned integer type is
reduced modulo the number that is one greater than the largest value that can be
represented by the resulting type.
Code: Select all
x + ~x == ~0 = 111...111B
x + ~x + 1 == x + -x = x - x = 0
Code: Select all
int j = 23;
j <<= 32;
Print("%i", j);
BITBOARD i = 0xFFFFFFFFFFFFFFFFULL;
i <<= 65;
Print("%llx", i);
j = INT_MAX;
Print("%i , %i", j, j + 1);
i = 0xFFFFFFFFFFFFFFFFULL;
Print("%llx , %llx", i, i + 1);
j = INT_MIN;
Print("%i , %i", j, j - 1);
i = 0x0ULL;
Print("%llx , %llx", i, i - 1);
Code: Select all
Sapeli.c:4015:5: warning: left shift count >= width of type [-Wshift-count-overflow]
j <<= 32;
^~~
Sapeli.c:4019:5: warning: left shift count >= width of type [-Wshift-count-overflow]
i <<= 65;
^~~
At top level:
Sapeli.c:4000:13: warning: ‘Go’ defined but not used [-Wunused-function]
static void Go()
^~
0
0
2147483647 , -2147483648
ffffffffffffffff , 0
-2147483648 , 2147483647
0 , ffffffffffffffff
This is because the hardware (x86 since 286, not sure about ARM) does mask before shift.