C++ Bitwise Operators: Rules
In C++, bitwise operators are used to manipulate the binary digits of integer data. Here are the common bitwise operators and their operations in C++.
- Bitwise AND (&): Performs a logical AND operation on each pair of corresponding bits in the two numbers, resulting in a 1 only if both bits are 1, otherwise 0.
- Bitwise OR (|): Perform a bitwise OR operation on each binary digit of two numbers, resulting in a 1 for each corresponding digit if either of the operands has a 1, otherwise it will be 0.
- Bitwise XOR (^): Perform an XOR operation on each binary digit of two numbers, resulting in 0 if the corresponding bits are the same, and 1 if they are different.
- Bitwise NOT (~): Performs a negation operation on each binary digit of a number, turning 1s into 0s and 0s into 1s.
- Left shift (<<): Moving all the binary digits of a number to the left by a specified number of positions, filling the lower positions with zeros.
- Right shift (>>) : Moving all the binary digits of a number to the right by a specified number of places, filling the high bits with the sign bit (for signed numbers) or with 0 (for unsigned numbers).
These bitwise operators are often used for efficient bit manipulation, such as bitwise masking, clearing, and setting operations.