# [Tutor] stumped again adding bytes

Luke Paireepinart rabidpoobear at gmail.com
Fri Feb 23 22:18:08 CET 2007

```shawn bright wrote:
> ok, i am good with what you have explained here,
> now i am on a similar problem.
>
> the data i need is in a condensed format. in other words, they are
> sending 2 values in three bytes.
>
> so if i have 3 values say a = 53, b = 13, and c = 31
>
> so value 1 is the first byte ( a ) and the first 4 bits of the second
> byte (b)
> value 2 is the last 4 bits of byte (b) and byte (c)
Please try not to interchange your terms, it makes your questions confusing.
'so if i have 3 values say a = 53, b = 13, and c = 31'
Should be 'so if I have 3 bytes'
because immediately after, you talk about your extracted data as 'values.'
It's not too big of a deal, but it was confusing at first.
>
> so i believe i do shifting here. as in i do a
> (a << 4) * 32 + b
You need to read an article on bit manipulation.
I'll give you a summary of what you're trying to do.
given the byte
x = 1 1 0 0 0 1 1 0
If you want to extract just the four most significant bits, [1 1 0 0],
you shift the number TO THE RIGHT by 4.
x >> 4
will result in 4 shifts like so:
1 1 0 0 0 1 1 0 -> 1 1 0 0 0 1 1
1 1 0 0 0 1 1 -> 1 1 0 0 0 1
1 1 0 0 0 1 -> 1 1 0 0 0
1 1 0 0 0 -> 1 1 0 0
Which results in the number 0x0C or 12.
Now, since these are the MSB, you could shift back 4 places
x << 4
1 1 0 0 -> 1 1 0 0 0
1 1 0 0 0 -> 1 1 0 0 0 0
1 1 0 0 0 0 -> 1 1 0 0 0 0 0
1 1 0 0 0 0 0 -> 1 1 0 0 0 0 0 0

but I don't know if you want to do that or not.
and if you wanted to do that, you could just use a bitmask from the
beginning.  see the next part.

Now as for the least significant bits:
recall that

1 1 0 0 0 1 0 0 & 0 0 0 0 1 1 1 1
will yeild
0 0 0 0 + whatever the last 4 bits are in the first item.

Hope that helps.
It's better if you take the time to really understand what is happening
in all these cases, so you won't have to experiment with trial and error.
-Luke
```

More information about the Tutor mailing list