Bitwise operations are operations that directly manipulate bits (the smallest units of data in a computer's memory) within binary representations of data. These operations are performed at the bit level, enabling you to control individual bits within binary numbers, typically integers, at a very low level.
Common bitwise operators include:
AND (&) - Get Bit
If both bits are 1, the resulting bit is 1. Otherwise, the resulting bit is 0.
a: 1010 (decimal: 10) b: 1100 (decimal: 12) -------------- a & b: 1000 (decimal: 8)
Bitwise AND operations are commonly used for various purposes, such as:
Masking: Using bitwise AND with a mask to manipulate specific bits while keeping others unchanged. For example, to clear certain bits or extract specific information from a bit pattern.
Suppose you intend to extract the third and fourth bits from the binary number b1010. To accomplish this, you can employ the bitwise AND operation with the binary value b1100 as follows: b1010 & b1100 = b1000.
Checking odd or even: Checking the least significant bit (LSB). For instance, (number & 1) can determine if a number is odd or even. If the result is 1, the number is odd; if it's 0, the number is even.
Isolating specific bits: By using bitwise AND with a mask that has 1s in positions where you want to preserve bits and 0s elsewhere, you can isolate specific bits.
OR (|) - Set Bit
If at least one of the bits is 1, the resulting bit is 1.If both bits are 0, the resulting bit is 0.
a: 1010 (decimal: 10) b: 1100 (decimal: 12) -------------- a | b: 1110 (decimal: 14)
Bitwise OR operations are commonly used for various purposes, such as:
Combining or setting specific bits: By using bitwise OR with a mask that has 1s in positions where you want to set bits and 0s elsewhere, you can combine specific bits or set certain bits to 1.
For instance, if you wish to set the third bit from the left in the binary number b1010 to 1 while maintaining the other bits unchanged, you can achieve this by performing a bitwise OR operation with the binary value b0100. The operation would look like this: b1010 | b0100 = b1110.
Flag settings: In programming, bitwise OR is often used to set or combine flags in a flag-based system. Each flag corresponds to a specific bit in an integer value.
Conditional setting: It can be used to conditionally set bits based on some conditions.
XOR (^) - Triggle Bit
If the two bits being compared are different (one is 0 and the other is 1), the resulting bit is set to 1.If the two bits are the same (both 0 or both 1), the resulting bit is set to 0.
a: 1010 (decimal: 10) b: 1100 (decimal: 12) -------------- a ^ b: 0110 (decimal: 6)
Properties and Applications of XOR:
Complement and Flip: XOR can be used to toggle or flip bits. For instance, XOR a value with a mask containing all 1s will complement the bits in the value.
Swapping without Temporary Variable: XOR can be used to swap the values of two variables without using a temporary variable. This is possible because XOR a value with itself cancels out, allowing for efficient swapping.
Error Checking and Cryptography: XOR operations are used in error detection and correction algorithms, as well as in encryption and decryption in cryptography.
NOT (~) - Invert Bit
Performs a bitwise NOT operation, inverting each bit of the operand.
Left Shift (<<)
Shifts the bits of a number to the left by a certain number of positions.
Right Shift (>>)
Shifts the bits of a number to the right by a certain number of positions.
unsigned int a = 5; // Binary representation: 0101 unsigned int b = 3; // Binary representation: 0011 // Bitwise AND operation: 0101 & 0011 = 0001 (result = 1)
unsigned int result_and = a & b;
// Bitwise OR operation: 0101 | 0011 = 0111 (result = 7) unsigned int result_or = a | b;
// Bitwise XOR operation: 0101 ^ 0011 = 0110 (result = 6) unsigned int result_xor = a ^ b;
// Bitwise NOT operation for 'a': ~0101 = 1010 (result = 10) unsigned int result_not_a = ~a;
// Left shift 'a' by 1 bit: 0101 << 1 = 1010 (result = 10) unsigned int left_shift = a << 1;
// Right shift 'a' by 1 bit: 0101 >> 1 = 0010 (result = 2) unsigned int right_shift = a >> 1;
Bitwise operations are fundamental in understanding low-level programming, data manipulation, and certain optimization techniques in various domains within computer science and engineering.