Wednesday, 19 December 2018

Bitwise Operator in C Program


C Programs


Bitwise Operator Examples


1) WAP TO CHECK LEAST SIGNIFICANT BIT(LSB) OF A NUMBER IS SET OR NOT


1. /**
2.  * C program to check Least Significant Bit (LSB) of a number using bitwise operator
3.  */
4.  
5. #include <stdio.h>
6.  
7. int main()
8. {
9.     int num;
10.   
11.      /* Input number from user */
12.      printf("Enter any number: ");
13.      scanf("%d", &num);
14.   
15.      /* If (num & 1) evaluates to 1 */
16.      if(num & 1)
17.          printf("LSB of %d is set (1).", num);
18.      else
19.          printf("LSB of %d is unset (0).", num);
20.   
21.      return 0;
22.  }

Output:


Enter any number: 11
LSB of 11 is set (1)
 

2) WAP TO CHECK MOST SIGNIFICANT BIT(MSB)OF A NUMBER IS SET OR NOT

1. /**
2.  * C program to check Most Significant Bit (MSB) of a number using bitwise operator
3.  */
4.  
5. #include <stdio.h>
6. #define BITS sizeof(int) * 8 // Total bits required to represent integer
7.  
8. int main()
9. {
10.      int num, msb;
11.   
12.      /* Input number from user */
13.      printf("Enter any number: ");
14.      scanf("%d", &num);
15.   
16.      /* Move first bit of 1 to highest order */
17.      msb = 1 << (BITS - 1);
18.   
19.      /* Perform bitwise AND with msb and num */
20.      if(num & msb)
21.          printf("MSB of %d is set (1).", num);
22.      else
23.          printf("MSB of %d is unset (0).", num);
24.   
25.      return 0;
26.  }

Output:


Enter any number: -1
MSB of -1 is set (1).
 
 

3) WAP TO GET NTH BIT OF A NUMBER


1. /**
2.  * C program to get the nth bit of a number
3.  */
4.  
5. #include <stdio.h>
6.  
7. int main()
8. {
9.     int num, n, bitStatus;
10.   
11.      /* Input number from user */
12.      printf("Enter any number: ");
13.      scanf("%d", &num);
14.   
15.      /* Input bit position you want to check */
16.      printf("Enter nth bit to check (0-31): ");
17.      scanf("%d", &n);
18.   
19.      /* Right shift num, n times and perform bitwise AND with 1 */
20.      bitStatus = (num >> n) & 1;
21.   
22.      printf("The %d bit is set to %d", n, bitStatus);
23.   
24.      return 0;
25.  }

Output:


Enter any number: 12
Enter nth bit to check (0-31): 2
The 2 bit is set to 1
 
 

4) WAP TO SET NTH BIT OF A NUMBER


1. /**
2.  * C program to set the nth bit of a number
3.  */
4.  
5. #include <stdio.h>
6.  
7. int main()
8. {
9.     int num, n, newNum;
10.   
11.      /* Input number from user */
12.      printf("Enter any number: ");
13.      scanf("%d", &num);
14.   
15.      /* Input bit position you want to set */
16.      printf("Enter nth bit to set (0-31): ");
17.      scanf("%d", &n);
18.   
19.      /* Left shift 1, n times and perform bitwise OR with num */
20.      newNum = (1 << n) | num;
21.   
22.      printf("Bit set successfully.\n\n");
23.      printf("Number before setting %d bit: %d (in decimal)\n", n, num);
24.      printf("Number after setting %d bit: %d (in decimal)\n", n, newNum);
25.   
26.      return 0;
27.  }

Output:


 
 
Output
 
Enter any number: 12
Enter nth bit to set (0-31): 0
Bit set successfully.
 
Number before setting 0 bit: 12 (in decimal)
Number after setting 0 bit: 13 (in decimal)
 

5) WAP TO CLEAR NTH BIT OF A NUMBER

1.      /**
2.  * C program to clear the nth bit of a number
3.  */
4.  
5. #include <stdio.h>
6.  
7. int main()
8. {
9.     int num, n, newNum;
10.   
11.      /* Input number from user */
12.      printf("Enter any number: ");
13.      scanf("%d", &num);
14.   
15.      /* Input bit number you want to clear */
16.      printf("Enter nth bit to clear (0-31): ");
17.      scanf("%d", &n);
18.   
19.      /*
20.       * Left shifts 1 to n times
21.       * Perform complement of above
22.       * finally perform bitwise AND with num and result of above
23.       */
24.      newNum = num & (~(1 << n));
25.   
26.      printf("Bit cleared successfully.\n\n");
27.      printf("Number before clearing %d bit: %d (in decimal)\n", n, num);
28.      printf("Number after clearing %d bit: %d (in decimal)\n", n, newNum);
29.   
30.      return 0;
31.  }

Output:


Enter any number: 13
Enter nth bit to clear (0-31): 0
Bit cleared successfully.
 
Number before clearing 0 bit: 13 (in decimal)
Number after clearing 0 bit: 12 (in decimal)
 
 
6) WAP TO TOGGLE NTH BIT OF A NUMBER
1. /**
2.  * C program to toggle nth bit of a number
3.  */
4.  
5. #include <stdio.h>
6.  
7. int main()
8. {
9.     int num, n, newNum;
10.   
11.      /* Input number from user */
12.      printf("Enter any number: ");
13.      scanf("%d", &num);
14.   
15.      /* Input bit position you want to toggle */
16.      printf("Enter nth bit to toggle (0-31): ");
17.      scanf("%d", &n);
18.   
19.      /*
20.       * Left shifts 1, n times
21.       * then perform bitwise XOR with num
22.       */
23.      newNum = num ^ (1 << n);
24.   
25.      printf("Bit toggled successfully.\n\n");
26.      printf("Number before toggling %d bit: %d (in decimal)\n", n, num);
27.      printf("Number after toggling %d bit: %d (in decimal)\n", n, newNum);
28.   
29.      return 0;
30.  }

Output:


Enter any number: 22
Enter nth bit to toggle (0-31): 1
Bit toggled successfully.
 
Number before toggling 1 bit: 22 (in decimal)
Number after toggling 1 bit: 20 (in decimal)
 
7) WAP TO GET HIGHTEST SET BIT OF A NUMBER:
1. /**
2.  * C program to find highest order set bit in a number
3.  */
4.  
5. #include <stdio.h>
6. #define INT_SIZE sizeof(int) * 8 /* Integer size in bits */
7.  
8. int main()
9. {
10.      int num, order = -1, i;
11.   
12.      /* Input number from user */
13.      printf("Enter any number: ");
14.      scanf("%d", &num);
15.   
16.      /* Iterate over each bit of integer */
17.      for(i=0; i<INT_SIZE; i++)
18.      {
19.          /* If current bit is set */
20.          if((num>>i) & 1)
21.              order = i;
22.      }
23.   
24.      if (order != -1)
25.          printf("Highest order set bit in %d is %d", num, order);
26.      else 
27.          printf("0 has no set bits.");
28.   
29.      return 0;
30.  }

Output:


Enter any number: 22
Highest order set bit in 22 is 4
 
8) WAP TO GET LOWEST SET BIT OF A NUMBER
1. /**
2.  * C program to get lowest order set bit in a number
3.  */
4.  
5. #include <stdio.h>
6. #define INT_SIZE sizeof(int) * 8 /* Integer size in bits */
7.  
8. int main()
9. {
10.      int num, order, i;
11.   
12.      /* Input number from user */
13.      printf("Enter any number: ");
14.      scanf("%d", &num);
15.   
16.      /* Initially set the order to max size of integer */
17.      order = INT_SIZE - 1;
18.   
19.      /* Iterate through each bit of integer */
20.      for(i=0; i<INT_SIZE; i++)
21.      {
22.          /* If current bit is set */
23.          if((num>>i) & 1)
24.          {
25.              order = i;
26.   
27.              /* Terminate the loop */
28.              break;
29.          }
30.      }
31.   
32.      printf("Lowest order set bit in %d is %d", num, order);
33.   
34.      return 0;
35.  }

Output:


Enter any number: 22
Lowest order set bit in 22 is 1
 
9) WAP TO FLIP BITS OF A BINARY NUMBER USING BITWISE OPERATOR
1. /**
2.  * C program to count flip all bits of a binary number using bitwise operator
3.  */
4.  
5. #include <stdio.h>
6.  
7. int main()
8. {
9.     int num, flippedNumber;
10.   
11.      /* Input number from user */
12.      printf("Enter any number: ");
13.      scanf("%d", &num);
14.   
15.      flippedNumber = ~num;
16.   
17.      printf("Original number = %d (in decimal)\n", num);
18.      printf("Number after bits are flipped = %d (in decimal)", flippedNumber);
19.   
20.      return 0;
21.  }

Output:


 
 
Output
 
Enter any number: 22
Original number = 22 (in decimal)
Number after bits are flipped = -23 (in decimal) 

 


10)         WAP TO FLIP BITS OF A BINARY NUMBER USING BITWISE OPERATOR


1. /**
2.  * C program to count flip all bits of a binary number using bitwise operator
3.  */
4.  
5. #include <stdio.h>
6.  
7. int main()
8. {
9.     int num, flippedNumber;
10.   
11.      /* Input number from user */
12.      printf("Enter any number: ");
13.      scanf("%d", &num);
14.   
15.      flippedNumber = ~num;
16.   
17.      printf("Original number = %d (in decimal)\n", num);
18.      printf("Number after bits are flipped = %d (in decimal)", flippedNumber);
19.   
20.      return 0;
21.  }

Output:


 
 
Output
 
Enter any number: 22
Original number = 22 (in decimal)
Number after bits are flipped = -23 (in decimal)
 
11)  WAP TO GET LOWEST SET BIT OF A NUMBER
1. /**
2.  * C program to get lowest order set bit in a number
3.  */
4.  
5. #include <stdio.h>
6. #define INT_SIZE sizeof(int) * 8 /* Integer size in bits */
7.  
8. int main()
9. {
10.      int num, order, i;
11.   
12.      /* Input number from user */
13.      printf("Enter any number: ");
14.      scanf("%d", &num);
15.   
16.      /* Initially set the order to max size of integer */
17.      order = INT_SIZE - 1;
18.   
19.      /* Iterate through each bit of integer */
20.      for(i=0; i<INT_SIZE; i++)
21.      {
22.          /* If current bit is set */
23.          if((num>>i) & 1)
24.          {
25.              order = i;
26.   
27.              /* Terminate the loop */
28.              break;
29.          }
30.      }
31.   
32.      printf("Lowest order set bit in %d is %d", num, order);
33.   
34.      return 0;
35.  }

Output:


Enter any number: 22
Lowest order set bit in 22 is 1
 
12)  WAP TO COUNT TRAILING ZEROS IN A BINARY NUMBER
1. /**
2.  * C program to count trailing zeros in a binary number using bitwise operator
3.  */
4.  
5. #include <stdio.h>
6. #define INT_SIZE sizeof(int) * 8 /* Bits required to represent an integer */
7.  
8. int main()
9. {
10.      int num, count, i;
11.   
12.      /* Input number from user */
13.      printf("Enter any number: ");
14.      scanf("%d", &num);
15.   
16.      count = 0;
17.   
18.      /* Iterate over each bit of the number */
19.      for(i=0; i<INT_SIZE; i++)
20.      {
21.          /* If set bit is found the terminate from loop*/
22.          if((num >> i ) & 1)
23.          {
24.              /* Terminate from loop */
25.              break;
26.          }
27.   
28.          /* Increment trailing zeros count */
29.          count++;
30.      }
31.   
32.      printf("Total number of trailing zeros in %d is %d.", num, count);
33.   
34.      return 0;
35.  }

Output:


Enter any number: 48
Total number of trailing zeros in 48 is 4.
 
13)  WAP TO FLIP BITS OF A BINARY NUMBER USING BITWISE OPERATOR
1. /**
2.  * C program to count flip all bits of a binary number using bitwise operator
3.  */
4.  
5. #include <stdio.h>
6.  
7. int main()
8. {
9.     int num, flippedNumber;
10.   
11.      /* Input number from user */
12.      printf("Enter any number: ");
13.      scanf("%d", &num);
14.   
15.      flippedNumber = ~num;
16.   
17.      printf("Original number = %d (in decimal)\n", num);
18.      printf("Number after bits are flipped = %d (in decimal)", flippedNumber);
19.   
20.      return 0;
21.  }

Output:


 
 
Output
 
Enter any number: 22
Original number = 22 (in decimal)
Number after bits are flipped = -23 (in decimal)
 
14)  WAP TO COUNT TOTAL ZEROS AND ONES IN A BINARY NUMBER
1. /**
2.  * C program to count total of zeros and ones in a binary number using bitwise operator
3.  */
4.  
5. #include <stdio.h>
6. #define INT_SIZE sizeof(int) * 8 /* Total number of bits in integer */
7.  
8. int main()
9. {
10.      int num, zeros, ones, i;
11.   
12.      /* Input number from user */
13.      printf("Enter any number: ");
14.      scanf("%d", &num);
15.   
16.      zeros = 0;
17.      ones = 0;
18.   
19.      for(i=0; i<INT_SIZE; i++)
20.      {
21.          /* If LSB is set then increment ones otherwise zeros */
22.          if(num & 1)
23.              ones++;
24.          else
25.              zeros++;
26.   
27.          /* Right shift bits of num to one position */
28.          num >>= 1;
29.      }
30.   
31.      printf("Total zero bit is %d\n", zeros);
32.      printf("Total one bit is %d", ones);
33.   
34.      return 0;
35.  }

Output:


Enter any number: 22
Total zero bit is 29
Total one bit is 3 

 


 


15)         WAP TO COUNT LEADING ZEROS IN A BINARY NUMBER


1. /**
2.  * C program to count leading zeros in a binary number using bitwise operator
3.  */
4.  
5. #include <stdio.h>
6. #define INT_SIZE sizeof(int) * 8
7.  
8. int main()
9. {
10.      int num, count, msb, i;
11.   
12.      /* Input number from user */
13.      printf("Enter any number: ");
14.      scanf("%d", &num);
15.   
16.      // Equivalent to
17.      // 10000000 00000000 00000000 00000000
18.      msb = 1 << (INT_SIZE - 1);
19.   
20.      count = 0;
21.   
22.      /* Iterate over each bit */
23.      for(i=0; i<INT_SIZE; i++)
24.      {
25.          /* If leading set bit is found */
26.          if((num << i) & msb)
27.          {
28.              /* Terminate the loop */
29.              break;
30.          }
31.   
32.          count++;
33.      }
34.   
35.      printf("Total number of leading zeros in %d is %d", num, count);
36.   
37.      return 0;
38.  }

Output:


Enter any number: 22
Total number of leading zeros in 22 is 27
 
16)  WAP ROTATE BITS OF A GIVEN NUMBER
1. /**
2.  * C program to rotate bits of a number.
3.  */
4.  
5.  
6. #include <stdio.h>
7.  
8. #define INT_SIZE sizeof(int)        // Size of int in bytes
9. #define INT_BITS INT_SIZE * 8 - 1   // Size of int in bits - 1
10.   
11.   
12.  /* Function declarations */
13.  int rotateLeft(int num, unsigned int rotation);
14.  int rotateRight(int num, unsigned int rotation);
15.   
16.   
17.  int main()
18.  {
19.      int num;
20.      unsigned int rotation;
21.   
22.      /* Input number from user */
23.      printf("Enter a number: ");
24.      scanf("%d", &num);
25.   
26.      /* Input number of rotation */
27.      printf("Enter number of rotation: ");
28.      scanf("%u", &rotation);
29.   
30.   
31.      /* Print rotated number */
32.      printf("%d left rotated %u times = %d\n\n",  num, rotation, rotateLeft(num, rotation));
33.      printf("%d right rotated %u times = %d\n", num, rotation, rotateRight(num, rotation));
34.   
35.   
36.      return 0;
37.  }
38.   
39.   
40.   
41.  /**
42.   * Function to rotate bits of a number to left.
43.   *
44.   * @num         Number to rotate.
45.   * @rotation    Number of times to rotate left.
46.   */
47.  int rotateLeft(int num, unsigned int rotation)
48.  {
49.      int DROPPED_MSB;
50.   
51.      // The effective rotation
52.      rotation %= INT_BITS;
53.   
54.   
55.      // Loop till rotation becomes 0
56.      while(rotation--)
57.      {
58.          // Get MSB of num before it gets dropped
59.          DROPPED_MSB = (num >> INT_BITS) & 1; 
60.   
61.          // Left rotate num by 1 and 
62.          // Set its dropped MSB as new LSB
63.          num = (num << 1) | DROPPED_MSB;
64.      }
65.   
66.      return num;
67.  }
68.   
69.   
70.   
71.  /**
72.   * Function to rotate bits of a number to right.
73.   *
74.   * @num         Number to rotate.
75.   * @rotation    Number of times to rotate right.
76.   */
77.  int rotateRight(int num, unsigned int rotation)
78.  {
79.      int DROPPED_LSB;
80.   
81.      // The effective rotation
82.      rotation %= INT_BITS;
83.   
84.   
85.      // Loop till rotation becomes 0
86.      while(rotation--)
87.      {
88.          // Get LSB of num before it gets dropped
89.          DROPPED_LSB = num & 1;
90.   
91.          // Right shift num by 1 and 
92.          // Clear its MSB
93.          num = (num >> 1) & (~(1 << INT_BITS));
94.   
95.          // Set its dropped LSB as new MSB
96.          num = num | (DROPPED_LSB << INT_BITS);
97.      }
98.   
99.      return num;
100.            }

Output:


 
 
Output
 
Enter a number: -15
Enter number of rotation: 2
 
-15 left rotated 2 times = -57
-15 right rotated 2 times = 2147483644
 
17)  WAP TO CONVERT DECIMAL TO BINARY NUMBER SYSTEM USING BITWISE OPERATOR
1. /**
2.  * C program to convert decimal to binary number system
3.  */
4.  
5. #include <stdio.h>
6. #define INT_SIZE sizeof(int) * 8 /* Size of int in bits */
7.  
8. int main()
9. {
10.      int num, index, i;
11.      int bin[INT_SIZE];
12.   
13.      /* Input number from user */
14.      printf("Enter any number: ");
15.      scanf("%d", &num);
16.   
17.      index = INT_SIZE - 1;
18.   
19.      while(index >= 0)
20.      {
21.          /* Store LSB of num to bin */
22.          bin[index] = num & 1;
23.   
24.          /* Decrement index */
25.          index--;
26.   
27.          /* Right Shift num by 1 */
28.          num >>= 1;
29.      }
30.   
31.      /* Print converted binary */
32.      printf("Converted binary: ");
33.      for(i=0; i<INT_SIZE; i++)
34.      {
35.          printf("%d", bin[i]);
36.      }
37.   
38.      return 0;
39.  }

Output:


Enter any number: 22
Converted binary : 00000000000000000000000000010110
 
18)  WAP TO SWAP TWO NUMBERS USING BITWISE OPERATOR
1. /**
2.  * C program to swap two numbers using bitwise operator
3.  */
4.  
5. #include <stdio.h>
6.  
7. int main()
8. {
9.     int num1, num2;
10.   
11.      /* Input two numbers from user */
12.      printf("Enter any two numbers: ");
13.      scanf("%d%d", &num1, &num2);
14.   
15.      printf("Original value of num1 = %d\n", num1);
16.      printf("Original value of num2 = %d\n", num2);
17.   
18.      /* Swap two numbers */
19.      num1 ^= num2;
20.      num2 ^= num1;
21.      num1 ^= num2;
22.   
23.      printf("Num1 after swapping = %d\n", num1);
24.      printf("Num2 after swapping = %d\n", num2);
25.   
26.      return 0;
27.  }

Output:


Enter any two numbers: 22
65
Original value of num1 = 22
Original value of num2 = 65
Num1 after swapping = 65
Num2 after swapping = 22
 
19)  WAP TO SWAP TWO NUMBERS USING BITWISE OPERATOR
1. /**
2.  * C program to swap two numbers using bitwise operator
3.  */
4.  
5. #include <stdio.h>
6.  
7. int main()
8. {
9.     int num1, num2;
10.   
11.      /* Input two numbers from user */
12.      printf("Enter any two numbers: ");
13.      scanf("%d%d", &num1, &num2);
14.   
15.      printf("Original value of num1 = %d\n", num1);
16.      printf("Original value of num2 = %d\n", num2);
17.   
18.      /* Swap two numbers */
19.      num1 ^= num2;
20.      num2 ^= num1;
21.      num1 ^= num2;
22.   
23.      printf("Num1 after swapping = %d\n", num1);
24.      printf("Num2 after swapping = %d\n", num2);
25.   
26.      return 0;
27.  }

Output:


Enter any two numbers: 22
65
Original value of num1 = 22
Original value of num2 = 65
Num1 after swapping = 65
Num2 after swapping = 22
 
20)  WAP TO CHECK WHETHER ANUMBER IS EVEN OR ODD USING BITWISE OPERATOR
1. /**
2.  * C program to check even or odd number using bitwise operator
3.  */
4.  
5. #include <stdio.h>
6.  
7. int main()
8. {
9.     int num;
10.   
11.      /* Input number from user */
12.      printf("Enter any number: ");
13.      scanf("%d", &num);
14.   
15.      if(num & 1)
16.      {
17.          printf("%d is odd.", num);
18.      }
19.      else
20.      {
21.          printf("%d is even.", num);
22.      }
23.   
24.      return 0;
25.  }

Output:


Enter any number: 15
      15 is odd.

No comments:

Post a Comment

MCQ Question Bank Microsoft PowerPoint