Bit Manipulation Flashcards
what is a bit?
it’s either 1 or 0
what are the bitwise operators?
AND & OR | XOR ^ NOT ~ LEFT SHIFT << RIGHT SHIFT >>
why do we use bit manipulation?
if we want to cram alot of info into one place to improve an algorithm efficiency
AND &
only true if both bits are true
1 & 1 = 1
0 & 1 = 0
OR |
true if any bit is true
1 & 1 = 1
0 & 1 = 1
XOR ^
true if only one bit is true
0 & 1 = 1
1 & 1 = 0
NOT ~
one’s complement operator
flips the input bit
~1 = 0
~0 = 1
LEFT SHIFT <
Shift the binary digits by n, pad 0’s on the right
each shift is a multiply by 2 unless there is an overflow
00010110
«
00000010 it’s in the second column so it will be shifted by 2 columns
01011000
what happens when u left shift out of 8 bits?
some processors throw it away or they wrap around which is called rotation
RIGHT SHIFT»_space;
shift binary digits by n, pad 0’s on the left
each shift is a divide by 2 with round towards negative infinity