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?

6
  • 3
    did you mean 0b00000011 instead of 0x00000011? It didn't matter above because it's only one bit Commented Sep 20, 2019 at 14:18
  • 2
    0x00000011 is 17 and 28 & 17 = 16 is correct. I suspect that you want b everywhere that you have x in the above code (although as @Marat points out, you got lucky in the other places). Commented Sep 20, 2019 at 14:20
  • no, I meant 0x00000011.. With your mask it does work. Why do the other masks work? Sorry, but I'm totally a newby in bit manipulation operations. Commented Sep 20, 2019 at 14:22
  • 2
    0x00000011 is in base 16, not base 2. Hex isn't binary (though they are naturally related). The meaning of 0x00000011 is 1x16 + 1 = 17, not 1x2 +1 = 3 Commented Sep 20, 2019 at 14:23
  • @JohnColeman: ahh okay, now I see also why the other masks are working.. thank you Commented Sep 20, 2019 at 14:24

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.