# Thread: How to arrange bit side by side

1. ## How to arrange bit side by side

I have some problem to arrenge bit side by side. I have a binary value of a charecter is 00010000. which is stored in LSB of a pixel in an image, with the pattern of 3:3:2 from the char bit. ie

r = 10101 000( 000=first 3 bit from 8 bit chr )
g= 11011 100(100= middle 3 bit from 8bit chr)
b= 011001 00(00= last 2 bit from 8 bit chr)

I have successfully extracted the bit and store in an integer variable but I can't understand how to arrange the bit to form the 00010000.

I am using vb.net 2008 express

2. ## Re: How to arrange bit side by side

r = 10101 000( 000=first 3 bit from 8 bit chr )
g= 11011 100(100= middle 3 bit from 8bit chr)
b= 011001 00(00= last 2 bit from 8 bit chr
I'm sorry but that really doesn't seem to make any sense. If the bit value is 00010000 then that must be r = 0, g = 100, b = 0, mustn't it? I'm also a bit confused as to why b only gets 2 bits but I guess you have no control over that?

Bitwise comparisons are certainly possible in VB but this seems to be using some conversion process that is beyond my little brain.

3. ## Re: How to arrange bit side by side

The reason b only gets 2 bits is because that's all that's left. There may also be something about the sensitivity of the eye to certain color ranges, but it's ultimately a means of packing an RGB value into a single byte.

I, too, can't understand quite what you are doing, but the solution involves the >> or << bit-shift operators.

4. ## Re: How to arrange bit side by side

Here is a couple of options
Code:
``` Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim r, b, g As Integer
Dim ob As Byte

r = 0
g = 4
b = 0
ob = (((r And 7) << 3) Or (g And 7)) << 2 Or (b And 3)

'or
ob = 0
r = (r << 5) And &HE0
g = (g << 2) And &H1C
b = b And 3
ob = r Or g Or b

End Sub```
The first combines the shifts and masking and doesn't alter the values of r,g,b.
The second preshifts and masks each r,g,b value (could use secondary variables if you didn't want to clobber the originals), and then just ORs them together.
The second case would be simpler to mask first, then shift, as we did for the first case, i.e.
r = (r And 7) << 5
g = (g And 7) << 2
b = b And 3
ob = r Or g Or b

You wouldn't have to mask if you were 100% sure the r,g,b values were valid, ie. in the range (0-7, 0-7, 0-3)

p.s. my first version of this post had the first shift incorrect, it had 6 instead of 5, but I edited it fairly quickly so hopefully no one referenced it before it was corrected (and the first case had two shifts of 3, rather than 3 and 2).

5. ## Re: How to arrange bit side by side

Sounds more like steganogrpaphy to me, where ashisbiswas is encoding a character with bit pattern 00010000 into the least significant bits of a pixels RGB components.

So the letter A (01000001) for example would be split as 010 000 01 and those bit patterns would replace the LSbits of the R G and B components of an existing 32 or 24 bit coloured pixel. Now he want to reverse the process and wants to know how to join the 3 bit patterns to get back the hidden character.

And it looks like passel has provided an example of code that will do that job while I was typing

6. ## Re: How to arrange bit side by side

LOL, I didn't recognize what that part of the explaination was about so just ignored it and provided the bit packing examples, but now of course the whole post makes sense and seems obvious. Must be getting older everyday.

7. ## Re: How to arrange bit side by side

Thanks. I could not figure out the part. Now i understand and sorry for my explanation.Thank you.

#### Posting Permissions

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

Featured