Problem with bits in byte array....


Closed Thread
Results 1 to 16 of 16

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: Problem with bits in byte array....

    Hi Don,
    Thanks for that. I've tried to figure your code out without much luck, that visual representation helps me understand how you intend it to work. First I got thrown off by the arrays not being indexed from 0 and I thoght that was by intention (some trick) to make it work but now I'm not sure and I have to ask if you tried it?

    If I load arr1 with 77,97,110 I expect to get 19,22,5,46 (taken from Wikipedias example) but instead I get 19,0,4,0.

    The reason, I think, is that you're trying to load two bytes from the array into one word in one go with w1=arr1[1] etc.

    /Henrik.

    EDIT: It's not near as small as yours but this works for me:
    Code:
        Out[0] = ((In[0] >> 2) & 63)
        Out[1] = ((In[0] & 3) << 4) + ((In[1] & 240) >> 4) & 63
        Out[2] = ((In[1] & 15) << 2) + ((In[2] & 192) >> 6) & 63
        Out[3] = (In[2] & 63)
    Last edited by HenrikOlsson; - 18th October 2011 at 20:47.

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: Problem with bits in byte array....

    Henrik,
    Yes, I mistakingly was thinking that some 'W=' takes the two consecutive bytes. Also array[0] and [1] confuses me at times with for-next etc. so I just consider array start at [1].
    What you coded is good. The top and bottom (out[0] and out[3]) are simple but the ones where the 6 bit straddle 2 bytes take a little more. I tried to follow your calc on out[2] but my brain started smoking. Just wondering if '+' changes the value as opposed to the '>>' and '<<' and &. But, if it works, never argue with success.
    Anyway, thats all a relatively small simple code.
    Don

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: Problem with bits in byte array....

    Let's take the Wikipedia example where they show Man being encoded:
    Code:
      In[0]    In[1]   In[2]
    01001101.01100001.01101110
    The bytes we are interested in, in the case of Out[2] are marked in red. We can break the line down in steps, making it easier to see what's going on.

    Code:
    Out[2] = In[1] & 15          ' Isolate the 4 lower bits (Out[2] now equals %00000001)
    Out[2] = In[1] = In[1] << 2  ' Shift "up" two steps (Out[2] now equals %00000100)
    Temp = (In[2] & 192)         ' Isolate the 2 top bits in In[2] (Temp now equals %01000000)
    Temp = In[2] >> 6            ' Shift it down 6 steps (Temp now equals %00000001)
    Out[2] = Out[2] + Temp       ' Add the two together (Out[2] now equals %00000101)
    Out[2] = Out[2] & 63         ' Isolate the 6 lower bits
    In fact, I wonder if the last step is actually needed....depends on what gets "shifted in" at the top but I guess that "should" be all zeros - perhaps room for improvement there....

    /Henrik.

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts