interpret_bit_flags

astropy.nddata.bitmask.interpret_bit_flags(bit_flags, flip_bits=None)[source]

Converts input bit flags to a single integer value (bit mask) or None.

When input is a list of flags (either a Python list of integer flags or a sting of comma- or ‘+’-separated list of flags), the returned bit mask is obtained by summing input flags.

Note

In order to flip the bits of the returned bit mask, for input of str type, prepend ‘~’ to the input string. ‘~’ must be prepended to the entire string and not to each bit flag! For input that is already a bit mask or a Python list of bit flags, set flip_bits for True in order to flip the bits of the returned bit mask.

Parameters
bit_flagsint, str, list, None

An integer bit mask or flag, None, a string of comma- or ‘+’-separated list of integer bit flags, or a Python list of integer bit flags. If bit_flags is a str and if it is prepended with ‘~’, then the output bit mask will have its bits flipped (compared to simple sum of input flags). For input bit_flags that is already a bit mask or a Python list of bit flags, bit-flipping can be controlled through flip_bits parameter.

flip_bitsbool, None

Indicates whether or not to flip the bits of the returned bit mask obtained from input bit flags. This parameter must be set to None when input bit_flags is either None or a Python list of flags.

Returns
bitmaskint or None

Returns an integer bit mask formed from the input bit value or None if input bit_flags parameter is None or an empty string. If input string value was prepended with ‘~’ (or flip_bits was set to True), then returned value will have its bits flipped (inverse mask).

Examples

>>> from astropy.nddata.bitmask import interpret_bit_flags
>>> "{0:016b}".format(0xFFFF & interpret_bit_flags(28))
'0000000000011100'
>>> "{0:016b}".format(0xFFFF & interpret_bit_flags('4,8,16'))
'0000000000011100'
>>> "{0:016b}".format(0xFFFF & interpret_bit_flags('~4,8,16'))
'1111111111100011'
>>> "{0:016b}".format(0xFFFF & interpret_bit_flags('~(4+8+16)'))
'1111111111100011'
>>> "{0:016b}".format(0xFFFF & interpret_bit_flags([4, 8, 16]))
'0000000000011100'
>>> "{0:016b}".format(0xFFFF & interpret_bit_flags([4, 8, 16], flip_bits=True))
'1111111111100011'