This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Efficiently combining bytes into a long int

Hi,

I'm having problems assembling a set of 4 bytes into a long int. My approach has been this:

    ulong temp;
    temp = (temp << SHIFT_BYTE) | getByte();
    temp = (temp << SHIFT_BYTE) | getByte();
    temp = (temp << SHIFT_BYTE) | getByte();
    temp = (temp << SHIFT_BYTE) | getByte();
    return temp;

But this results in a ton of calls to the long int library shift and logical or routines, which really aren't necessary. My quick and dirty solution has been to code the function in assembly, and just put the bytes in the appropriate "temp+n" bytes that make up temp. I've tried fooling around with unions, but haven't had any luck.

Thanks,
Bob

Parents
  • I did read back a few months worth of forum discussion before posting, but I didn't search the knowledge base. Thanks for responding anyway.

    There's more Keil-specific stuff than just the byte ordering in a long. The original purpose of my post was to find out which C code the compiler converted into efficient assembly. An intelligent enough compiler should have produced the same code for both the union approach and the shift approach, in which case, I would have opted for the portable approach. In my pre-purchase testing of the Keil compiler, I had found that it generally was very good at efficient assembly code for constant math such as dividing or multiplying by constant that is a power of 2 (shifting). I guess I was just expecting too much. The Keil tools do generally do a very good job.

    Thanks,
    Bob

Reply
  • I did read back a few months worth of forum discussion before posting, but I didn't search the knowledge base. Thanks for responding anyway.

    There's more Keil-specific stuff than just the byte ordering in a long. The original purpose of my post was to find out which C code the compiler converted into efficient assembly. An intelligent enough compiler should have produced the same code for both the union approach and the shift approach, in which case, I would have opted for the portable approach. In my pre-purchase testing of the Keil compiler, I had found that it generally was very good at efficient assembly code for constant math such as dividing or multiplying by constant that is a power of 2 (shifting). I guess I was just expecting too much. The Keil tools do generally do a very good job.

    Thanks,
    Bob

Children
No data