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

compose a Long of two Ints

Hello Forum,
I want to compose two int variables, which contains the LSW and MSW, respective, into one unsigned long variable.
The following code does the right thing:

int lo, hi;
unsigned long longvar;
longvar = (unsigned long) (unsigned int) hi << 16 | (unsigned long) (unsigned int) lo;
But the compiled code is rather complicated. Are there possiblities to do this more straightforward (beside the use of an union)?
Something like
longvar.msw = hi;
longvar.lsw = lo;
?
Thanks for all hints and tips - Peter

Parents
  • Hello,
    sorry - I've said "thanks" somewhere in the thread to say "thanks" to all posters, regardless I used they suggestions or not.
    The reason why I wont use unions is the already meanted little/big endian problem. I use a solution that depends on the ordering - that's ok because the ordering doesn't change.
    In general, I expect the ordering wont change at runtime. Therefore, to get the right solution, the ordering must be known at compiletime.
    The reason why I'd asked here is that I supposed there is a solution that uses the same .c input, but gets different output, based on the compilers knowledge on byte/word ordering; without forcing me to know about the ordering.
    As a conclusion, I'm still using the solution that depends on the ordering...

    - Peter

    Sorry if there is any strange language, but I'm not a native speaker.

Reply
  • Hello,
    sorry - I've said "thanks" somewhere in the thread to say "thanks" to all posters, regardless I used they suggestions or not.
    The reason why I wont use unions is the already meanted little/big endian problem. I use a solution that depends on the ordering - that's ok because the ordering doesn't change.
    In general, I expect the ordering wont change at runtime. Therefore, to get the right solution, the ordering must be known at compiletime.
    The reason why I'd asked here is that I supposed there is a solution that uses the same .c input, but gets different output, based on the compilers knowledge on byte/word ordering; without forcing me to know about the ordering.
    As a conclusion, I'm still using the solution that depends on the ordering...

    - Peter

    Sorry if there is any strange language, but I'm not a native speaker.

Children
No data