PART-2
BIT MANIPULATION OPERATORS
AND LEVEL-2 PROGRAMS
Shivaleela Hoonalli
Level-2 Programs
11. Print n bits of number from MSB to LSB
12. Print the binary representation of a number.
13. Count the number of set bits in the number.
14. Swap the two numbers using bit manipulation operators.
15. Reverse the bits of a number.
16. Check if the number is power of 2.
17. Check whether the number is even or odd.
18. Multiply any number by 9 in fastest manner.
19. Write a program to implement circular right shift of a number.
20. Write a program to implement circular left shift a number.
Operators
[Link] &:It takes two operands as inputs, does the and operation on each bit of two numbers. The result of
& operation is ‘1’ only if both the bits are 1,otherwise ‘0’.
[Link] |:It takes two operands as input,does the or operation on every bit of two [Link] result of |
operation is ‘0’ if both the bits are ‘0’,otherwise ‘1’.
[Link] ~:It takes one operand as a input,does the invert operation on each bit of the number.
[Link] ^:It takes two operands as input,does the xor operation on every bit of two [Link] result of
the operation is ‘1’ if both the bits are different, otherwise ‘0’.
[Link] right shift(>>):It takes the two operands as a input, right shift the bits of first operand,second
operand decides by how many bit positions we need to right shift the first operand.
[Link] left shift(<<):It takes the two operands as a input, left shift the bits of first operand,second operand
decides by how many bit positions we need to left shift the first operand.
11. Print n bits of number from MSB to LSB
Let’s have num =9 and n=4 Explanation:
Num=0000 1 0 0 1
LSB
n=4 so i range will be 0 to 3
1st iteration: i=3
num & 1<<3=00001001 & 00001000=
0001000→Non zero so print ‘1’
2nd iteration: i=2
num & 1<<3=00001001 & 00000100=
00000000→zero so print ‘0’
3rd iteration: i=1
num & 1<<3=00001001 & 00000001=
00000000→zero so print ‘0’
4th iteration: i=0
num & 1<<3=00001001 & 00000001=
00000001→non-zero so print ‘1’
[Link] the binary representation of a number.
Explanation:
Let’s have num =9 Num=0000 1 0 0 1
LSB
n=1*8->8 so i range will be 0 to 7
1st iteration: i=7
num & 1<<7=00001001 & 10000000=
0000000→zero so print ‘0’
2nd iteration: i=6
num & 1<<6=00001001 & 01000000=
00000000→zero so print ‘0’
3rd iteration: i=5
num & 1<<5=00001001 & 00100000=
00000000→zero so print ‘0’
4th iteration: i=4
num & 1<<4=00001001 & 00010000=
00000000→zero so print ‘0’
5th iteration: i=3
num & 1<<3=00001001 & 00001000=
00001000→non zero so print ‘1’
6th iteration: i=2
num & 1<<2=00001001 & 00000100=
00000000→zero so print ‘0’
7th iteration: i=1
num & 1<<1=00001001 & 00000010=
00000000→ zero so print ‘0’
8th iteration: i=0
num & 1<<0=00001001 & 00000001=
00000001→non zero so print ‘1’
13. Count the number of set bits in the number.
Explanation:
Let’s have num=13. Num=0000 1 1 0 1
LSB
n=8 so i range will be 0 to 7
1st iteration: i=7
num & 1<<7=00001101 & 10000000=
0000000→don’t increment the count
2nd iteration: i=6
num & 1<<6=00001101 & 01000000=
00000000→don’t increment the count
3rd iteration: i=5
num & 1<<5=00001101 & 00100000=
00000000→don’t increment the count
4th iteration: i=4
num & 1<<4=00001101 & 00010000=
00000000→don’t increment the count
5th iteration: i=3
num & 1<<3=00001101 & 00001000=
00001000→ increment the count
6th iteration: i=2
num & 1<<2=00001101 & 00000100=
00000100→ increment the count
7th iteration: i=1
num & 1<<1=00001101 & 00000010=
00000000→ don’t increment the count
8th iteration: i=0
num & 1<<0=00001101 & 00000001=
00000001→ increment the count
14. Swap the two numbers using bit manipulation operators.
Let’s have num1=10 and num2=11
Explanation:
num1=10,num2=11
num1=00001010 num2=00001011
num1=num1^
num2→00001010^00001011→00000001
num2=num1^num2→000000001^00001011→
00001010→10
num1=num1^num2→00000001^00001011→
00001011→11
Explanation:
Num=0000 1 0 0 0
15. Reverse the bits of a number LSB
n=8 so pos range will be 0 to 7
Let’s have num=8 1st iteration: pos=0
Temp=temp<<1→00000000<<1→00000000
Temp=temp | (num &1)→00000000 |(00001000&1)→00000000
Num=num>>1→00001000>>1→00000100
2nd iteration: pos=6
Temp=temp<<1→00000000<<1→00000000
Temp=temp | (num &1)→00000000 |(00000100&1)→00000000
Num=num>>1→00000100>>1→00000010
3rd iteration: pos =5
Temp=temp<<1→00000000<<1→00000000
Temp=temp | (num &1)→00000000 |(00000010&1)→00000000
Num=num>>1→00000010>>1→00000001
4th iteration: pos =4
Temp=temp<<1→00000000<<1→00000000
Temp=temp | (num &1)→00000000 |(00000001 &1)→00000001
Num=num>>1→00000001>>1→00000000
5th iteration: pos =3
Temp=temp<<1→00000001<<1→00000010
Temp=temp | (num &1)→00000010 |(00000000 &1)→00000010
Num=num>>1→00000000>>1→00000000
6th iteration: pos =2
Temp=temp<<1→00000010<<1→00000100
Temp=temp | (num &1)→00000100 |(00000000 &1)→00000100
Num=num>>1→00000000>>1→00000000
7th iteration: pos =1
Temp=temp<<1→00000100<<1→00001000
Temp=temp | (num &1)→00001000 |(00000000 &1)→00001000
Num=num>>1→00000000>>1→00000000
8th iteration: pos =0
Temp=temp<<1→00001000<<1→00010000
Temp=temp | (num &1)→00010000|(00000000 &1)→00010000
Num=num>>1→00000000>>1→00000000
16. Check if the number is power of 2
Let’s have num=12 and num=16
Explanation:
Case1:Num= 00001100→12
Mask=num-1→(12-1)→00001011
Result=num & mask→00001100 &
00001011→00001000→Non-zero so its not
power of 2
Case2:num=16→00010000
Mask=num-1→16-1→15→00001111
Result=num &
mask→00010000&00001111→00000000→
zero so it’s a power of 2
[Link] whether the number is even or odd
Let’s have a num=11
Explanation:
Case1:Num=00001011
Num &1→00001011 &
00000001→00000001→non zero→odd
Case2:12
Num &1→00001100 &
1→00000000→zero→even
18. Multiply any number by 9 in fastest manner
Let’s have a num=12
Explanation:
Case1:Num=00001100
Num=(num <<3) +num→(00001100<<3)+12
01100000+12→64+32+12→108
[Link] a program to implement circular right shift of a number
Let’s have a num=12 and n=3
Explanation:
Num=12,n=3
Sizeof(num)→4 bytes
Mask=(1<<n)-1→(1<<3)-1→7
Num &mask=00001100 &
00000111→0000000000000000000000000
0000100
(Num & mask)<<sizeof(num)-3→
00000000000000000000000000000100
<<27→100000000000000000000000000000
00
Num>>n→12>>3→00000000000000000000
000000000001
((Num & mask)<<sizeof(num)-3) |
num>>n→100000000000000000000000000
00001
[Link] a program to implement circular left shift a number
Let’s have num=44,n=3
Explanation:
Num=00101100,n=3
Sizeof(num)→1 bytes
Size=sizeof(num)*8→8
Mask=((1<<n)-1)<<size-n→11100000
Result1=(num & mask)>>size-n→00000001
Result1=(num<<n)|result1→01100000|000
00001→01100001
Thank you
Shivaleela Hoonalli