简体   繁体   中英

Need help understanding a short python code

I want to emphasis that this is not a ask for completing my homework or job: I am studying the LZW algorithm for gif file compression by reading someone's code on github , and got confused by a code block here:

class DataBlock(object):

    def __init__ (self):
        self.bitstream = bytearray()
        self.pos = 0

    def encode_bits (self, num, size):
        """
        Given a number *num* and a length in bits *size*, encode *num* 
        as a *size* length bitstring at the current position in the bitstream.
        """
        string = bin(num)[2:]
        string = '0'*(size - len(string)) + string
        for digit in reversed(string):
            if len(self.bitstream) * 8 <= self.pos:
                self.bitstream.append(0)
            if digit == '1':
                self.bitstream[-1] |= 1 << self.pos % 8
            self.pos += 1

What I cannot understand is the for loop in the function encode_bits() :

for digit in reversed(string):
    if len(self.bitstream) * 8 <= self.pos:
        self.bitstream.append(0)
    if digit == '1':
        self.bitstream[-1] |= 1 << self.pos % 8
    self.pos += 1

Here is my guess (depend on his comment):

The function encode_bits() will turn an input integer num into a binary string of length size (padding zeroes at left if needed) and reverse the string, and append the digits to bitstream one by one. Hence suppose s=DataBlock() , then s.encode_bits(3, 3) would firstly turn 3 into 011 (padding a zero at left to make it length 3) and reverse it to 110 , and then append 110 to self.bitstream , hence the result should be bytearray('110'). But as I run the code the result gives bytearray(b'\\x03') , not as expected. Further more, \\x03 is one byte, not 3 bits, conflicts with his comment, I cannot understand why?

I forgot to add that his code runs and gives correct output hence there's something wrong in my understanding.

Try looking at it this way:

  • You will be given a bytearray object (call it x for the moment).
  • How many bytes are in the object? (Obviously, it's just len(x) .)
  • How many bits are in the object? (This is an exercise; please calculate the answer.)

Once you've done that, suppose we start with no (zero) bytes in x , ie, x is a bytearray(b'') . How many bytes do we need to add ( x.append(...) ) in order to store three bits ? What if we want to store eight bits? What if we want to store ten bits?

Again, these are exercises. Calculate the answers and you should be enlightened.

(Incidentally, this technique, of compressing some number of sub-objects into some larger space, is called packing . In mathematics the problem is generalized , while in computers it is often more limited .)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM