Python Basics: 09 bitwise operators

Introducing to bitwise :))

Suwes Muhammad Hafiz
6 min readApr 6, 2022

This time, we will learn bitwise operators in the python programming language.

Bitwise is an operator that functions to handle logical processes in binary form, bitwise operators consist of two numbers, namely 0 and 1. In python, we can manipulate bitwise to handle logical processes using the logical operators we have learned before, such as NOT, OR, AND and XOR.

Compared to java, doing bitwise operations in python can be done easily and practically.

Bitwise is actually the format of a value that is converted into a binary number in bytes, usually consisting of a combination of 8 binary numbers. Bitwise, we can also say as an operation on each bit. Let’s see how a value is converted into bit format.

For example, we have an integer value 2, so we will have 8-bit binary number 2 to bit = 00000010

NOTE: Every binary number has an index that counts from zero.

For example :

00000010^^^^^^^^76543210

and every value entered, the binary will calculate using 2n
n is the index position.

int 2 = 00000010
index = 76543210

so 2¹ = 2, so it will produce the integer number 2.

Suppose we don’t know the index of the following bit = 00001001

int? = 000001001
index = 76543210

so the index is at = 8 and 2⁰ = 1, then add up = 9
then the integer number in the bit 00001001 is 9.

That’s all for the recognition of binary or its bits. Next, we will return to the main topic, namely bitwise operators.

The following logical operators are used in the bitwise operators in python
NOT using a sign (~)
OR using a sign (|)
AND using a sign (&)
XOR using a sign (^)

c for dummies — reading bits
c for dummies — XOR on bits

This flag can also be replaced with the usual logical operators in python

how to use bitwise operators.
For example, we have two values
2 and 1 and we OR will become 2 | 1. , so we have 2 binary numbers

00000010 = 2
00000001 = 1
— — — — — — — — — — — |
00000011 = 3

here’s an example code


# bitwise operators, binary, binary
# operation on each bit
# serves to perform logical processes

a = 9
b = 5

# bitwise OR ( | )
c = a | b # bitwise operators
print(“\n====== OR ======”)
print(“value = “,a,” binary = “,format(a,”08b”)) # format to view binary
print(“value = “,b,” binary = “,format(b,”08b”))
print(“ — — — — — — — — — — — — — — — — ( | )”)
print(“value = “,c,” binary = “,format(c,”08b”))
# bitwise AND ( & )c = a & b # bitwise operators
print(“\n====== OR ======”)
print(“value = “,a,” binary = “,format(a,”08b”)) # format to view binary
print(“value = “,b,” binary = “,format(b,”08b”))
print(“ — — — — — — — — — — — — — — — — ( & )”)
print(“value = “,c,” binary = “,format(c,”08b”))

# bitwise XOR ( ^ )
c = a ^ b # bitwise operator
print(“\n====== OR ======”)
print(“value = “,a,” binary = “,format(a,”08b”)) # format to view binary
print(“value = “,b,” binary = “,format(b,”08b”))
print(“ — — — — — — — — — — — — — — — — ( ^ )”)
print(“value = “,c,” binary = “,format(c,”08b”))
# bitwise NOT ( ~ )
# be careful when using notes in bitwise
c = ~a # bitwise operator
print(“\n====== OR ======”)
print(“value = “,a,” binary = “,format(a,”08b”)) # format to view binary
print(“ — — — — — — — — — — — — — — — — ( ~ )”)
print(“value = “,c,” binary = “,format(c,”08b”)) # result will be minus if value is positive

# then what if we want to reverse the value or flip it
# by using xor and creating a binary variable = 11111111 for
# xor the binary value you want to flip
d = 0b00001001 # content is the same as 9
e = 0b1111111
f = d^e
print(“ — — — — — — — — — — — — — — — — ( ^ )”)
print(“value = “,f,” binary = “,format(f,”08b”)) # then the value will be flipped

the output

====== OR ======
value = 9 binary = 00001001
value = 5 binary = 00000101
— — — — — — — — — — — — — — — — ( | )
value = 13 binary = 00001101
====== OR ======
value = 9 binary = 00001001
value = 5 binary = 00000101
— — — — — — — — — — — — — — — — ( & )
value = 1 binary = 00000001
====== OR ======
value = 9 binary = 00001001
value = 5 binary = 00000101
— — — — — — — — — — — — — — — — ( ^ )
value = 12 binary = 00001100
====== OR ======
value = 9 binary = 00001001
— — — — — — — — — — — — — — — — ( ~ )
value = -10 binary = -0001010
— — — — — — — — — — — — — — — — ( ^ )
value = 246 binary = 11110110

Shifting bitwise

We can also shift the bit to the right or left several times by using the bitwise shifting operator. With shifting, we will change the bit value by shifting the binary number.

c for dummies _ shifting bits

The operator used is
>> for shift right
<< for shift left

# bitwise shift
# shift right ( >> )c = a >> 2 # shift the binary number to the right 2 times
print(“\n====== >> ======”)
print(“value = “,a,” binary = “,format(a,”08b”)) # format to view binary
print(“ — — — — — — — — — — — — — — — — ( >> )”)
print(“value = “,c,” binary = “,format(c,”08b”)) # binary number will shift

# shift left ( << )
c = a << 2 # shift the binary number to the left 2 times
print(“\n====== << ======”)
print(“value = “,a,” binary = “,format(a,”08b”)) # format to view binary
print(“ — — — — — — — — — — — — — — — — ( << )”)
print(“value = “,c,” binary = “,format(c,”08b”)) # binary number will shift

the output

====== >> ======
value = 9 binary = 00001001
— — — — — — — — — — — — — — — — ( >> )
value = 2 binary = 00000010
====== << ======
value = 9 binary = 00001001
— — — — — — — — — — — — — — — — ( << )
value = 36 binary = 00100100

please repeat and practice until you understand, this is very important for those who want to know and learn. Understand bitwise operators. Don’t bother first, repeat this material slowly, then practice. :))

--

--

No responses yet