Python bitwise operators (logical product, logical OR, exclusive OR, inversion, shift)

Money and Business

Python provides the following bitwise operators, which perform logical conjunction, logical disjunction, exclusive disjunction, bitwise inversion, left bit shift, and right bit shift on each bit of a binary integer type int value, respectively.

  • &
  • |
  • ^
  • ~
  • <<
  • >>

In this section, we first explain the following.

  • intersection (AND) : &
  • disjunction (OR) : |
  • EXCLUSIVE-OR operation (XOR) : ^

Next, we will discuss the following.

  • Bitwise operations on negative integers
  • bit flip ( NOT) : ~
  • bit shift : << , >>

For more information on how to write integers in binary, octal, and hexadecimal, and how to convert binary, octal, and hexadecimal numbers and strings using the following functions, see the following article.

  • bin()
  • oct()
  • hex()
  • format()

Also, for logical operations (Boolean operations) on boolean values (true, false) instead of bitwise operations, refer to the following article. Use and,or instead of &,|.

intersection (AND) : &operator

This is an example of a logical AND using the & operator, with the result converted to a string in binary notation by bin().

x = 9   # 0b1001
y = 10  # 0b1010

print(x & y)
print(bin(x & y))
# 8
# 0b1000

disjunction (OR) : |operator

An example of a logical product (OR) using the | operator, with the result converted to a string in binary notation by bin() and output together.

print(x | y)
print(bin(x | y))
# 11
# 0b1011

EXCLUSIVE-OR operation (XOR) : ^operator

Example of a logical product (XOR) using the ^ operator, combined with the result of conversion to a string in binary notation using bin().

print(x ^ y)
print(bin(x ^ y))
# 3
# 0b11

The relationship between the input and output for each bit of logical AND, OR, and XOR is shown in the table below.

Input 1Input 2intersection (AND)disjunction (OR)EXCLUSIVE-OR operation (XOR)
11110
10011
01011
00000

Bitwise operations on negative integers

When a bitwise operation is performed on a negative integer, the value is processed as if it were expressed in two's complement form.

Note, however, that if you convert a negative integer to a binary string using bin() or format(), the absolute value will have a minus sign instead of a two's complement format.

If you want to get a string with two's complement representation, take AND with the maximum number of bit digits required, as shown below.

  • For 4-bit0b1111 (=0xf)
  • For 8-bit0xff
  • For 16-bit0xffff

You can get a string of two's complement representation (each bit is inverted and 1 is added).

x = -9

print(x)
print(bin(x))
# -9
# -0b1001

print(bin(x & 0xff))
print(format(x & 0xffff, 'x'))
# 0b11110111
# fff7

bit flip : ~operator

~example of bit flipping with operators.

Bitwise inversion is not simply the value of each bit inverted. The return value when using this operator is as follows.
~x=-(x+1)

-(x+1)This value is equivalent to considering the input value x as a two's complement form and inverting all the bits.

As mentioned above, in Python, when a negative integer is converted to a binary string using bin(), format(), etc., it is not in two's complement form, but in absolute value with a minus sign. Therefore, converting ~x directly to a string will not result in a string with the bits of the original value inverted.

x = 9  # 0b1001

print(~x)
print(bin(~x))
# -10
# -0b1010

When we perform the AND operation and turn it into a string of two's complement representation, we can see that the bits of the original value are inverted.

In addition, for example, to obtain a bit string that is a 4-digit bit string inverted as is (sign bit omitted), use format() to fill in the zeros for the ANDed value as follows'04b'

print(bin(~x & 0xff))
print(format(~x & 0b1111, '04b'))
# 0b11110110
# 0110

bit shift : << , >>

Examples of left bit shift and right bit shift using bit shift operators.

x = 9  # 0b1001

print(x << 1)
print(bin(x << 1))
# 18
# 0b10010

print(x >> 1)
print(bin(x >> 1))
# 4
# 0b100

For negative values, the sign bit is extended and shifted, and the positive/negative sign remains the same. A negative value is an image of a line of 1s all the way to the left.

x = -9
print(bin(x))
print(bin(x & 0xff))
# -0b1001
# 0b11110111

print(x << 1)
print(bin(x << 1))
print(bin((x << 1) & 0xff))
# -18
# -0b10010
# 0b11101110

print(x >> 1)
print(bin(x >> 1))
print(bin((x >> 1) & 0xff))
# -5
# -0b101
# 0b11111011

It is better to think in terms of strings of two's complement expressions, since thinking in terms of numbers is not clear.

Copied title and URL