LSB – least significant beat, most right bit
MSB – most significant bit, most left bit
code below shows if there is a bit in k position from leeast significant bit
int getBit(int n, int k) {
return (n >> k) & 1;
}
// Explanation (in bits):
n
100010101011101010 (example)
n >> 5
000001000101010111 (all bits are moved over 5 spots, therefore
& the bit you want is at the end)
000000000000000001 (0 means it will always be 0,
= 1 means that it will keep the old value)
1