Skip to content

Instantly share code, notes, and snippets.

@shabbirbhimani
Created April 1, 2018 12:03
Show Gist options
  • Save shabbirbhimani/0a0f1960635680e762d7bed74612d5a8 to your computer and use it in GitHub Desktop.
Save shabbirbhimani/0a0f1960635680e762d7bed74612d5a8 to your computer and use it in GitHub Desktop.
Bitwise Operators
# declare 2 variables, variable1 with value 78, and variable2 with value 341
# we are going to perform series of operations and the result will be stored in output variable
variable1 = 78
variable2 = 341
print("variable1 =",variable1," - binary value =",bin(variable1))
print("variable2 =",variable2," - binary value =",bin(variable2))
# output: variable1 = 78 - binary value = 0b1001110
# output: variable2 = 341 - binary value = 0b101010101
# performing binary & operation
output = variable1 & variable2
print ("output of binary & operation is ", output,' -',bin(output))
# output : output of binary & operation is 68 - 0b1000100
# performing binary or operation
output = variable1 | variable2
print ("output of binary or operation is ", output,' -',bin(output))
# output : output of binary or operation is 351 - 0b101011111
# performing binary xor operation
output = variable1 ^ variable2
print ("output of binary xor operation is ", output,' -',bin(output))
# output: output of binary xor operation is 283 - 0b100011011
# performing binary complement operation
output = ~variable1
print ("output of binary complement is ", output,' -',bin(output))
# output: output of binary complement is -79 - -0b1001111
# performing binary left shift operation
output = variable2 << 2
print ("output of binary left shift is ", output,' -',bin(output))
# output: output of binary left shift is 1364 - 0b10101010100
# performing binary right shift operation
output = variable2 >> 2
print ("output of binary right shift is ", output,' -',bin(output))
# output: output of binary right shift is 85 - 0b1010101
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment