1
\$\begingroup\$

I'm using the C18 compiler and need to get the three bytes of a short long from MSB to LSB. I'd say I could use this:

void theFunction(unsigned short long input) {
    doSomething((input>>16) & 0xff);
    doSomething((input>>8) & 0xff);
    doSomething(input & 0xff);
}

But now my code isn't working (does compile, but doesn't run as expected). I do not know where the problem is, so can anyone confirm that this code works?

\$\endgroup\$
  • \$\begingroup\$ a byte is 0xff long not 0x0f. Anyways, this is a nice example where it comes in handy to look at the generated assembly code. \$\endgroup\$ – jippie Apr 13 '13 at 15:22
  • \$\begingroup\$ @jippie thanks, miscopied that from my code. Fixed now. \$\endgroup\$ – user17592 Apr 13 '13 at 15:22
  • \$\begingroup\$ @Camil You're right, my bad. \$\endgroup\$ – m.Alin Apr 13 '13 at 15:22
  • \$\begingroup\$ The code calls doSomething three times, first with the highest byte, second with the middle byte, and third with the lowest byte. \$\endgroup\$ – Pete Becker Apr 13 '13 at 15:25
  • \$\begingroup\$ @PeteBecker thanks, that's what I wanted to hear - can you make it an answer? \$\endgroup\$ – user17592 Apr 13 '13 at 15:26
1
\$\begingroup\$

The code calls doSomething three times, first with the highest byte, second with the middle byte, and third with the lowest byte.

|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy