### Extracting nibbles (binary)

If I have a number 117, represented in binary as : 01110101 and I wanted to grab the top nibble. What would be the decimal value I would be extracting?

Would it be 0111 or 0101 decimal values 112 or 5 or is my understanding completely wrong?

Thanks
The "top" bits are more significant than the "bottom" bits.

So seven is the value you want to extract (since the top nibble is 0111, not 01110000).

Hope this helps.
Lets say I have three binary values 117,117,117 or
01110101 01110101 01110101 and I want to add the first byte to the top nibble of the second byte. Then I want to add the bottom nibble of the second byte to the third byte? Would it then be 117+7 and 5 + 117 ?

Thanks

Uh, sure?

What exactly are you trying to do?
It's just some compressed stream data I have to read a certain way. Is my assumption of how to add these bits correct?
I can't possibly know how to answer that without specific information about what you are trying to do.
I'm reading a compressed stream in 8 bit characters. To decompress that stream it needs to take in 12 bits at a time. That 12 bits of information needs to come from the compressed stream. Therefore, for the first index I take the first byte + the top nibble of the second byte. For the second index I take the bottom nibble of the second byte and add it to the third byte.

It is the adding process I just wanted to confirm, hence my questions above.

Don't look into it too much. Just trying to confirm that I am using the correct logic for adding top and bottom nibbles to an existing 8 bit byte.
It doesn't sound like it to me.

What file format are you reading? Sometimes people mess with bit order, which is weird and confusing (bit-endianness), but the first twelve bits in a file are bits 0-7 of the first byte and bits 0-3 of the second.

 ``12345678`` ``````unsigned first12, second12; unsigned char bs[ 3 ]; f.get( (char*)bs, 3 ); first12 = bs[ 0 ] | ((bs[ 1 ] & 0xF) << 8); second12 = (bs[ 1 ] >> 4) | bs[ 2 ];``````

In this case, you aren't adding bits, you are concatenating them. (By using bit shifting and logical OR on the corresponding bits -- also called bitwise OR.)

Hope this helps.
Thanks for that. I'm using windows XP so from what I understand it uses Little Endian..

I just want to get my understanding right.. The two numbers 244,244 would display in memory as:

11110100 11110100 so if I am concatenating the first byte with the second byte (top nibble) eg.. 11110100 11110100 I would read the 12 bits as one variable in little endian format 111101001111 . Is that right? OR, should I always concatenate the first byte with the least significant bits of the second byte. eg..11110100 11110100 to equal 111101000100 ? Just wondering which one is correct?

Or.. does it totally depend on endianess.
Last edited on
?
I understand why you are so confused by all the different stuff you are reading.

There is byte endianness, which is an issue based on your hardware. But that is not the issue here (or shouldn't be -- you still haven't told me what file format you are trying to read -- it might be).

The issue is bit endianness. When reading the bits in a byte, usually bit 0 is the least significant. Sometimes, however, people consider it differently. So the bytes

244,123 == 0xF4, 0x7B == 1111 0100, 0111 1011

Should technically be bit-ordered 0010 1111 1101 1110, which is backwards from our usual view of numbers. Remember, in a stream, the least significant bits come first, whereas in a number the most-significant bit comes first.

So you read the first byte, and append the least-significant nibble of the second byte to the most-significant end of the first.

Hope this helps.
That does help.. Thanks very much.
Topic archived. No new replies allowed.