top of page

Bitwise operators

Before looking at Bitwise operators, we need to understand first Logical values vs. single bits.

Logical operators take their arguments as a whole regardless of how many bits they contain. The operators are aware only of the value: zero (when all the bits are reset) means False; not zero (when at least one bit is set) means True.

The result of their operations is one of these values: False or True. This means that this snippet will assign the value True to the j variable if i is not zero; otherwise, it will be False.

i = 1

j = not not i

 

There are four operators that allow you to manipulate single bits of data. They are called bitwise operators.

They cover all the operations we mentioned before in the logical context, and one additional operator. This is the xor (as in exclusive or) operator, and is denoted as ^ (caret). Here are all of them:

  • & (ampersand) - bitwise conjunction;

  • | (bar) - bitwise disjunction;

  • ~ (tilde) - bitwise negation;

  • ^ (caret) - bitwise exclusive or (xor).

 
 

Let's make it easier:

  • & requires exactly two 1s to provide 1 as the result;

  • | requires at least one 1 to provide 1 as the result;

  • ^ requires exactly one 1 to provide 1 as the result.

bottom of page