Extracting nibbles (binary)

Nov 29, 2013 at 1:41pm
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
Nov 29, 2013 at 2:14pm
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.
Nov 29, 2013 at 4:03pm
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

Nov 29, 2013 at 4:23pm
Uh, sure?

What exactly are you trying to do?
Nov 29, 2013 at 10:18pm
It's just some compressed stream data I have to read a certain way. Is my assumption of how to add these bits correct?
Nov 29, 2013 at 11:41pm
I can't possibly know how to answer that without specific information about what you are trying to do.
Nov 30, 2013 at 12:14am
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.
Nov 30, 2013 at 12:50am
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.

1
2
3
4
5
6
7
8
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.
Nov 30, 2013 at 2:25am
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 Dec 1, 2013 at 12:05pm
Dec 2, 2013 at 1:02am
?
Dec 2, 2013 at 2:12am
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.
Dec 2, 2013 at 4:11am
That does help.. Thanks very much.
Topic archived. No new replies allowed.