0% found this document useful (0 votes)
16 views26 pages

Bit Wise

The document discusses bitwise operators and provides examples and functions to set, reset, invert bits in integers. It defines functions like setBit, resetBit, invertBit, setBits, resetBits, invertBits explaining how to manipulate individual bits and ranges of bits in binary representations of numbers.

Uploaded by

niloy2105044
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views26 pages

Bit Wise

The document discusses bitwise operators and provides examples and functions to set, reset, invert bits in integers. It defines functions like setBit, resetBit, invertBit, setBits, resetBits, invertBits explaining how to manipulate individual bits and ranges of bits in binary representations of numbers.

Uploaded by

niloy2105044
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Bitwise operators

Reading list

• Read Kernighan & Ritchie Page 48-49


• Lecture Slides

• Reading material from the course website

A2+B2: Lab exam (online) after


midterm break
Most Commonly Used Laws of
Operators

a&00 a|0a a^0a

a&1a a|11 a ^ 1  ~a

a&aa a|aa a^a0


Fill it with 0

For signed numbers fill it with 1


Some quick examples
Determining a number positive or negative?
Positive
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

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|0a
a|11 setBit(x,p)
a|aa
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)

Write down a function setBits(x,p,n) that will set the n


bits of the integer x starting from position p leaving
other bits unchanged. Assume 0 ≤ p ≤31 and n ≤ p+1
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

setBits(x, 6, 3)
setBits(x,p,n)

Write down a function setBits(x,p,n) that will set the n


bits of the integer x starting from position p leaving
other bits unchanged. Assume 0 ≤ p ≤31 and n ≤ p+1
p

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

setBits(x, 6, 3) Call setBit(x,p) in a


loop
Efficient setBits(x,p,n)
p
setBits(x, 6, 3)
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
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&00
a&1a resetBit(x,p)
a&aa
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)

Write down a function resetBits(x,p,n) that will reset the n


bits of the integer x starting from position p leaving other
bits unchanged. Assume 0 ≤ p ≤31 and n ≤ p+1
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

resetBits(x, 6, 3)
resetBits(x,p,n)

Write down a function resetBits(x,p,n) that will reset the n


bits of the integer x starting from position p leaving other
bits unchanged. Assume 0 ≤ p ≤31 and n ≤ p+1
p

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

resetBits(x, 6, 3) Call resetBit(x,p) in


a loop
Efficient resetBits(x,p,n)
p
resetBits(x, 6, 3)
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
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^0a
a ^ 1  ~a invertBit(x,p)
a^a0
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)

Write down a function invertBits(x,p,n) that will invert


n bits of the integer x starting from position p leaving
other bits unchanged. Assume 0 ≤ p ≤31 and n ≤ p+1
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

invertBits(x, 6, 3)
invertBits(x,p,n)

Write down a function invertBits(x,p,n) that will invert


n bits of the integer x starting from position p leaving
other bits unchanged. Assume 0 ≤ p ≤31 and n ≤ p+1
p

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

inverBits(x, 6, 3) Call invertBit(x,p)


in a loop
Efficient invertBits(x,p,n)
p
invertBits(x, 6, 3)
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
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

int circularRightShift(int x){


unsigned int y = x;
int s = 8*sizeof(int);
return (y >> 1) | x << (s-1);

rightRotate (x, n)
xtractRightMostBits(x,n)

Write down a function xtractRightMostBits(x,n) that


will return rightmost n bits of the integer x.
Assume 1 ≤ n ≤32
n
xtractRightMostBits(x, 3)
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

0 0 0 0 … 0 0 0 0 0 0 0 0 1 1

You might also like