470,833 Members | 1,952 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,833 developers. It's quick & easy.

little/big endian question.


Suppose I have this code:

unsigned short svalue;
unsigned char hibyte, lobyte;

svalue = 0xABCD;

hibyte = (svalue >8) & 0xFF;
lobyte = svalue & 0xFF;

Will this result in the values of hibyte and lobyte being 0xAB and 0xCD
respectively, regardless of whether the platform is little or big endian?

Thanks for your help.

Regards,
Charles Sullivan
Jun 27 '08 #1
5 1809
Charles wrote:
)
) Suppose I have this code:
)
) unsigned short svalue;
) unsigned char hibyte, lobyte;
)
) svalue = 0xABCD;
)
) hibyte = (svalue >8) & 0xFF;
) lobyte = svalue & 0xFF;
)
) Will this result in the values of hibyte and lobyte being 0xAB and 0xCD
) respectively, regardless of whether the platform is little or big endian?

Of course it will. The shift operator doesn't care about endianess.
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
Jun 27 '08 #2
On Sat, 03 May 2008 14:34:25 +0000, Willem wrote:
Charles wrote:
)
) Suppose I have this code:
)
) unsigned short svalue;
) unsigned char hibyte, lobyte;
)
) svalue = 0xABCD;
)
) hibyte = (svalue >8) & 0xFF;
) lobyte = svalue & 0xFF;
)
) Will this result in the values of hibyte and lobyte being 0xAB and
0xCD ) respectively, regardless of whether the platform is little or big
endian?

Of course it will. The shift operator doesn't care about endianess.
SaSW, Willem
Thanks Willem. That had been my understanding but some strange bug
reports from users got me wondering whether I was mistaken.

Regards,
Charles Sullivan

Jun 27 '08 #3
On May 3, 5:45 pm, Charles Sullivan <cwsul...@triad.rr.comwrote:
On Sat, 03 May 2008 14:34:25 +0000, Willem wrote:
Charles wrote:
)
) Suppose I have this code:
)
) unsigned short svalue;
) unsigned char hibyte, lobyte;
)
) svalue = 0xABCD;
)
) hibyte = (svalue >8) & 0xFF;
) lobyte = svalue & 0xFF;
)
) Will this result in the values of hibyte and lobyte being 0xAB and
0xCD ) respectively, regardless of whether the platform is little or big
endian?
Of course it will. The shift operator doesn't care about endianess.
SaSW, Willem

Thanks Willem. That had been my understanding but some strange bug
reports from users got me wondering whether I was mistaken.
Then perhaps the bug lies somewhere else?
From ISO 9899:1999, 6.5.7:
The result of E1 >E2 is E1 right-shifted E2 bit positions.
If E1 has an unsigned type or if E1 has a signed type and a
nonnegative value, the value of the result is the integral part
of the quotient of E1 / 2^(E2) . If E1 has a signed type and a
negative value, the resulting value is implementation-deļ¬ned.
(int)(0xABCD / pow(2, 8)) == 0xAB (== 171)
Jun 27 '08 #4
On Sat, 03 May 2008 15:29:24 +0100, Charles Sullivan
<cw******@triad.rr.comwrote:
hibyte = (svalue >8) & 0xFF;
What's the point of the '& 0xFF'?

--
Martin

Jun 27 '08 #5
Martin <ma***********************@which.netwrites:
On Sat, 03 May 2008 15:29:24 +0100, Charles Sullivan
<cw******@triad.rr.comwrote:
> hibyte = (svalue >8) & 0xFF;

What's the point of the '& 0xFF'?
Well, it does make a difference if unsigned char has more than 8 bits
and unsigned short has more than 16. If you've used a machine with
36, 18 and 9 bit types to implement octet-based processing, you do
this automatically.

--
Ben.
Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by hicham | last post: by
8 posts views Thread by Perception | last post: by
2 posts views Thread by Mehta Shailendrakumar | last post: by
2 posts views Thread by bhatia | last post: by
23 posts views Thread by guthena | last post: by
23 posts views Thread by Niranjan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.