473,382 Members | 1,611 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 1935
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: hicham | last post by:
Hi, I am looking for help, i would like to know how can i use the endian.h and config.h to convert compiled files under solaris from BIG-ENDIAN to compiled files LITTLE-ENDIAN. I am working...
3
by: gary | last post by:
Hi, 1. About all C/C++ compilers, Does stack increase from high address to low address and heap grow increase from low to high? What on earth decides their increase direction, CPU architecture, OS...
8
by: Perception | last post by:
Hello all, If I have a C-like data structure such that struct Data { int a; //16-bit value char; //3 ASCII characters int b; //32-bit value int c; //24-bit value }
11
by: Steve | last post by:
Hi, i know this is an old question (sorry) but its a different problem, i need to write a binary file as follows 00000011 00000000 00000000 00000101 00000000 11111111
2
by: Mehta Shailendrakumar | last post by:
Hi, I am sending this question again as new question rather than reply to old question Please refer below: struct raw_data { unsigned char x; unsigned char y; }; union full_data
2
by: bhatia | last post by:
Hello all, If I have a C-like data structure such that struct Data { int a; //16-bit value char; //3 ASCII characters int b; //32-bit value int c; //24-bit value }
3
RRick
by: RRick | last post by:
This was a question that showed up in a job interview once. (And to answer your next question: No, I didn't :)) Write a subroutine that returns a bool on whether a system supports big endian...
6
by: Javier | last post by:
Hello people, I'm recoding a library that made a few months ago, and now that I'm reading what I wrote I have some questions. My program reads black and white images from a bitmap (BMP 24bpp...
23
by: guthena | last post by:
Write a small C program to determine whether a machine's type is little-endian or big-endian.
23
by: Niranjan | last post by:
I have this program : void main() { int i=1; if((*(char*)&i)==1) printf("The machine is little endian."); else printf("The machine is big endian."); }
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.