Bitwise Operators
Go Up to Binary Operators Index
Syntax
AND-expression & equality-expression exclusive-OR-expression ^ AND-expression inclusive-OR-expression exclusive-OR-expression ~ cast-expression shift-expression << additive-expression shift-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.