Write a Program in C using bitwise operator only .
[Link] a Program in C to set 3rd and 2nd bit .
i/p: int n= 51 o/p : 63
2. Write a Program in C to set 0th and 5th bit .
i/p: int n= 128 o/p: 161
3. Write a Program in C to clear 3rd and 2nd bit .
i/p: int n= 63 o/p: 51
[Link] a Program in C to toggle 1st and 4th bit .
i/p: int n= 42 o/p: 56
5. Write a Program in C to delete 0th bit .
i/p: int n= 170 o/p: 85
6. Write a Program in C to delete 0th, 1st , 2nd bit .
i/p: int n= 511 o/p: 63
7. Write a Program in C to delete 2nd bit .
i/p: int n= 39 o/p: 19
8. Write a Program in C to delete 5th bit .
i/p: int n= 99 o/p: 35
---------------------------------------$ Pawan KY
Hints:
How to delete bit in num :
**********************
1. to delete left most bit
: <<
2. to delete right most bit
: >>
3. to delete in b/w : it’s very tricky
// need to write logic with help of [ >> , << , | ]
Ex.
WAP in C to delete 3rd bit in unsigned int num:
unsigned int n= 50;
// i/p : 50
0000 0000 0000 0000 0000 0000 0011 0010
// o/p: 26
0000 0000 0000 0000 0000 0000 0001 1010
How to delete bit : try to understand
--------------------------->
1st 3 bit need to store . n1=n<<29;
n1=n1>>29;
then delete rightmost 4 bit , n2= n>>4 ;
then left shift 3 times . n2=n2<<3;
then bitwise OR // n=n1|n2;
i/p: is 50 , 3rd bit need to delete .
0000 0000 0000 0000 0000 0000 0011 0010
Step1. n1= n << 29;
0100 0000 0000 0000 0000 0000 0000 0000
Step2. n1 = n1>>29;
0000 0000 0000 0000 0000 0000 0000 0010
Step3. n2= n >> 4;
0000 0000 0000 0000 0000 0000 0000 0011
Step4. n2 = n2 << 3;
0000 0000 0000 0000 0000 0000 0001 1000
Step5. n= n1 | n2 ;
0000 0000 0000 0000 0000 0000 0000 0010
0000 0000 0000 0000 0000 0000 0001 1000
0000 0000 0000 0000 0000 0000 0001 1010
// o/p : 26
--------------------------------------------------------------------$ Coding Sirji