0% completed
Bitwise operators perform operations on the individual bits of integer values. They are used for low-level programming tasks, such as controlling hardware, setting flags, and optimizing performance. Bitwise operators work directly on the binary (bit) representations of numbers.
Operator | Description | Example |
---|---|---|
& | Bitwise AND: Sets each bit to 1 if both bits are 1 | a & b |
| | Bitwise OR: Sets each bit to 1 if one of the bits is 1 | a | b |
^ | Bitwise XOR: Sets each bit to 1 if only one of the bits is 1 | a ^ b |
~ | Bitwise NOT: Inverts all the bits | ~a |
Below is an example that demonstrates how bitwise operators work using two integer values.
Explanation:
&
):
0101 & 0011
results in 0001
(only the rightmost bit is 1 in both).1
|
):
0101 | 0011
results in 0111
(if either bit is 1, the result is 1).7
^
):
0101 ^ 0011
results in 0110
(bit is set to 1 only if the bits differ).6
This example demonstrates how bitwise operators manipulate individual bits within integers. Experiment with these operators to understand how changing bits affects the outcome of expressions.
.....
.....
.....