How do I check if a specific bit is on or off?

Question

How do I check if a specific bit is on or off?

Answer

If a specific bit is on, it will result in a 1 in that position when ANDed together with a mask containing a 1 in that position and 0s everywhere else. For example, if we wanted to check if the 0th bit is on, we’d write:

input = 0b1010
mask = 0b0001
desired = input & mask

if desired > 0:
  print “it’s on!”
else:
  print “it’s off!”

Our mask has only one bit turned on, since that’s the only position we’re looking for to be on. Once we AND it with our input, only that position can possibly be a 1, since AND requires both inputs to be True, or 1. That’s why in the example above we get it’s off! printed to the console.

4 Likes

HI, why is that wrong?

def check_bit4(bit):
  bit = int()
  mask = 0b1000
  desi = bit & mask
  
  if desi > 0:
    return "on"
  else:
    return "off"

If you use the int() method without a parameter it is returning 0 as default.
Delete this line from the code and it will be fine.

1 Like

remove bit = int()
it should be ok

‘’’
def check_bit4(input):

num = bin(input)

mask = 0b1000

desired = mask & inut

if desired > 0:

return "on"

else:

return "off"

‘’’
Hi, I was wondering why does the num valuable messes up the code, since once I remove it and replace it with input in the & operation, it works as it should.
Thanks for your help

1 Like

bin() takes in an integer and converts it into binary, but it is in string format. So num takes on a string and Python is unable to compare a string and a binary using &.

However, Python does allow you to use & to compare between an integer and a binary. After you removed num and replaced it with input in the & operation, input, which is an integer, is now being compared with mask, a binary.

And so the code works.

1 Like

def check_bit4(input):

mask = 0b101

disere = input & mask

if disere > 0:

return "on"

else:

return "off"

#why it is wrong

The model answer of this exercise is:

def check_bit4(input):
  mask = 0b1000
  desired = input & mask
  if desired > 0:
    return "on"
  else:
    return "off"

If the fourth bit is “on”, “desired” should be at least “0b1000”, which is “8”. Why the code is not “if desired >= 8”?

If we want to check whether a particular bit is on/off, then the mask should have a 1 in that bit and 0’s in all the other bits. The 0’s will ensure that the other bits are not taken into consideration when making our decision (because 0 & 1 as well as 0 & 0 will result in 0, so we are purposely ignoring all the other bits by zeroing them).

The mask should have a 1 in the bit that we want to check.
If the input has a 0 in that specific bit, then 0 & 1 will result in 0. Since we have already ignored all the other bits, so the final result will be 0.
If the input has a 1 in that specific bit, then 1 & 1 will result in 1. Since we have already ignored all the other bits, so the final result will be non-zero (the exact result depends on the bit we wanted to check).

Consider the following:

input = 0b101001100
mask = 0b1000 # Want to check 4th bit
result = input & mask
print(result) # 8 (Non-Zero Result because 4th bit of input is 1) 

input = 0b101001100
mask = 0b100 # Want to check 3rd bit
result = input & mask
print(result) # 4 (Non-Zero Result because 3rd bit of input is 1) 

# If the particular bit is 1, then the & will give a non-zero result. The exact
# result depends on the position of the bit.

input = 0b1010001
mask = 0b1000 # Want to check 4th bit
result = input & mask
print(result) # 0 (Zero Result because 4th bit of input is 0) 

If we just wanted to check the 4th bit and no other bits, then technically we could have written the condition as:

if desired == 8:
    return "on"
else:
    return "off"

But, suppose we had a second parameter in our function for specifying which bit we want to check, then the following could work:

def check_bit(num, position):
    mask = 1 << (position - 1)
    desired = num & mask

    if desired > 0:
        return "on"
    else:
        return "off"

print(check_bit(0b101001101, 1))  # on
print(check_bit(0b101001101, 2))  # off
print(check_bit(0b101001101, 3))  # on 
print(check_bit(0b101001101, 4)) # on
print(check_bit(0b101001101, 5))  # off
print(check_bit(0b101001101, 6)) # off 
print(check_bit(0b101001101, 7))  # on

Just specifying the condition    if desired > 0:    as non-zero gives a more general condition (works for any bit), as opposed to    if desired == 8:    (works only for 4th bit).