11.the man behind the bit mask
1.define a function,check_bit4,with one argument,input,a integer
2.it should check to see if the fourth bit from the right is on
3.if the bit is no,return "on"(not print)
4.if the bit is off,return "off"
def check_bit4(input):
mask =0b1000
desired = input & mask
if desired > 0:
return "on"
else:
return "off"
example:
num = 0b1100
mask =0b0100
desired = num & mask
if desired > 0:
print "bit was on"
we want to see if the third bit from the right is on.
1.first,we first create a varible num containing the number 12,or 0b1100
2.next,we create a mask with the third bit on.
3.then,we use a bitwise and operation to see if the third bit from the right of numis on
4.if desired is greater than zero,then the third bit of num must have been one.
the bitwise AND(&) operator compares two numbers on a bit level and returns a number where the bits of that number are turned on if the corresponding bits of both numbers are 1