Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
03/26/08 18:51
Read: times


 
#152579 - helpful!
Responding to: ???'s previous message
Frieder,

This bit of code ...
union
{
    unsigned long a;
    struct
    {
        // is this your endianness?
        unsigned char b0;
        unsigned char b1;
        unsigned char b2;
        unsigned char b3;
    } b;
} myvar;
is something that I can use in a current project.

For some reason, I always forget about unions.

I'm adapting my IR volume control project to use an Apple Remote (the little stick-of-gum thing), and that remote uses the NEC format for data. Since there are four bytes, I have a little state machine that keeps track of which byte the incoming data bits get shifted into.

Using the union, the incoming bits all shift into the unsigned long member a, and the methods that actually use the data can look at the unsigned chars.

I'll look at the generated code to see which is bigger. I suspect that it'll end up as a wash; shifting a 32-bit value in an 8-bit machine is what it is, but that code may still be smaller than that which detects which byte to shift into.

-a

PS: Once I clean up the NEC IR data decode program, I'll post it.

List of 7 messages in thread
TopicAuthorDate
24 bit number comparison in a 8 bit mc            01/01/70 00:00      
   assembler or C            01/01/70 00:00      
   How about this? (union, C-compiler, assembler)            01/01/70 00:00      
      helpful!            01/01/70 00:00      
         OT: shifting 32 bit            01/01/70 00:00      
            re: OT: shifting 32 bit            01/01/70 00:00      
   Thanks            01/01/70 00:00      

Back to Subject List