[Tinyos-help] Bits operations

Philip Levis pal at cs.stanford.edu
Tue Apr 17 16:11:17 PDT 2007


On Apr 17, 2007, at 2:32 PM, Benjamin Madore wrote:

>
> On Tue, April 17, 2007 5:08 pm, Jose A. said:
>> Hi.
>> In my Tiny OS application I´m trying to obtain the first eight  
>> bits of
>> a variable that take up 2 bytes of space but i don´t know how I  
>> can do
>> it.
>>
>> Any idea?.
>
> typedef union _byte_word {
>   struct byte {
>     uint8_t byteA, byteB;
>   };
>   uint16_t word;
> } byteword;
>
> byteword x;
> x.word = (byteword) variable;
> ...
> uint8_t whatIwant = x.byte.byteA;
>
> Now, I'm sure I didn't do that right. I'm also sure I don't know  
> the endian
> format of the transmitter. byteB may be first but last. (Don't try to
> understand that. Endian problems are difficult.)

uint16_t val;
uint8_t a;

a = val >> 8; // For most significant 8 bits of val
a = ((uint8_t*)&val)[0]; // For first byte of val

Phil





More information about the Tinyos-help mailing list