2

I am checking status of single bit currently based on the variable value I pass. I have tried

def check_status(bit0 = False,bit2 = False,bit3=False):
    regvalue = read_register()
    if bit0:
        if regvalue & 0x01:
            return 1
        else:
            return 0
    elif bit2:
        if regvalue & 0x04:
            return 1
        else:
            return 0
    elif bit3:      
        if regvalue & 0x08:
            return 1
        else:
            return 0
    else:
        raise

How in a efficient way can I return the status of more than one bit? like if want to check the status of bit0 and bit3 by passing them as true, in return I should get 1 if both are set or their current values

NGB
  • 320
  • 3
  • 15
  • 1
    Possible dublicate of: http://stackoverflow.com/questions/11669178/how-to-create-an-array-of-bits-in-python – Stanislav Ivanov Feb 02 '17 at 09:47
  • Possible duplicate of [How to create an array of bits in Python?](http://stackoverflow.com/questions/11669178/how-to-create-an-array-of-bits-in-python) – ilim Feb 02 '17 at 10:11

3 Answers3

0

I am not sure that it is exactly what you are looking for but I would propose the following

var =  0b10100
mask = 0b10001
if not var^mask:
    print "all bits match"
else:
    print "match bits  (0 if bits match, 1 otherwise)", "{0:b}".format(var ^ mask)
Roman Fursenko
  • 576
  • 2
  • 8
0

You can create a bit mask out of your bit0/2/3 variables and check your register against that mask

def check_status(bit0 = False,bit2 = False,bit3=False):
    regvalue = read_register()
    mask = (0, 0x1)[bit0]
    mask = mask | (0, 0x4)[bit2]
    mask = mask | (0, 0x8)[bit3]
    return regvalue & mask != 0 

This returns true if at least one of the requested bits are set. If you want to return True only if all the requested bits are set, do return regvalue & mask == mask

This constructs a mask where mask = 0xd if bit0, bit2, and bit3 are True, and e.g. a mask of 0x1 if bit0 is True and bit2/bit3 are False.

(See here if you are wondering about the (0,0x4)[bit2] syntax)

Community
  • 1
  • 1
nos
  • 207,058
  • 53
  • 381
  • 474
0

The module bitarray is really good to construct bitarrays from various inputs. Here is a little general example:

from bitarray import bitarray
a1 = bitarray('101')
a2 = bitarray(True, False, True)
a3 = bitarray('100')

# Connecting arrays with & gives you an array in which all the bytes which are 1 in both arrays are 1 as well
a1 & a2  # bitarray('101')
a1 & a3  # bitarray('100')

# comparing this result with your original mask, will give you a True or False
a1 & a2 == a1  # True
a1 & a3 == a2  # False

For more information see here.

Regarding your function given that read_register() returns a valid convertible value for bitarray() I'd go about it this way:

import bitarray from bitarray
def check_status(bit0 = False,bit2 = False,bit3=False):
    regvalue = bitarray(read_register())
    mask = bitarray([bit1, bit2, bit3])  # or even use the args parameter here
    return regvalue & mask == mask
Kie
  • 1,585
  • 12
  • 25