You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
x ^ 0s = x x & 0s = 0 x | 0s = x
x ^ 1s = ~x x & 1s = x x | 1s = 1s
x ^ x = 0 x & x = x x | x = x
Two's Complement
Computers typically store integers in two's complement representation.
Range of unsigned numbers that can be stored with N bits is 0 - +(2^N - 1).
Range of signed numbers that can be stored with N bits in two's complement representation is -(2^(N - 1)) - +(2^(N - 1) - 1).
Binary representation of -K is concat(1, bin(2^(N - 1) - K)). Another way to compute it is to flip the bits of the binary representation of K, add 1 and then prepend the sign bit 1.
Arithmetic vs. Logical Shift
In an arithmetic right shift (>>), the bits are shifted and the sign bit is put in the MSB.
In a logical right shift (>>>), the bits are shifted and a 0 is put in the MSB.