I know that longs are stored MSB first in memory. I need to access individual bytes of a long in order to send them out the SPI port. There are a number of ways to do this, I'd like your opinion on which approach might be best (from whatever perspective). Two that come to mind is to simply declare the variable as a long pointer and then access each byte using the pointer. Pretty clean as far as I am concerned, but don't know if there's a better C51 way to handle this task. Another --more convoluted, maybe even ugly-- is to avoid direct memory access and simply rotate/mask the long and cast into an unsigned char four times to grab each byte. Ugly at best. Thanks, -Martin
While Rotate and Mask is portable, It will most likely generate a lot of code. You have Pointers. You could use a union instead. The Best way. Try both and look at the generated code. For both size and speed, depending on which is more important to you.
I should have also mentioned that some of these unsigned longs will be arguments passed to functions. It would seem to me that using pointer addressing might be the easiest way to access the individual bytes in the long.
some of these unsigned longs will be arguments passed to functions That's actually an argument against using pointer fudging to access individual bytes --- if they're function arguments, they'll have arrived in single-byte registers. There is thus not really a contiguous ordinary memory location where a pointer could point at them. [Well, strictly spoken there is, but it's the IDATA equivalent of absolute register access. I wouldn't bet on the compiler figuring that out anywhere near automatically]
someone mentioned "portable" While I believe that "portable" is an illusion in small embedded (did you ever visualize converting a '51 product to PIC or vv?) if you have to "maintain portability" make sure the routines that depend on the byte sequence of longs, ints etc are enclosed in #ifs Erik
View all questions in Keil forum