Bitwise Operators
Go Up to Binary Operators Index
Syntax
AND-expression & equality-expressionexclusive-OR-expression ^ AND-expressioninclusive-OR-expression exclusive-OR-expression~ cast-expressionshift-expression << additive-expressionshift-expression >> additive-expression
Remarks
Use the bitwise operators to modify the individual bits rather than the number.
| Operator | What it does |
|---|---|
|
& |
bitwise AND; compares two bits and generates the result |
|
| |
bitwise inclusive OR; compares two bits and generates the result |
|
^ |
bitwise exclusive OR; compares two bits and generates the result |
|
~ |
bitwise complement; inverts each bit. ~ is used to create destructors. |
|
>> |
bitwise shift right; moves the bits to the right, discards the far right bit and if unsigned assigns |
|
<< |
bitwise shift left; moves the bits to the left, it discards the far left bit and assigns |
Both operands in a bitwise expression must be of an integral type.
|
A |
B |
A & B |
A ^ B |
A | B |
|
0 |
0 |
0 |
0 |
0 |
|
0 |
1 |
0 |
1 |
1 |
|
1 |
0 |
0 |
1 |
1 |
|
1 |
1 |
1 |
0 |
1 |
Note: &, >>, << are context sensitive. & can also be the pointer reference operator.
Note: >> is often overloaded to be the input operator in I/O expressions. << is often overloaded to be the output operator in I/O expressions.