+ 1
Can someone explain me this bit operation code
n1 = input("Binary n1: ") n2 = input("Binary n2: ") print() n1 = int(n1, 2) n2 = int(n2, 2) bit_or = n1 | n2 bit_and = n1 & n2 bit_xor = n1 ^ n2 bit_or = bin(bit_or) bit_and = bin(bit_and) bit_xor = bin(bit_xor) print(" n1: %10s" % bin(n1)) print(" n2: %10s" % bin(n2)) print(" ------------") print(" OR: %10s" % bit_or) print("AND: %10s" % bit_and) print("XOR: %10s" % bit_xor)
3 Answers
+ 3
It takes two binary numbers ,say for example
0b111
0b111
Converts them into integers
7
7
Then perform binary operations like binary or ,and,XOR on them
which results in
7
7
0
and then converts these integers back into binary numbers
0b111
0b111
0b0
+ 1
Adding to Abhay's answer. If you are not aware of bitwise operations, these will help
https://www.sololearn.com/learn/4073/?ref=app
https://www.sololearn.com/learn/4072/?ref=app
https://www.sololearn.com/learn/4074/?ref=app