Bitwise Operators
Introduction
Bitwise operations alter binary strings at the bit level. These operations are incredibly basic and are directly supported by the processor. These few operations are necessary in working with device drivers, lowlevel graphics, cryptography, and network communications. This section provides useful knowledge and examples of Python's bitwise operators.
Syntax

x << y # Bitwise Left Shift

x >> y # Bitwise Right Shift

x & y # Bitwise AND

x  y # Bitwise OR

~ x # Bitwise NOT

x ^ y # Bitwise XOR
Bitwise AND
The &
operator will perform a binary AND, where a bit is copied if it exists in both operands. That means:
Bitwise Left Shift
The <<
operator will perform a bitwise "left shift," where the left operand's value is moved left by the number of bits given by the right operand.
Performing a left bit shift of 1
is equivalent to multiplication by 2
:
Performing a left bit shift of n
is equivalent to multiplication by 2**n
:
Bitwise NOT
The ~
operator will flip all of the bits in the number. Since computers use signed number representations — most notably, the two's complement notation to encode negative binary numbers where negative numbers are written with a leading one (1) instead of a leading zero (0).
This means that if you were using 8 bits to represent your two'scomplement numbers, you would treat patterns from 0000 0000
to 0111 1111
to represent numbers from 0 to 127 and reserve 1xxx xxxx
to represent negative numbers.
Eightbit two'scomplement numbers
Bits  Unsigned Value  Two'scomplement Value 

0000 0000  0  0 
0000 0001  1  1 
0000 0010  2  2 
0111 1110  126  126 
0111 1111  127  127 
1000 0000  128  128 
1000 0001  129  127 
1000 0010  130  126 
1111 1110  254  2 
1111 1111  255  1 
In essence, this means that whereas 1010 0110
has an unsigned value of 166 (arrived at by adding (128 * 1) + (64 * 0) + (32 * 1) + (16 * 0) + (8 * 0) + (4 * 1) + (2 * 1) + (1 * 0)
), it has a two'scomplement value of 90 (arrived at by adding (128 * 1)  (64 * 0)  (32 * 1)  (16 * 0)  (8 * 0)  (4 * 1)  (2 * 1)  (1 * 0)
, and complementing the value).
In this way, negative numbers range down to 128 (1000 0000
). Zero (0) is represented as 0000 0000
, and minus one (1) as 1111 1111
.
In general, though, this means ~n = n  1
.
Note, the overall effect of this operation when applied to positive numbers can be summarized:
~n > n+1
And then, when applied to negative numbers, the corresponding effect is:
~n > n1
The following examples illustrate this last rule...
Bitwise OR
The 
operator will perform a binary "or," where a bit is copied if it exists in either operand. That means:
Bitwise Right Shift
The >>
operator will perform a bitwise "right shift," where the left operand's value is moved right by the number of bits given by the right operand.
Performing a right bit shift of 1
is equivalent to integer division by 2
:
Performing a right bit shift of n
is equivalent to integer division by 2**n
:
Bitwise XOR (Exclusive OR)
The ^
operator will perform a binary XOR in which a binary 1
is copied if and only if it is the value of exactly one operand. Another way of stating this is that the result is 1
only if the operands are different. Examples include:
Inplace Operations
All of the Bitwise operators (except ~
) have their own in place versions