# Need help with Bit manipulation programming

This is a discussion on Need help with Bit manipulation programming within the C Programming forums, part of the General Programming Boards category; I hope someone can give me some clues or tips to get started in solving the following problem. I have ...

1. ## Need help with Bit manipulation programming

I hope someone can give me some clues or tips to get started in solving the following problem.

I have an Integer array of size 5. Now I need to copy only the first 20 bits of each integer into a Character array. So finally, the character array has first 20 bits from first integer and immediately followed by first 20 bits from second integer and so on..

I know this can be done using bit manipulation but I am not getting the right idea. I would really appreciate if someone can give me some clues.

Thanks.

2. This sounds rather ankward to do. When you say you want to store it in a char array, i guess you mean that you want to store it "somewhere" in memory. Also, i'm curious why you need to do this ?

If you have 5x20 bits to store, that mean you need 100 bits. 100 / 8 = 12.5, so it means you'll need, in fact, 13 bytes of memory, where the last 4 bits will contain some random stuff. Note that each operation has to move k*8 bits at the same time, where k is an integer > 0, because that's how much of the computer works....

So... well... i don't have much time left... but from what i see, it's going to be easier to write a purely sequential program for treating 5 integer instead of using some kind of more elaborate structure (like a loop).

3. Indeed. Unfortunately, the instructions can only work with data sizes 1, 2, 4 or 8 bytes. Processor instructions cannot work with other sizes, so what you propose will be cumbersome.
One thing that "might" work is that you treat one byte as one bit. That way you can allocate as many bytes of memory as you need for the number of bits and set them accordingly.
But then using that array will be cumbersome.
Perhaps you should explain a little more on what you're trying to do.

4. You put bits into an int in multiples of 20, and take them out in multiples of 8.
If any bits are left afterwards, you flush out the remainder.
Like this:
Code:
```unsigned int bitsHeld=0, bits=0;

pack20BitsOf(int val)
{
bitsHeld += 20;
bits = (bits << 20) | val;
while (bitsHeld >= 8)
{
bitsHeld -= 8;
writeoutByte(bits >> bitsHeld);
bits ^= (bits >> bitsHeld) << bitsHeld;
}
}

flushBits()
{
if (bitsHeld > 0)
{
writeoutByte(bits << (8-bitsHeld));
bits = 0;
bitsHeld = 0;
}
}```
or if you want to pack the bits in the other order (which may be a little bit simpler), then like this:
Code:
```unsigned int bitsHeld=0, bits=0;

pack20BitsOf(int val)
{
bitsHeld += 20;
bits |= val << bitsHeld;
while (bitsHeld >= 8)
{
bitsHeld -= 8;
writeoutByte(bits & 0xFF);
bits >>= 8;
}
}

flushBits()
{
if (bitsHeld > 0)
{
writeoutByte(bits & 0xFF);
bits = 0;
bitsHeld = 0;
}
}```

5. Why do I see missing return type?

6. Rats! - Guess whether I tried compiling it or not.
void should do for the return type. However if writeoutByte were writing to disk and itself could return failure codes, then I'd make the return type mirror that of writeoutByte, with the failure codes being passed back in the same manner.
It's the algorithm that I was more worried about. Make sure you take the time to understand how it works, as you'll have a better idea how to do such things in future.

If you would like the corresponding "unpacker" and have trouble implementing this yourself, then let me know. Or if you have trouble using the code I've posted, just ask.
flushBits is to only be called once, at the end, after all numbers have been processed.

7. What are the "first bits?" The lowest 20 bits, or the highest 20 bits?

8. its the lowest 20 bits

Popular pages Recent additions