Bitwise operators are operators in computer programming that manipulate individual bits of binary numbers. They operate on the binary representation of numbers, treating them as sequences of zeros and ones.
In computer programming, where every operation boils down to ones and zeros, bitwise operators play a crucial role. These operators perform various logical and arithmetic operations at the bit level, allowing programmers to manipulate and extract specific bits, set or clear certain flags, and perform other bitwise operations.
There are six primary bitwise operators you need to know: AND (&), OR (|), XOR (^), NOT (~), left shift (<<), and right shift (>>). Let’s take a closer look at each of these operators and their applications:
This operator performs a bitwise AND operation on the corresponding bits of two numbers, returning a new number with ones only where both input bits are ones. It is commonly used for masking and clearing specific bits in a number.
X | Y | X&Y |
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
#include <iostream>int main() {// Example valuesint num1 = 12; // Binary: 1100int num2 = 7; // Binary: 0111// Perform bitwise AND operationint result = num1 & num2;// Output the resultstd::cout << "Result of bitwise AND: " << result << std::endl;return 0;}
The OR operator performs a bitwise OR operation, resulting in a new number with ones in positions where either of the input bits is one. It is often used for setting specific bits to one.
X | Y | X&Y |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
#include <iostream>int main() {// Example valuesint num1 = 12; // Binary: 1100int num2 = 7; // Binary: 0111// Perform bitwise OR operationint result = num1 | num2;// Output the resultstd::cout << "Result of bitwise OR: " << result << std::endl;return 0;}
The XOR operator, also known as the exclusive OR operator, returns a new number with ones where the input bits differ. It can be employed to toggle or swap bits without needing temporary variables.
X | Y | X&Y |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
#include <iostream>int main() {// Example valuesint num1 = 12; // Binary: 1100int num2 = 7; // Binary: 0111// Perform bitwise XOR operationint result = num1 ^ num2;// Output the resultstd::cout << "Result of bitwise XOR: " << result << std::endl;return 0;}
The NOT operator is a unary operator that flips the bits of a number, transforming ones into zeros and zeros into ones. It is typically used to create the one’s complement of a number.
X | Y |
0 | 1 |
1 | 0 |
#include <iostream>int main() {// Example valuesint num1 = 12; // Binary: 1100int num2 = 7; // Binary: 0111// Perform bitwise NOT operationint notResult1 = ~num1;int notResult2 = ~num2;// Output the resultsstd::cout << "Result of bitwise NOT (num1): " << notResult1 << std::endl;std::cout << "Result of bitwise NOT (num2): " << notResult2 << std::endl;return 0;}
This operator shifts the bits of a number to the left by a specified number of positions, effectively multiplying the number by 2. It finds applications in efficient multiplication and division by powers of two.
#include <iostream>int main() {// Example valueint num1 = 12; // Binary: 1100// Perform left shift operationint leftShiftResult = num1 << 2;// Output the resultstd::cout << "Result of left shift: " << leftShiftResult << std::endl;return 0;}
The right shift operator shifts the bits of a number to the right, effectively dividing the number by 2. It is commonly used for efficient division and extracting specific bits.
#include <iostream>int main() {// Example valueint num2 = 7; // Binary: 0111// Perform right shift operationint rightShiftResult = num2 >> 1;// Output the resultstd::cout << "Result of right shift: " << rightShiftResult << std::endl;return 0;}
Bitwise operators unlock the potential to manipulate binary data at the bit level, providing efficient solutions to various programming challenges. By understanding and utilizing these operators, programmers can harness the power of binary logic and achieve elegant and optimized solutions in their code.
Free Resources