# [Tutor] stumped again adding bytes

shawn bright nephish at gmail.com
Fri Feb 23 22:47:23 CET 2007

```whoops, meant this to the list, sorry Luke.

On 2/23/07, shawn bright <nephish at gmail.com> wrote:
> Thanks for your help, Luke.
> i am trying to get a grasp on how all this works, which is the msb, lsb, etc..
>
> if i use i bitmask of 240 it will mask the most significant 4 bits
> so that only the most significant 4 bits remain..
> like 53 & 240 = 48 ( because only the 32 and 16 are set)
> and if i use 15 it should mask the least significant bits, right ?
>
> now if i am using the first half of the 2nd byte, and making the first
> byte the most significant.
> i would shift the first byte to the Left by 4
> byte1 << 4  then add ( byte2 & 240 )
>
> so for the next number that i want that will consist of the 2nd half
> of the 2nd byte as the most significant , i would take the second byte
> with my bitmask then shift it right by 4 then add the third byte to
> that .... am i getting this right
> like x = ((byte2 & 240) << 4) + byte 3
>
> i think ?
>
> shawn
>
>
> On 2/23/07, Luke Paireepinart <rabidpoobear at gmail.com> wrote:
> > 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)
> > '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
> >
>
```