bitwise operators - bangladesh university of …ashikur.buet.ac.bd/cse101/bitwise.pdfbitwise...
TRANSCRIPT
![Page 1: Bitwise operators - Bangladesh University of …ashikur.buet.ac.bd/CSE101/bitWise.pdfBitwise operators •Read Kernighan & Ritchie Page 48-49 •Lecture Slides •Reading material](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af03b7e708231d42e148c/html5/thumbnails/1.jpg)
Bitwise operators
![Page 2: Bitwise operators - Bangladesh University of …ashikur.buet.ac.bd/CSE101/bitWise.pdfBitwise operators •Read Kernighan & Ritchie Page 48-49 •Lecture Slides •Reading material](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af03b7e708231d42e148c/html5/thumbnails/2.jpg)
• Read Kernighan & Ritchie Page 48-49
• Lecture Slides
• Reading material from the course website
Reading list
• Reading material from the course website
A2+B2: Lab exam (online) after midterm break
![Page 3: Bitwise operators - Bangladesh University of …ashikur.buet.ac.bd/CSE101/bitWise.pdfBitwise operators •Read Kernighan & Ritchie Page 48-49 •Lecture Slides •Reading material](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af03b7e708231d42e148c/html5/thumbnails/3.jpg)
a & 0 0 a | 0 a a ^ 0 a
Most Commonly Used Laws of Operators
a & 1 a
a & a a
a | 1 1
a | a a
a ^ 1 ~a
a ^ a 0
![Page 4: Bitwise operators - Bangladesh University of …ashikur.buet.ac.bd/CSE101/bitWise.pdfBitwise operators •Read Kernighan & Ritchie Page 48-49 •Lecture Slides •Reading material](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af03b7e708231d42e148c/html5/thumbnails/4.jpg)
Fill it with 0
For signed numbers fill it with 1
Fill it with 0
![Page 5: Bitwise operators - Bangladesh University of …ashikur.buet.ac.bd/CSE101/bitWise.pdfBitwise operators •Read Kernighan & Ritchie Page 48-49 •Lecture Slides •Reading material](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af03b7e708231d42e148c/html5/thumbnails/5.jpg)
Some quick examples
Determining a number positive or negative?
p
0 1 0 0 …
9 8 7 6 5 4 3 2 1 0
0 1 0 0 0 1 1 0 1 1
31 30 29 28
Positive
0 1 0 0 … 0 1 0 0 0 1 1 0 1 1
p
1 1 0 0 …
9 8 7 6 5 4 3 2 1 0
0 1 0 0 0 1 1 0 1 1
31 30 29 28
Negative
![Page 6: Bitwise operators - Bangladesh University of …ashikur.buet.ac.bd/CSE101/bitWise.pdfBitwise operators •Read Kernighan & Ritchie Page 48-49 •Lecture Slides •Reading material](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af03b7e708231d42e148c/html5/thumbnails/6.jpg)
Some quick examples
Determining a number odd or even?p
0 1 0 0 …
9 8 7 6 5 4 3 2 1 0
0 1 0 0 0 1 1 0 1 1
31 30 29 28
ODD
0 1 0 0 … 0 1 0 0 0 1 1 0 1 1
p
0 1 0 0 …
9 8 7 6 5 4 3 2 1 0
0 1 0 0 0 1 1 0 1 0
31 30 29 28
Even
![Page 7: Bitwise operators - Bangladesh University of …ashikur.buet.ac.bd/CSE101/bitWise.pdfBitwise operators •Read Kernighan & Ritchie Page 48-49 •Lecture Slides •Reading material](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af03b7e708231d42e148c/html5/thumbnails/7.jpg)
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
setBit(x, 6)
x0 1 0 0 …
9 8 7 6 5 4 3 2 1 0
0 1 0 0 0 1 1 0 1 1
31 30 29 28
![Page 8: Bitwise operators - Bangladesh University of …ashikur.buet.ac.bd/CSE101/bitWise.pdfBitwise operators •Read Kernighan & Ritchie Page 48-49 •Lecture Slides •Reading material](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af03b7e708231d42e148c/html5/thumbnails/8.jpg)
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
setBit(x, 6)
x0 1 0 0 …
9 8 7 6 5 4 3 2 1 0
0 1 0 1 0 1 1 0 1 1
31 30 29 28
![Page 9: Bitwise operators - Bangladesh University of …ashikur.buet.ac.bd/CSE101/bitWise.pdfBitwise operators •Read Kernighan & Ritchie Page 48-49 •Lecture Slides •Reading material](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af03b7e708231d42e148c/html5/thumbnails/9.jpg)
setBit(x,p)
setBit(x, 6) p
a | 0 aa | 1 1a | 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)
x0 1 0 0 …
9 8 7 6 5 4 3 2 1 0
0 1 0 0 0 1 1 0 1 1
31 30 29 28
0 0 0 0 …
9 8 7 6 5 4 3 2 1 0
0 0 0 1 0 0 0 0 0 0
31 30 29 28
m
![Page 10: Bitwise operators - Bangladesh University of …ashikur.buet.ac.bd/CSE101/bitWise.pdfBitwise operators •Read Kernighan & Ritchie Page 48-49 •Lecture Slides •Reading material](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af03b7e708231d42e148c/html5/thumbnails/10.jpg)
p
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
setBits(x, 6, 3)
x0 1 0 0 …
9 8 7 6 5 4 3 2 1 0
0 1 0 0 0 1 1 0 1 1
31 30 29 28
![Page 11: Bitwise operators - Bangladesh University of …ashikur.buet.ac.bd/CSE101/bitWise.pdfBitwise operators •Read Kernighan & Ritchie Page 48-49 •Lecture Slides •Reading material](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af03b7e708231d42e148c/html5/thumbnails/11.jpg)
p
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
setBits(x, 6, 3)
x0 1 0 0 …
9 8 7 6 5 4 3 2 1 0
0 1 0 1 1 1 1 0 1 1
31 30 29 28
Call setBit(x,p) in a loop
![Page 12: Bitwise operators - Bangladesh University of …ashikur.buet.ac.bd/CSE101/bitWise.pdfBitwise operators •Read Kernighan & Ritchie Page 48-49 •Lecture Slides •Reading material](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af03b7e708231d42e148c/html5/thumbnails/12.jpg)
setBits(x, 6, 3)
x
p
0 1 0 0 …
9 8 7 6 5 4 3 2 1 0
0 1 0 1 1 1 1 0 1 1
31 30 29 28
Efficient setBits(x,p,n)
9 8 7 6 5 4 3 2 1 031 30 29 28
0 0 0 0 … 0 0 0 1 1 1 0 0 0 0
31 30 29 28
m
x | m
![Page 13: Bitwise operators - Bangladesh University of …ashikur.buet.ac.bd/CSE101/bitWise.pdfBitwise operators •Read Kernighan & Ritchie Page 48-49 •Lecture Slides •Reading material](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af03b7e708231d42e148c/html5/thumbnails/13.jpg)
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
resetBit(x, 6)
x0 1 0 0 …
9 8 7 6 5 4 3 2 1 0
0 1 0 1 0 1 1 0 1 1
31 30 29 28
![Page 14: Bitwise operators - Bangladesh University of …ashikur.buet.ac.bd/CSE101/bitWise.pdfBitwise operators •Read Kernighan & Ritchie Page 48-49 •Lecture Slides •Reading material](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af03b7e708231d42e148c/html5/thumbnails/14.jpg)
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
resetBit(x, 6)
x0 1 0 0 …
9 8 7 6 5 4 3 2 1 0
0 1 0 0 0 1 1 0 1 1
31 30 29 28
![Page 15: Bitwise operators - Bangladesh University of …ashikur.buet.ac.bd/CSE101/bitWise.pdfBitwise operators •Read Kernighan & Ritchie Page 48-49 •Lecture Slides •Reading material](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af03b7e708231d42e148c/html5/thumbnails/15.jpg)
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
resetBit(x, 6) p
a & 0 0a & 1 aa & a a
resetBit(x, 6)
x0 1 0 0 …
9 8 7 6 5 4 3 2 1 0
0 1 0 1 0 1 1 0 1 1
31 30 29 28
1 1 1 1 … 1
9 8 7 6 5 4 3 2 1 0
1 1 1 0 1 1 1 1 1 1
31 30 29 28
m
![Page 16: Bitwise operators - Bangladesh University of …ashikur.buet.ac.bd/CSE101/bitWise.pdfBitwise operators •Read Kernighan & Ritchie Page 48-49 •Lecture Slides •Reading material](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af03b7e708231d42e148c/html5/thumbnails/16.jpg)
p
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
resetBits(x, 6, 3)
x0 1 0 0 …
9 8 7 6 5 4 3 2 1 0
0 1 0 1 0 1 1 0 1 1
31 30 29 28
![Page 17: Bitwise operators - Bangladesh University of …ashikur.buet.ac.bd/CSE101/bitWise.pdfBitwise operators •Read Kernighan & Ritchie Page 48-49 •Lecture Slides •Reading material](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af03b7e708231d42e148c/html5/thumbnails/17.jpg)
p
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
resetBits(x, 6, 3)
x0 1 0 0 …
9 8 7 6 5 4 3 2 1 0
0 1 0 0 0 0 1 0 1 1
31 30 29 28
Call resetBit(x,p) in a loop
![Page 18: Bitwise operators - Bangladesh University of …ashikur.buet.ac.bd/CSE101/bitWise.pdfBitwise operators •Read Kernighan & Ritchie Page 48-49 •Lecture Slides •Reading material](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af03b7e708231d42e148c/html5/thumbnails/18.jpg)
resetBits(x, 6, 3)
x
p
0 1 0 0 …
9 8 7 6 5 4 3 2 1 0
0 1 0 1 1 1 1 0 1 1
31 30 29 28
Efficient resetBits(x,p,n)
9 8 7 6 5 4 3 2 1 031 30 29 28
1 1 1 1 … 1 1 1 0 0 0 1 1 1 1
31 30 29 28
m
x & m
![Page 19: Bitwise operators - Bangladesh University of …ashikur.buet.ac.bd/CSE101/bitWise.pdfBitwise operators •Read Kernighan & Ritchie Page 48-49 •Lecture Slides •Reading material](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af03b7e708231d42e148c/html5/thumbnails/19.jpg)
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
invertBit(x, 6)
x0 1 0 0 …
9 8 7 6 5 4 3 2 1 0
0 1 0 0 0 1 1 0 1 1
31 30 29 28
![Page 20: Bitwise operators - Bangladesh University of …ashikur.buet.ac.bd/CSE101/bitWise.pdfBitwise operators •Read Kernighan & Ritchie Page 48-49 •Lecture Slides •Reading material](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af03b7e708231d42e148c/html5/thumbnails/20.jpg)
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
invertBit(x, 6)
x0 1 0 0 …
9 8 7 6 5 4 3 2 1 0
0 1 0 1 0 1 1 0 1 1
31 30 29 28
![Page 21: Bitwise operators - Bangladesh University of …ashikur.buet.ac.bd/CSE101/bitWise.pdfBitwise operators •Read Kernighan & Ritchie Page 48-49 •Lecture Slides •Reading material](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af03b7e708231d42e148c/html5/thumbnails/21.jpg)
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
invertBit(x, 6) p
a ^ 0 aa ^ 1 ~aa ^ a 0
invertBit(x, 6)
x0 1 0 0 …
9 8 7 6 5 4 3 2 1 0
0 1 0 0 0 1 1 0 1 1
31 30 29 28
0 0 0 0 …
9 8 7 6 5 4 3 2 1 0
0 0 0 1 0 0 0 0 0 0
31 30 29 28
m
![Page 22: Bitwise operators - Bangladesh University of …ashikur.buet.ac.bd/CSE101/bitWise.pdfBitwise operators •Read Kernighan & Ritchie Page 48-49 •Lecture Slides •Reading material](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af03b7e708231d42e148c/html5/thumbnails/22.jpg)
p
invertBits(x,p,n)
Write down a function invertBits(x,p,n) that will invertn bits of the integer x starting from position p leaving other bits unchanged. Assume 0 ≤ p ≤31 and n ≤ p+1
invertBits(x, 6, 3)
x0 1 0 0 …
9 8 7 6 5 4 3 2 1 0
0 1 0 0 0 1 1 0 1 1
31 30 29 28
![Page 23: Bitwise operators - Bangladesh University of …ashikur.buet.ac.bd/CSE101/bitWise.pdfBitwise operators •Read Kernighan & Ritchie Page 48-49 •Lecture Slides •Reading material](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af03b7e708231d42e148c/html5/thumbnails/23.jpg)
p
invertBits(x,p,n)
Write down a function invertBits(x,p,n) that will invertn bits of the integer x starting from position p leaving other bits unchanged. Assume 0 ≤ p ≤31 and n ≤ p+1
inverBits(x, 6, 3)
x0 1 0 0 …
9 8 7 6 5 4 3 2 1 0
0 1 0 1 1 0 1 0 1 1
31 30 29 28
Call invertBit(x,p) in a loop
![Page 24: Bitwise operators - Bangladesh University of …ashikur.buet.ac.bd/CSE101/bitWise.pdfBitwise operators •Read Kernighan & Ritchie Page 48-49 •Lecture Slides •Reading material](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af03b7e708231d42e148c/html5/thumbnails/24.jpg)
invertBits(x, 6, 3)
x
p
0 1 0 0 …
9 8 7 6 5 4 3 2 1 0
0 1 0 1 1 0 1 0 1 1
31 30 29 28
Efficient invertBits(x,p,n)
9 8 7 6 5 4 3 2 1 031 30 29 28
0 0 0 0 … 0 0 0 1 1 1 0 0 0 0
31 30 29 28
m
x ^ m
![Page 25: Bitwise operators - Bangladesh University of …ashikur.buet.ac.bd/CSE101/bitWise.pdfBitwise operators •Read Kernighan & Ritchie Page 48-49 •Lecture Slides •Reading material](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af03b7e708231d42e148c/html5/thumbnails/25.jpg)
RIGHT CIRCULAR SHIFT
int circularRightShift(int x){unsigned int y = x;unsigned int y = x;int s = 8*sizeof(int);return (y >> 1) | x << (s-1);
}
rightRotate (x, n)
![Page 26: Bitwise operators - Bangladesh University of …ashikur.buet.ac.bd/CSE101/bitWise.pdfBitwise operators •Read Kernighan & Ritchie Page 48-49 •Lecture Slides •Reading material](https://reader033.vdocuments.us/reader033/viewer/2022060311/5f0af03b7e708231d42e148c/html5/thumbnails/26.jpg)
xtractRightMostBits(x, 3)
xtractRightMostBits(x,n)
Write down a function xtractRightMostBits(x,n) that will return rightmost n bits of the integer x.
Assume 1 ≤ n ≤32n
xtractRightMostBits(x, 3)
x0 1 0 0 …
9 8 7 6 5 4 3 2 1 0
0 1 0 0 0 1 1 0 1 1
31 30 29 28
n
0 0 0 0 …
9 8 7 6 5 4 3 2 1 0
0 0 0 0 0 0 0 0 1 1
31 30 29 28