note.nkmk.me

Bitwise operation in Python (AND, OR, XOR, NOT, SHIFT)

Posted: 2021-01-15 / Tags: Python

Python provides the bitwise operators, &(AND), |(OR), ^(XOR), ~(NOT, invert), <<(LEFT SHIFT), >>(RIGHT SHIFT).

This article describes the following contents.

  • Bitwise AND: &
  • Bitwise OR: |
  • Bitwise XOR: ^
  • Bitwise operations with negative integers
  • Bitwise NOT, invert: ~
  • Bit shifts: <<, >>

For more information about converting binary, octal, and hexadecimal numbers and strings using bin(), oct(), hex() and format(), see the following articles.

For Boolean operations on boolean types (True, False) instead of bitwise operations, see the following article. Use and and or instead of & and |.

Sponsored Link

Bitwise AND:: &

Bitwise AND with the & operator:

It also outputs the result of conversion to binary string with bin().

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

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

Bitwise OR: |

Bitwise OR with the | operator:

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

Bitwise XOR: ^

Bitwise XOR with the ^ operator:

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

The inputs and outputs for each bit of AND, OR, and XOR are as follows.

Input 1 Input 2 AND OR XOR
1 1 1 1 0
1 0 0 1 1
0 1 0 1 1
0 0 0 0 0
Sponsored Link

Bitwise operations with negative integers

Bitwise operations on negative integers are handled as if the values were expressed in two's complement.

However, note that if you convert a negative integer to a binary string using bin() or format(), minus sign will be used instead of two's complement format.

If you want to get a string expressed in two's complement representation, take the bitwise AND & of the maximum number of digits required, for example, 0b1111 (= 0xf) for 4bit, 0xff for 8bit, and 0xffff for 16bit.

x = -9

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

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

Bitwise NOT, invert: ~

The ~ operator yields the bitwise inversion. The bitwise inversion of x is defined as -(x+1).

If the input value x is regarded as two's complement and all bits are inverted, it is equivalent to -(x+1).

Converting ~x to a string does not result in a string with the bits of the original value inverted.

x = 9  # 0b1001

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

By performing the AND operation to make a string of two's complement representation, you can obtain a string with the bits inverted.

For example, to get a 4-digit bit inverted string, specify '04b' with format() and pad it with zeros.

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

Bit shifts: <<, >>

Left shift and right shift with 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 expanded and shifted, and the positive and negative signs do not change. Negative values are considered to have infinite 1 on the left side.

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's hard to understand if you think of it as a numerical value, so it's better to think of it as a two's complement string.

Sponsored Link
Share

Related Categories

Related Articles