The Logical Bitwise Operators
The logical bitwise operators are analogous to the regular logical operators, except they apply to a value on a bit-by-bit basis rather than to the whole. For example, consider the regular negation operator (!
) and the bitwise negation (or complement) operator (~
). The !
operator converts a true
(or nonzero) value to false
and a false
value to true
. The ~
operator converts each individual bit to its opposite (1
to 0
and 0
to 1
). For example, consider the unsigned char
value of 3
:
unsigned char x = 3;
The expression !x
has the value 0
. To see the value of ~x
, you write it in binary form: 00000011
. Then you convert each 0
to 1
and each 1
to 0
. This produces the value 11111100
, which in base 10 is the value 252
. (
Get C++ Primer Plus now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.