Converting a String representation of bits to a byte

前端 未结 3 578
南方客
南方客 2021-02-15 01:48

I\'m just beginning to learn about file compression and I\'ve run into a bit of a roadblock. I have an application that will encode a string such as \"program\" as a compressed

3条回答
  •  抹茶落季
    2021-02-15 02:14

    An introduction to bit-shift operators:

    First, we have the left-shift operator, x << n. This will shift all the bits in x left by n bits, filling the new bits with zero:

          1111 1111 
    << 3: 1111 1000
    

    Next, we have the signed right-shift operator, x >> n. This shifts all the bits in x right by n, copying the sign bit into the new bits:

          1111 1111 
    >> 3: 1111 1111
    
          1000 0000
    >> 3: 1111 0000
    
          0111 1111 
    >> 3: 0000 1111
    

    Finally, we have the zero-fill right-shift operator, x >>> n. This shifts all bits in x right by n bits, filling the new bits with zero:

           1111 1111 
    >>> 3: 0001 1111
    

    You may also find useful the bitwise-or operator, x | y. This compares the bits in each position in x and y, setting the new number's bit on if it was on in either x or y, off otherwise:

      1010 0101
    | 1010 1010
      ---------
      1010 1111
    

    You should only need the previous operators for the problem at hand, but for the sake of completeness, here are the last two:

    The bitwise-and operator, x & y sets the bits in the output to one if and only if the bit is on in both x and y:

      1010 0101
    & 1010 1010
      ---------
      1010 0000
    

    The bitwise-xor operator, x ^ y sets the output bits to one if the bit is on in one number or the other but not both:

      1010 0101
    ^ 1010 1010
      ---------
      0000 1111
    

    Now, applying these to the situation at hand:

    You will need to use the bit-shift operators to add and manipulate bits. Start setting bits at the right side according to their string representations and shift them over. Continue until you hit the end of a byte, and then move to the next byte. Say we want to create a byte representation of "1100 1010":

    Our byte    Target
    ---------   --------
    0000 0000
                1100 1010
    0000 0001   ^
                1100 1010
    0000 0011    ^
                1100 1010
    0000 0110     ^
                1100 1010
    0000 1100      ^
                1100 1010
    0001 1001        ^
                1100 1010
    0011 0010         ^
                1100 1010
    0110 0101          ^
                1100 1010
    1100 1010           ^
    

    I will, of course, leave it to you to apply this to your work.

提交回复
热议问题