??? 11/18/04 20:18 Read: times |
#81464 - Efficient method... (C question) |
I've got a situation where I need to move a single number 1-255 into the 3rd byte of a 'long' (32-bit) variable. So...
What is generally the most efficient method of doing this? The ways I'm thinking about: long target; char initial; target=initial*65536; This seems like it would be the most inefficient - but would a compiler recognize the pattern and optimize it? I guess that's probably compiler-specific... Or- long target; char initial; target = initial << 16; Probably more efficient than the previous, but once again, would the compiler recognize the pattern and just move the target into the 3rd highest byte, or would it actually to a crudload of bit shifts? or: long target; char initial; char *pointer; pointer = ⌖ pointer = pointer-2; *pointer = initial; This seems convoluted and wasteful. It adds extra commands *and* extra memory for the pointer... But if the other two aren't optimized, I'd think it would actually take much less program space... So what would be the preferred method here? Or is there a better way? Also, am I making any logical errors in my stuff? for example, I'm assuming the 3rd byte of the long is 2 bytes before the initial byte, but I might be envisioning it backwards... Not sure how C implements multi-byte variables... |
Topic | Author | Date |
Efficient method... (C question) | 01/01/70 00:00 | |
what about a union | 01/01/70 00:00 | |
Well that makes sense! | 01/01/70 00:00 | |
It's the standard way! | 01/01/70 00:00 | |
Same subject as the post I'm replying to | 01/01/70 00:00 | |
byte order | 01/01/70 00:00 | |
Thanks! :)![]() | 01/01/70 00:00 |