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

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 1 if both bits are 1, otherwise it returns 0.

|

bitwise inclusive OR; compares two bits and generates the result 1 if either or both bits are 1, otherwise it returns 0.

^

bitwise exclusive OR; compares two bits and generates the result 1 if the bits are complementary, otherwise it returns 0.

~

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 0 to the left most bit, otherwise sign extends.

<<

bitwise shift left; moves the bits to the left, it discards the far left bit and assigns 0 to the right most bit.

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.