Bit Wise
Bit Wise
Bit Wise
Reading list
a&1a a|11 a ^ 1 ~a
31 30 29 28 9 8 7 6 5 4 3 2 1 0
0 1 0 0 … 0 1 0 0 0 1 1 0 1 1
Negative
p
31 30 29 28 9 8 7 6 5 4 3 2 1 0
1 1 0 0 … 0 1 0 0 0 1 1 0 1 1
Some quick examples
Determining a number odd or even?
ODD p
31 30 29 28 9 8 7 6 5 4 3 2 1 0
0 1 0 0 … 0 1 0 0 0 1 1 0 1 1
Even p
31 30 29 28 9 8 7 6 5 4 3 2 1 0
0 1 0 0 … 0 1 0 0 0 1 1 0 1 0
setBit(x,p)
Write down a function setBit(x,p) that will set a bit of
integer x at position p leaving other bits unchanged.
Assume 0 ≤ p ≤31
p
31 30 29 28 9 8 7 6 5 4 3 2 1 0
0 1 0 0 … 0 1 0 0 0 1 1 0 1 1
x
setBit(x, 6)
setBit(x,p)
Write down a function setBit(x,p) that will set a bit of
integer x at position p leaving other bits unchanged.
Assume 0 ≤ p ≤31
p
31 30 29 28 9 8 7 6 5 4 3 2 1 0
0 1 0 0 … 0 1 0 1 0 1 1 0 1 1
x
setBit(x, 6)
a|0a
a|11 setBit(x,p)
a|aa
Write down a function setBit(x,p) that will set a bit of
integer x at position p leaving other bits unchanged.
Assume 0 ≤ p ≤31
setBit(x, 6) p
31 30 29 28 9 8 7 6 5 4 3 2 1 0
0 1 0 0 … 0 1 0 0 0 1 1 0 1 1
x
31 30 29 28 9 8 7 6 5 4 3 2 1 0
m 0 0 0 0 … 0 0 0 1 0 0 0 0 0 0
setBits(x,p,n)
31 30 29 28 9 8 7 6 5 4 3 2 1 0
0 1 0 0 … 0 1 0 0 0 1 1 0 1 1
x
setBits(x, 6, 3)
setBits(x,p,n)
31 30 29 28 9 8 7 6 5 4 3 2 1 0
0 1 0 0 … 0 1 0 1 1 1 1 0 1 1
x
0 1 0 0 … 0 1 0 1 1 1 1 0 1 1
x
31 30 29 28 9 8 7 6 5 4 3 2 1 0
m 0 0 0 0 … 0 0 0 1 1 1 0 0 0 0
x|m
resetBit(x,p)
Write down a function resetBit(x,p) that will reset a bit
of integer x at position p leaving other bits unchanged.
Assume 0 ≤ p ≤31
p
31 30 29 28 9 8 7 6 5 4 3 2 1 0
0 1 0 0 … 0 1 0 1 0 1 1 0 1 1
x
resetBit(x, 6)
resetBit(x,p)
Write down a function resetBit(x,p) that will reset a bit
of integer x at position p leaving other bits unchanged.
Assume 0 ≤ p ≤31
p
31 30 29 28 9 8 7 6 5 4 3 2 1 0
0 1 0 0 … 0 1 0 0 0 1 1 0 1 1
x
resetBit(x, 6)
a&00
a&1a resetBit(x,p)
a&aa
Write down a function resetBit(x,p) that will reset a bit
of integer x at position p leaving other bits unchanged.
Assume 0 ≤ p ≤31
resetBit(x, 6) p
31 30 29 28 9 8 7 6 5 4 3 2 1 0
0 1 0 0 … 0 1 0 1 0 1 1 0 1 1
x
31 30 29 28 9 8 7 6 5 4 3 2 1 0
m 1 1 1 1 … 11 1 1 0 1 1 1 1 1 1
resetBits(x,p,n)
31 30 29 28 9 8 7 6 5 4 3 2 1 0
0 1 0 0 … 0 1 0 1 0 1 1 0 1 1
x
resetBits(x, 6, 3)
resetBits(x,p,n)
31 30 29 28 9 8 7 6 5 4 3 2 1 0
0 1 0 0 … 0 1 0 0 0 0 1 0 1 1
x
0 1 0 0 … 0 1 0 1 1 1 1 0 1 1
x
31 30 29 28 9 8 7 6 5 4 3 2 1 0
m 1 1 1 1 … 1 1 1 0 0 0 1 1 1 1
x&m
invertBit(x,p)
Write down a function invertBit(x,p) that will invert a
bit of integer x at position p leaving other bits
unchanged. Assume 0 ≤ p ≤31
p
31 30 29 28 9 8 7 6 5 4 3 2 1 0
0 1 0 0 … 0 1 0 0 0 1 1 0 1 1
x
invertBit(x, 6)
invertBit(x,p)
Write down a function setBit(x,p) that will invert a bit
of integer x at position p leaving other bits unchanged.
Assume 0 ≤ p ≤31
p
31 30 29 28 9 8 7 6 5 4 3 2 1 0
0 1 0 0 … 0 1 0 1 0 1 1 0 1 1
x
invertBit(x, 6)
a^0a
a ^ 1 ~a invertBit(x,p)
a^a0
Write down a function invertBit(x,p) that will invert a
bit of integer x at position p leaving other bits
unchanged. Assume 0 ≤ p ≤31
invertBit(x, 6) p
31 30 29 28 9 8 7 6 5 4 3 2 1 0
0 1 0 0 … 0 1 0 0 0 1 1 0 1 1
x
31 30 29 28 9 8 7 6 5 4 3 2 1 0
m 0 0 0 0 … 0 0 0 1 0 0 0 0 0 0
invertBits(x,p,n)
31 30 29 28 9 8 7 6 5 4 3 2 1 0
0 1 0 0 … 0 1 0 0 0 1 1 0 1 1
x
invertBits(x, 6, 3)
invertBits(x,p,n)
31 30 29 28 9 8 7 6 5 4 3 2 1 0
0 1 0 0 … 0 1 0 1 1 0 1 0 1 1
x
0 1 0 0 … 0 1 0 1 1 0 1 0 1 1
x
31 30 29 28 9 8 7 6 5 4 3 2 1 0
m 0 0 0 0 … 0 0 0 1 1 1 0 0 0 0
x^m
RIGHT CIRCULAR SHIFT
rightRotate (x, n)
xtractRightMostBits(x,n)
0 1 0 0 … 0 1 0 0 0 1 1 0 1 1
x
31 30 29 28 9 8 7 6 5 4 3 2 1 0
0 0 0 0 … 0 0 0 0 0 0 0 0 1 1