pythonpython-3.xbinary-databit-shift

How to shift bits in a 2-5 byte long bytes object in python?


I am trying to extract data out of a byte object. For example: From b'\x93\x4c\x00' my integer hides from bit 8 to 21. I tried to do bytes >> 3 but that isn't possible with more than one byte. I also tried to solve this with struct but the byte object must have a specific length.

How can I shift the bits to the right?


Solution

  • Don't use bytes to represent integer values; if you need bits, convert to an int:

    value = int.from_bytes(your_bytes_value, byteorder='big')
    bits_21_to_8 = (value & 0x1fffff) >> 8
    

    where the 0x1fffff mask could also be calculated with:

    mask = 2 ** 21 - 1
    

    Demo:

    >>> your_bytes_value = b'\x93\x4c\x00'
    >>> value = int.from_bytes(your_bytes_value, byteorder='big')
    >>> (value & 0x1fffff) >> 8
    4940
    

    You can then move back to bytes with the int.to_bytes() method:

    >>> ((value & 0x1fffff) >> 8).to_bytes(2, byteorder='big')
    b'\x13L'