1. ## Extracting bits

I have a little Endian machine.
I want to extract two bits from right side of this number 1011001

This number is 89. If I go on moding it with 2, I'll get the desired two bits, but then how to store them in another variable ?

Kindly suggest some other way out !

2. O_o

With the assignment operator!?

Soma

3. If you want the right two bits you could do something like this:

Code:
```int x = 89;
int y = x & 0x3;```
EDIT: Or for your modulo approach:

Code:
`int y = 89 % 2;`
Not sure if you meant the hex value or the base 10 value.

4. Originally Posted by Syscal
If you want the right two bits you could do something like this:

Code:
```int x = 89;
int y = x & 0x3;```
Thanks for the answer, but I forgot to mention in my first post that the variable in which I am supposed to store the extracted bits *may* contain some previously stored bits. The new bits should not overlap them !

What to do ?

5. Originally Posted by AnishaKaul
Thanks for the answer, but I forgot to mention in my first post that the variable in which I am supposed to store the extracted bits *may* contain some previously stored bits. The new bits should not overlap them !

What to do ?
Then you must clear the bits in the target value to 0 first, then OR the required bits into those positions.

6. o_O

I'm not getting this. What you ask is impossible. You can't just keep on cramming more bits in the same byte.

Can you give an example of the input you have and the output you desire?

Soma

7. For that it would be:

Code:
```int x = 89;
y |= (x & 3);```

8. Originally Posted by phantomotap
Can you give an example of the input you have and the output you desire?
Yes,
I have an character array which is to be filled with the desired extracted bits of various numbers.

Originally Posted by Syscal
For that it would be:

Code:
```int x = 89;
y |= (x & 3);```
Thanks you for the information, but I did the same before you replied and this solved the problem.
New problem was how to decide that the ANDing number has to be 3 without hard coding it, a person from LQ gave me the following formula:
Code:
`(2^number) - 1`
where number is the number of bits to be extracted !

9. Initially I tried to solve it by the following method: The problem with this method is that the bits get stored in the reverse order which I don't want !!

Can some one tell me how to get around the problem in the following code !
Code:
```unsigned int byteToBeFilled = 0;
unsigned int holder  = 89;
unsigned int i  = 2;

message = new unsigned char [4096];
bzero (message, 4096);

while (((holder / 2) >= 1) && (i > 0))
{
message [byteToBeFilled] = message [byteToBeFilled] | holder % 2;
holder = holder / 2;

i--;

if (i > 0)
{
message [byteToBeFilled] = message [byteToBeFilled] << 1;
}
}```

10. It sounds like you are building a status variable where bits are used to confirm certain actions or features. You should look into the use of named bitfields. This allows you to organize your status byte as you need and also to prevent overlaps between bits.

Code:
```// define your bitfield
unsigned int a : 1,
b : 1,
...
y : 2,
z: 2;

y = x & 3;```
This will assign the 2 least significant bits of x into the bitfield without disturbing the other bits, which is what I believe you originally needed.

11. I'm not sure what you're trying to do. If you want to extract all of the bits and move them to a new variable, just copy them. If you only want the low order bits, you'd do something like

Code:
```unsigned int mask = (1<<length) - 1; // fill mask with <length> bits of 1s

dest &= ~mask; // Clear out low order bits in dest
dest |= source & mask; // copy low order bits from source to dest, leaving the rest of dest intact.```
You can shift mask up to pick something other than the least significant bits.

If you're copying from a 32 bit value to an array of 4 8 bit values :

Code:
```dest[0] = source;
dest[1] = source >> 8;
dest[2] = source >> 16;
dest[3] = source >> 24;```
Combine the two bits of code if you only need to copy parts of source to dest.