Because two's complement bit-arithmetic makes it so
Cribbed from the wikipedia page and expanded:
Most
Significant
Bit 6 5 4 3 2 1 0 Value
0 0 0 0 0 0 1 1 3
0 0 0 0 0 0 1 0 2
0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 -1
1 1 1 1 1 1 1 0 -2
1 1 1 1 1 1 0 1 -3
1 1 1 1 1 1 0 0 -4
So you get:
0 0 0 0 0 0 1 0 = 2
1 1 1 1 1 1 0 1 = -3
And as you can see, all the bits are flipped, which is what the bitwise NOT operator (~
) does.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…