Not expected output in bit-masking an integer in Python

0

The following data is added to a bitstream

bitstream.add(BYTE_OFFSET_SIZE_FLAG, 1)  # value = 0
bitstream.add(NON_OVERLAPPING_AU_RANGE_FLAG, 1)  # value = 0
bitstream.add(POS_40_BITS_FLAG, 1)  # value = 0
bitstream.add(BLOCK_HEADER_FLAG, 1)  # value = 1
bitstream.add(MIT_FLAG, 1)  # value = 1
bitstream.add(CC_MODE_FLAG, 1) # # value = 1
bitstream.add(0, 2)  # to make the bitstream of 8 bits

When I unpack it:

data = struct.unpack('>B', in_file.read(1))[0] # evaluates to 28, in bin 00011100
ds_header["byte_offset_size_flag"] = data >> 7 # value = 0
ds_header["non_overlapping_au_range_flag"] = data >> 6 & 0x01  # value = 0
ds_header["pos_40_bits_flag"] = data >> 5 & 0x001 # value = 0
ds_header["block_header_flag"] = data >> 4 & 0x0001 # value = 1
ds_header["mit_flag"] = data >> 3 & 0x00001  # value = 1
ds_header["cc_mode_flag"] = data >> 2 & 0x000001 # value = 1

ignore = data & 0x00000011 # value = 16, but 0 is expected

I do not really understand why the ignore value is 16, since the last 2 bits of data are 00.. What am I doing wrong?

python
asked on Stack Overflow Sep 20, 2019 by user123892

1 Answer

1

0x00000011 is 17 and 28 & 17 = 16 is correct. As @Marat observed, you probably intended 0b00000011. The x flags the literal as base 16, and 1+16 = 17. The b would flag it as its intended base 2. Since the value of 01 is 1 in all bases, the error didn't bite you earlier in the computation, but even there the x should probably be changed to b for clarity.

answered on Stack Overflow Sep 20, 2019 by John Coleman

User contributions licensed under CC BY-SA 3.0