Bits manipulation (Important tactics) in C++

Let’s first recall the about bits and the bitwise operator is short.
Bit is a binary digit. It is the smallest unit of data that is understandable by the computer. In can have only one of the two values 0 (denotes OFF) and 1 (denotes ON).
Bitwise operators are the operators that work a bit level in the program.
These operators are used to manipulate bits in the program.
In C, we have 6 bitwise operators −
Bitwise AND (&)
Bitwise AND (&)
Bitwise OR (OR)
Bitwise OR (OR)
Bitwise XOR (XOR)
Bitwise XOR (XOR)
Bitwise left Shift (<<)/p>
Bitwise left Shift (<<)/p>
Bitwise right Shift (>>)
Bitwise right Shift (>>)
Bitwise not (~)
Bitwise not (~)
https://www.tutorialspoint.com/cprogramming/c_bitwise_operators.htm
Now, let’s learn some important tactics i.e. things that can be helpful if you work with bits.
We can swap two values using the bitwise XOR operator. The implementation is −
 Live Demo
#include <stdio.h>
int main(){
   int x = 41;
   int y = 90;
   printf("Values before swapping! \n");
   printf("x = %d \t", x);
   printf("y = %d \n", y);
   x = x ^ y;
   y = y ^ x;
   x = x ^ y;
   printf("Values after swapping! \n");
   printf("x = %d \t", x);
   printf("y = %d \n", y);
   return 0;
}
Values before swapping!
x = 41 y = 90
Values before swapping!
x = 90 y = 41
For any integer value, we can find the most significant bit is an effective way. This is done using or operator along with a bitwise shift operator. This method can find the MSB in o(1) time complexity.
The size of the integer should be predefined to create the program.
Program to find MSB of 32-bit integer −
 Live Demo
#include <stdio.h>
int findMSB(int x){
   x |= x>>1;
   x |= x>>2;
   x |= x>>4;
   x |= x>>8;
   x |= x>>16;
   x = x+1;
   return(x >> 1);
}
int main(){
   int x = 49;
   printf("The number is %d\n", x);
   int msb = findMSB(x);
   printf("MSB of the number is %d\n", msb);
}
The number is 49
MSB of the number is 32
If we observe the XOR of 0 to n carefully we can derive a general pattern. Which is illustrated here −
 Live Demo
#include <stdio.h>
// Direct XOR of all numbers from 1 to n
int findXORuptoN(int n){
   switch( n%4){
      case 0: return n;
      case 1: return 1;
      break;
      case 2: return n+1;
      break;
      case 3: return 0;
      break;
      default: break;
   }
}
int main(){
   int n = 9870;
   int xorupton = findXORuptoN(n);
   printf("XOR of all number up to %d is %d\n", n, xorupton);
}
XOR of all number up to 9870 is 9871
Using the bitwise shifting operators, we can easily do the work and it will require less time.
 Live Demo
#include <stdio.h>
int countValues(int n){
   int unset=0;
   while (n){
      if ((n & 1) == 0)
         unset++;
      n=n>>1;
   }
   return (1<<unset);
}
int main(){
   int n = 32;
   printf("%d", countValues(n));
}
32
There are inbuilt methods to find the number of leading and trailing zeroes of an integer, thanks to bit manipulation.
* It is a GCC inbuilt function
 Live Demo
#include <stdio.h>
int main(){
   int n = 32;
   printf("The integer value is %d\n", n);
   printf("Number of leading zeros is %d\n", __builtin_clz(n));
   printf("Number of trailing zeros is %d\n",__builtin_clz(n));
}
The integer value is 32
Number of leading zeros is 26
Number of trailing zeros is 26
To check, if a number is a power of 2, is made easy using a bitwise operator.
 Live Demo
#include <stdio.h>
int isPowerof2(int n){
   return n && (!(n&(n-1)));
}
int main(){
   int n = 22;
   if(isPowerof2(n))
      printf("%d is a power of 2", n);
   else
      printf("%d is not a power of 2", n);
}
22 is not a power of 2
This can be done using the fact that if there are more than 1 element the XOR of all subsets is always 0 and the number otherwise.
 Live Demo
#include <stdio.h>
int findsubsetXOR (int set[], int size){
   if (size == 1){
      return set[size - 1];
   }
   else
      return 0;
}
int main (){
   int set[] = { 45, 12 };
   int size = sizeof (set) / sizeof (set[0]);
   printf ("The XOR of all subsets of set of size %d is %d\n", size,
   findsubsetXOR (set, size));
   int set2[] = { 65 };
   size = sizeof (set2) / sizeof (set2[0]);
   printf ("The XOR of all subsets of set of size %d is %d\n", size,
   findsubsetXOR (set2, size));
}
The XOR of all subsets of set of size 2 is 0
The XOR of all subsets of set of size 1 is 65
auto keyword in C is employed to do the task.
 Live Demo
#include <stdio.h>
int main (){
   auto integer = 0b0110110;
   printf("The integer conversion of binary number '0110110' is %d", integer);
}
The integer conversion of binary number '0110110' is 54
We can flip all the bits of a number by subtracting it from a number whose all bits are set.
Number = 0110100
The number will all bits set = 1111111
Subtraction -> 1111111 - 0110100 = 1001011 (number with flipped bits)
 Live Demo
#include <stdio.h>
int main (){
   int number = 23;
   int n = number;
   n |= n>>1;
   n |= n>>2;
   n |= n>>4;
   n |= n>>8;
   n |= n>>16;
   printf("The number is %d\n", number);
   printf("Number with reversed bits %d\n", n-number);
}
The number is 23
Number with reversed bits 8
Using the bitwise XOR operation, we can find if the bits of a number are in alternate patterns or not. The below code shows how to −
 Live Demo
#include <stdio.h>
int checkbitpattern(int n){
   int result = n^(n>>1);
   if(((n+1)&n) == 0)
      return 1;
   else
      return 0;
}
int main (){
   int number = 4;
   if(checkbitpattern == 1){
      printf("Bits of %d are in alternate pattern", number);
   }
   else
      printf("Bits of %d are not in alternate pattern", number);
}
Bits of 4 are not in alternate pattern