# Bitwise operators

### Contents

Search other websites:

##### 1. Assignment of bitwise operators. List

The Python language supports working with binary digits (bits) of integer values, where each bit of a number is considered separately. To ensure this, Python uses the so-called bit or bitwise operators, which implement well-known bitwise operations. Support for bit operators is also available in other programming languages.

In bit operators (operations), each operand is considered as a sequence of binary digits (bits), which take the value 0 or 1 (binary number system). Known operations can be performed on these digits (logical AND, logical OR, etc.).

The list of Python bitwise operators in descending order of priority is as follows:

• ~ – bit operator NOT (inversion, highest priority);
• <<, >> – left shift or right shift operators by a specified number of bits;
• & – bitwise operator AND;
• ^ – bitwise exclusive OR (XOR);
• | – bitwise OR.

### ⇑

##### 2. Bitwise operator ~ (NOT). Example

In a bit operator (operation) ~ inversion, the value of any bit of a number is reversed. The value of bit 0 is set to 1, and the value of 1 is set to 0. That is, a positive number becomes negative with an offset of -1. Also, a negative number becomes positive with an offset of -1.

Example.

```# Bitwise operator ~ NOT - inversion
a = 0b1001 # a = 9 in decimal system
b = ~a # b = -10 - decimal system
c = bin(b) # c = -0b1010 - decimal system

a = -0b1001 # a = -9 in decimal system
b = ~a # b = 8 - decimal system
c = bin(b) # c = 0b1000 - binary system

a = 0b1111 # a = 15
b = ~a # b = -16
c = bin(b) # c = -0b10000

a = -0b1111 # a = -15
b = ~a # b = 14
c = bin(b) # c = 0b1110```

### ⇑

##### 3. Left shift operators <<, right >>. Example

The left shift << and right shift >> operators shift each bit by one or more positions left or right. The general form of the operators is as follows

```op1 << op2
op1 >> op2```

where op1, op2 – operands. The operand can be a number, an integer variable, or an expression that returns an integer result. Figure 1 shows the operation of the left shift << and right shift >> operators. When calculating the y value, the x value is shifted 1 position to the left (case a) or to the right (case b). Accordingly, the result of y is multiplied by 2 or divided by 2.

Figure 1. The operations: а) left shift << (multiplication by 2); b) right shift >> (integer division by 2)

If you need to multiply a number by 16, then you need to shift this number by 4 bits to the left. If you need to divide the number by 8, then you need to shift this number by 3 bits to the right. The speed of performing shift operations is higher in comparison with the operations of multiplication and division by multiples of 2 to the power of N (N is the number of shifted bits).

Example.

```# Left shift operations << and right shift operations >>
x = 5 # left shift by 3 digits, multiplication by 2 ** 3 = 8
y = x << 3 # y = x*2**3 = 40
print('x = ', x)
print('y = x<<3 = ', y)

x = 25
y = x >> 2 # y = 6
print('x = ', x)
print('y = x>>2 = ', y)

# For negative numbers
x = -10
y = x << 1 # y = -20
print('x = ', x)
print('y = x<<1 = ', y)

x = -100
y = x >> 3 # y = -13
print('x = ', x)
print('y = x>>3 = ', y)```

The result of the program

```x = 5
y = x<<3 = 40
x = 25
y = x>>2 = 6
x = -10
y = x<<1 = -20
x = -100
y = x>>3 = -13```

### ⇑

##### 4. Bitwise operator & (AND). Example

The bitwise operator & (AND) is binary and executes the bitwise AND for each pair of bits of the operands that are placed to the left and right of the sign of the & operator. The general form of the operator is as follows

`op1 & op2`

here op1, op2 – operands. The operands can be numbers, integer type variables, or expressions that return an integer result. Each integer operand is considered as a set of bits, on any of which the bitwise operation “AND” is performed.

Figure 2 shows the operation of the bitwise operation “AND”.

Figure 2. Bitwise operator & “AND”

As can be seen from the figure, the bit at position 0 of the first operand (x) is calculated with the bit at position 0 of the second operand (y), respectively, the bit at position 1 of the first operand (x) is calculated with the bit at position 1 of the second operand (y), etc. With such calculations, the resulting value of any bit is determined by the following formulas:

```0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1```

Example.

```# Bitwise operation & (AND)
x = 37
y = 58
z = x & y # z = 32
print('x = ', x)
print('y = ', y)
print('z = ', z)```

The result of the program

```x = 37
y = 58
z = 32```

### ⇑

##### 5. Bitwise operator ^ (exclusive OR, XOR). Example

The exclusive OR bitwise operator is indicated by ^ and performs the modulo 2 addition operation for any bit of the operands. The general form of the operator is as follows

`op1 ^ op2`

here op1, op2 – integer operands.

The exclusive OR operator (XOR) operates with binary digits. Each operand is considered as a sequence of bits. The result of a bitwise exclusive OR is determined by the following formulas

```0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 0 = 1
1 ^ 1 = 0```

Figure 3 shows an example of a bit exclusive OR for two operands.

Figure 3. Bitwise operator | “exclusive OR”

Example.

```# Bitwise Operator - Exclusive OR (XOR)
x = 37
y = 58
z = x ^ y # z = 31
print('x = ', x)
print('y = ', y)
print('z = ', z)```

The result of the program

```x = 37
y = 58
z = 31```

### ⇑

##### 6. Bitwise operator | OR. Example

The bitwise operator OR is binary and is denoted by the symbol |. The operator implements bitwise logical addition according to the pattern of the operators & and ^ (see sections 4, 5).

The general form of a bit operator | is as follows

`op1 | op2`

here op1, op2 – operands, which can be variables or integers.

For two operands op1, op2, a bitwise OR is performed in accordance with the following rules

```0 | 0 = 0
0 | 1 = 1
1 | 0 = 1
1 | 1 = 1```

Figure 4 demonstrates the operation of the bitwise OR operator using two arbitrary operands as an example

Figure 4. Bitwise operator OR

Example.

```# Bitwise OR
x = 37
y = 58
z = x | y # z = 63
print('x = ', x)
print('y = ', y)
print('z = ', z)```

The result of the program

```x = 37
y = 58
z = 63```

### ⇑

##### 7. Examples of using bitwise operators

Example 1. Pull out 4, 5, 6 bits from the number and determine their integer value.

```# Pull out 4,5,6 bits from the integer number
# input the integer number
number = int(input('Input number: '))

# filter for 4,5,6 bits
number &= 0b1110000

# shift 4 digits to the right
number >>= 4
print('number = ', number)```

The result of the program

```Input number: 95
number = 5```

Example 2. Multiply the values of two numbers. In the first number, take the bits that are located at positions 0-5. In the second number, take the bits that are located at positions 0-7.

```# Multiply 0-5 bits of the first number by 0-7 bits of the second number
x = int(input('x = '))
y = int(input('y = '))

# filter for 0-5 bits
x &= 0b11111

# filter for 0-7 bits
y &= 0b1111111

# multiply
z = x*y

print('x = ', x)
print('y = ', y)
print('z = ', z)```

The result of the program

```x = 234234253
y = 322797987
x = 13
y = 35
z = 455```