Bitwise operators - Bangladesh University of …ashikur.buet.ac.bd/CSE101/bitWise.pdfBitwise...

26
Bitwise operators

Transcript of Bitwise operators - Bangladesh University of …ashikur.buet.ac.bd/CSE101/bitWise.pdfBitwise...

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

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

• 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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