1️⃣

Shortcut Tricks in Bit Manipulation

 
Shortcut Tricks play a major role in solving complex problems in simple, fast and efficient way.
They can be useful and efficient for working with binary representations of integers in various programming tasks.
 
Here are some popular bit manipulation shortcut tricks in C++:
1. Setting a bit using the OR operator
To set a bit at a specific position in an integer variable x, you can use the OR operator (|) with a bit mask that has a 1 in the desired position and zeroes everywhere else. Here's an example code that sets the third bit of x to 1:
 
int x = 10; // Binary representation: 1010 int pos = 2; // Position of the bit to be set x |= (1 << pos); // Set the bit at position pos cout << x; // Output: 14 (Binary representation: 1110)
 
In this code, the integer variable x is initialized with the decimal value 10, which has a binary representation of 1010.
The integer variable pos specifies the position of the bit to be set, starting from zero indexing.
The bit mask (1 << pos) has a value of 2^pos, which has a single 1 bit in the position corresponding to pos and zeroes everywhere else. In this example, the bit mask is equal to 100 in binary notation, which is equivalent to the decimal value 4.
The OR operator between x and the bit mask (1 << pos) sets the bit at position pos of x to 1, resulting in a binary value of 1110, which is equivalent to the decimal value 14.
 
2. Clearing a bit using the AND operator with complement
To clear a bit at a specific position in an integer variable x, you can use the AND operator (&) with a bit mask that has zeroes in the desired position and ones everywhere else. Here's an example code that clears the fourth bit of x:
 
int x = 25; // Binary representation: 11001 int pos = 3; // Position of the bit to be cleared x &= ~(1 << pos); // Clear the bit at position pos cout << x; // Output: 17 (Binary representation: 10001)
 
In this code, the integer variable x is initialized with the decimal value 25, which has a binary representation of 11001.
The integer variable pos specifies the position of the bit to be cleared, starting from zero indexing.
The bit mask ~(1 << pos) has a value of bitwise NOT of 2^pos, which has a single 0 bit in the position corresponding to pos and ones everywhere else. In this example, the bit mask is equal to 1110111 in binary notation, which is equivalent to the decimal value -9.
The AND operator between x and the bit mask ~(1 << pos) clears the bit at position pos of x, resulting in a binary value of 10001, which is equivalent to the decimal value 17.
 
3. Toggling a bit using the XOR operator
To toggle (i.e. flip) a bit at a specific position in an integer variable x, you can use the XOR operator (^) with a bit mask that has a single 1 in the desired position and zeroes everywhere else. Here's an example code that toggles the fifth bit of x:
 
int x = 27; // Binary representation: 11011 int pos = 4; // Position of the bit to be toggled x ^= (1 << pos); // Toggle the bit at position pos cout << x; // Output: 11 (Binary representation: 1011)
 
In this code, the integer variable x is initialized with the decimal value 27, which has a binary representation of 11011.
The integer variable pos specifies the position of the bit to be toggled, starting from zero indexing.
The bit mask (1 << pos) has a value of 2^pos, which has a single 1 bit in the position corresponding to pos and zeroes everywhere else. In this example, the bit mask is equal to 10000 in binary notation, which is equivalent to the decimal value 16.
The XOR operator between x and the bit mask (1 << pos) toggles the bit at position pos of x, resulting in a binary value of 1011, which is equivalent to the decimal value 11.
 
4. Testing a bit using the AND operator
To test whether a bit is set at a specific position in an integer variable x, you can use the AND operator (&) with a bit mask that has a single 1 in the desired position and zeroes everywhere else. If the result is non-zero, the bit at that position is set; otherwise, it is not set. Here's an example code that tests whether the second bit of x is set:
 
int x = 6; // Binary representation: 0110 int pos = 1; // Position of the bit to be tested if (x & (1 << pos)) { // Test the bit at position pos cout << "Bit " << pos << " is set"; } else { cout << "Bit " << pos << " is not set"; } // Output: Bit 1 is set
 
In this code, the integer variable x is initialized with the decimal value 6, which has a binary representation of 0110.
The integer variable pos specifies the position of the bit to be tested, starting from zero indexing.
The bit mask (1 << pos) has a value of 2^pos, which has a single 1 bit in the position corresponding to pos and zeroes everywhere else. In this example, the bit mask is equal to 0010 in binary notation, which is equivalent to the decimal value 2.
The AND operator between x and the bit mask (1 << pos) tests the bit at position pos of x. Since the second bit is set in this example, the result is non-zero, and the code prints "Bit 1 is set".
 
5. Swapping two integers using XOR
To swap the values of two integer variables a and b without using a third variable, you can use the XOR operator (^) to perform a bitwise swap. The idea is to XOR a with b, store the result in a, then XOR the new value of a with b again to restore the original value of b. Here's an example code that swaps the values of a and b:
 
int a = 5; // Binary representation: 0101 int b = 9; // Binary representation: 1001 a ^= b; // XOR a and b to get new value of a b ^= a; // XOR new value of a with b to get new value of b a ^= b; // XOR new value of b with new value of a to restore original value of a cout << "a = " << a << ", b = " << b; // Output: a = 9, b = 5
 
In this code, the integer variables a and b are initialized with the decimal values 5 and 9, which have binary representations of 0101 and 1001, respectively.
The first XOR operation between a and b produces a new value for a that contains both the bits that are set in a and b, which is 1100 in binary notation, equivalent to the decimal value 12.
The second XOR operation between the new value of a and b produces a new value for b that contains the bits that were originally set in a, which is 0101 in binary notation, equivalent to the decimal value 5.
The third XOR operation between the new value of b and the original value of a restores the original value of a, which is 0101 in binary notation, equivalent to the decimal value 9.
After the bitwise swap, the values of a and b have been swapped, as evidenced by the output a = 9, b = 5.
 
 
PRACTICE PROBLEMS :
  1. Reverse Bits
  1. Hamming Distance