473,574 Members | 2,892 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Endianess: why does this code not change value on BE machine?

Ok,

I've searched this group for Big/Little endian issues, don't kill me,
I know endianess issues have been discussed a 1000 times. But my
question is a bit different:

I've seen the follwing function several times, it converts data stored
in Big Endian (BE) format into host native format (LE on LE machines,
BE on BE machines):

/* - this code swaps the bytes on a Little Endian Machine
- this code returns 'data' unmodified on a Big Endian Machine.
*/
static short getShortBE (char *data)
{
return (short) ((data[0] << 8) | data[1]);
}

I can understand why this function swaps bytes on a LE machine, but
why doesn't it alter 'data' on a BE machine? I've tried to understand
it with diagrams and everything, but my brain went just crazy! Can
anyone give me a simple explanation please? :) I'm really curious...

And how would a function look like which does the "opposite": swap
bytes on a BE machine, don't change values on a LE machine (that is,
"read and convert LE data")? :) Hope I can answer the 2nd quesion
myself when I understand the 1st question... :)
Here's the complete test program:

#include <stdlib.h>
#include <stdio.h>

/* This function is useful when reading and converting data which is
stored
in Big Endian Format.
- this code swaps the bytes on a Little Endian Machine
- this code returns 'data' unmodified on a Big Endian Machine.
*/
static short getShortBE (char *data)
{
return (short) ((data[0] << 8) | data[1]);
}

int main (int argc, char **argv)
{
int a;
char c;
short data1;
short data2;
char *d;

(void)argc;
(void)argv;

a = 0x01020304;
c = ((char *)&a)[0];

if (c == 1)
{
fprintf (stdout, "Integer a: %x - first byte: %x (MSB) -> Big
endian machine.\n", a, c);
}
else if (c == 4)
{
fprintf (stdout, "Integer a: %x - first byte: %x (LSB) -> Little
endian machine.\n", a, c);
}
else
{
fprintf (stdout, "Integer a: %x - first byte: %x -> A weirdo
machine.\n", a, c);
}

data1 = 0x0102;
d = ((char *)&data1);

fprintf (stdout, "Data[0]: %x - Data[1]: %x\n",
*d, *(d + 1));

data2 = getShortBE ((char *)&data1);

d = ((char *)&data2);
fprintf (stdout, "After GET: Data[0]: %x - Data[1]: %x\n",
*d, *(d + 1));

return 1;
}

Output:
-------

On Sun:
./Endian

Integer a: 1020304 - first byte: 1 (MSB) -> Big endian machine.
Data[0]: 1 - Data[1]: 2
After GET: Data[0]: 1 - Data[1]: 2

On Linux (i386):

Integer a: 1020304 - first byte: 4 (LSB) -> Little endian machine.
Data[0]: 2 - Data[1]: 1
After GET: Data[0]: 1 - Data[1]: 2

Thanks, Oliver
Nov 14 '05 #1
12 1629

"Oliver Knoll" <tk****@bluewin .ch> wrote

/* - this code swaps the bytes on a Little Endian Machine
- this code returns 'data' unmodified on a Big Endian Machine.
*/
static short getShortBE (char *data)
{
return (short) ((data[0] << 8) | data[1]);
}

I can understand why this function swaps bytes on a LE machine, but
why doesn't it alter 'data' on a BE machine? I've tried to understand
it with diagrams and everything, but my brain went just crazy! Can
anyone give me a simple explanation please? :) I'm really curious...
Firstly arbitrary data should be unsigned char. Plain char is for actual
text.

Don't be fooled by the << operator. This suggests that data is being shifted
"leftwards" in memory, but in fact it always moves less significant bits to
the more significant position.
Your function therefore takes an arbitrary stream of bytes, and treats the
first one as the top eight bits and the second one as the lower eight bits
of a 16-bit number.
Incidentally it will not work as expected if CHAR_BIT is not eight, which it
isn not always. short isn't necessarily sixteen bits, either.
And how would a function look like which does the "opposite": swap
bytes on a BE machine, don't change values on a LE machine (that is,
"read and convert LE data")? :) Hope I can answer the 2nd quesion
myself when I understand the 1st question... :)
So you can treat your arbitrary bit stream as little endian simply by
placing the first byte in the least-significant position, and shifting up
the second byte to the most significant position.

Nov 14 '05 #2
In article <b2************ **************@ posting.google. com>,
Oliver Knoll <tk****@bluewin .ch> wrote:
static short getShortBE (char *data)
{
return (short) ((data[0] << 8) | data[1]);
}

I can understand why this function swaps bytes on a LE machine, but
why doesn't it alter 'data' on a BE machine? I've tried to understand
it with diagrams and everything, but my brain went just crazy! Can
anyone give me a simple explanation please? :) I'm really curious...


Well, one way to see it is that this function doesn't depend on
endianness at all, so it will give the same result on a big- or
little-endian machine. But just accesing the data as a short will
give different results on the two.

-- Richard
Nov 14 '05 #3
tk****@bluewin. ch (Oliver Knoll) wrote:
/* - this code swaps the bytes on a Little Endian Machine
- this code returns 'data' unmodified on a Big Endian Machine.
*/
static short getShortBE (char *data)
{
return (short) ^^^^^^ useless cast ((data[0] << 8) | data[1]);
Implementation-defined behaviour, due to sign-extension. You should
use unsigned data types for bit manipuation, eg:

short getShortBE(void *data)
{
unsigned char *p = data;
return ( p[0] << CHAR_BIT ) | p[1] ; } I can understand why this function swaps bytes on a LE machine, but
why doesn't it alter 'data' on a BE machine?
Explain in your own words why it works on an LE machine, and
then it should be obvious why it does nothing on BE.
And how would a function look like which does the "opposite": swap
bytes on a BE machine, don't change values on a LE machine (that is,
"read and convert LE data")?


Switch the '0' with the '1'.
Nov 14 '05 #4
On 12 Sep 2004 05:00:49 -0700, tk****@bluewin. ch (Oliver Knoll) wrote:
Ok,

I've searched this group for Big/Little endian issues, don't kill me,
I know endianess issues have been discussed a 1000 times. But my
question is a bit different:

I've seen the follwing function several times,
where?
it converts data stored
in Big Endian (BE) format into host native format (LE on LE machines,
BE on BE machines):
it is wrong because if a char "x" is 8 bit then x<<8 == 0
/* - this code swaps the bytes on a Little Endian Machine
- this code returns 'data' unmodified on a Big Endian Machine.
*/
static short getShortBE (char *data)
{
return (short) ((data[0] << 8) | data[1]);
}

#include <limits.h>

/* Suppone 2*sizeof(char)= sizeof(short) */
unsigned short GetShort(unsign ed char *data)
{unsigned short u=data[0];
return (u<<CHAR_BIT)| data[1];
}
Nov 14 '05 #5

On Mon, 13 Sep 2004, RoSsIaCrIiLoIA wrote:

On 12 Sep 2004 05:00:49 -0700, tk****@bluewin. ch (Oliver Knoll) wrote:

I've seen the follwing function several times,


where?
it converts data stored
in Big Endian (BE) format into host native format (LE on LE machines,
BE on BE machines):


it is wrong because if a char "x" is 8 bit then x<<8 == 0


No, it's not. Usual arithmetic promotions apply. Stop spreading
misinformation; that's bad.

-Arthur

Nov 14 '05 #6
"Malcolm" <ma*****@55bank .freeserve.co.u k> wrote in message news:<ci******* ***@news5.svr.p ol.co.uk>...
"Oliver Knoll" <tk****@bluewin .ch> wrote

/* - this code swaps the bytes on a Little Endian Machine
- this code returns 'data' unmodified on a Big Endian Machine.
*/
static short getShortBE (char *data)
{
return (short) ((data[0] << 8) | data[1]);
}

I can understand why this function swaps bytes on a LE machine, but
why doesn't it alter 'data' on a BE machine? I've tried to understand
,,,
Firstly arbitrary data should be unsigned char. Plain char is for actual
text.


Thanks :) Good point. (In the real world I'm using such ugly stuff
like Q_UINT16 and the like though - I guess I've never seen a library
(Qt in this case) which doesn't define their own "datatypes" to ensure
correct byte-siyes ;)
Don't be fooled by the << operator. This suggests that data is being shifted
"leftwards" in memory, but in fact it always moves less significant bits to
the more significant position.
Ahh, that's exactly the explanation I was looking for, my brain was
stuck with this "bits go to the left". It makes perfectly sense now,
thanks a lot!
...
Incidentally it will not work as expected if CHAR_BIT is not eight, which it
isn not always. short isn't necessarily sixteen bits, either.


I've taken char and short and was naively assuming them to be 8 and 16
bit for illustration purposes.

Thanks, Oliver
Nov 14 '05 #7
On Mon, 13 Sep 2004 03:13:24 -0400 (EDT), "Arthur J. O'Dwyer"
<aj*@nospam.and rew.cmu.edu> wrote:
On Mon, 13 Sep 2004, RoSsIaCrIiLoIA wrote:

On 12 Sep 2004 05:00:49 -0700, tk****@bluewin. ch (Oliver Knoll) wrote:

I've seen the follwing function several times,
where?
it converts data stored
in Big Endian (BE) format into host native format (LE on LE machines,
BE on BE machines):


it is wrong because if a char "x" is 8 bit then x<<8 == 0


No, it's not. Usual arithmetic promotions apply. Stop spreading
misinformation ; that's bad.

static short getShortBE (char *data)
{
return (short) ((data[0] << 8) | data[1]);
}


where is the "arithmetic promotion"? I don't know how it can be ok
data[0] is a char and do << 8 (it seems an error to me)
then or data[1]; then the promotion to short
[if it was return (short) (data[0]<<8) | data[1]; you are right]
but (short) ((data[0]<<8) | data[1])
seems to me if *data is 8 bit = (short) (data[1])
-Arthur


Nov 14 '05 #8
On Mon, 13 Sep 2004 03:13:24 -0400 (EDT), "Arthur J. O'Dwyer"
<aj*@nospam.and rew.cmu.edu> wrote:
On Mon, 13 Sep 2004, RoSsIaCrIiLoIA wrote:

On 12 Sep 2004 05:00:49 -0700, tk****@bluewin. ch (Oliver Knoll) wrote:

I've seen the follwing function several times,
where?
it converts data stored
in Big Endian (BE) format into host native format (LE on LE machines,
BE on BE machines):


it is wrong because if a char "x" is 8 bit then x<<8 == 0


No, it's not. Usual arithmetic promotions apply. Stop spreading
misinformation ; that's bad.

static short getShortBE (char *data)
{
return (short) ((data[0] << 8) | data[1]);
}


where is the "arithmetic promotion"? I don't know how it can be ok
data[0] is a char and do << 8 (it seems an error to me)
then or data[1]; then the promotion to short
[if it was return (short) (data[0]<<8) | data[1]; you are right]
but (short) ((data[0]<<8) | data[1])
seems to me if *data is 8 bit = (short) (data[1])
-Arthur


Nov 14 '05 #9
On Mon, 13 Sep 2004 03:13:24 -0400 (EDT), "Arthur J. O'Dwyer"
<aj*@nospam.and rew.cmu.edu> wrote:
On Mon, 13 Sep 2004, RoSsIaCrIiLoIA wrote:

On 12 Sep 2004 05:00:49 -0700, tk****@bluewin. ch (Oliver Knoll) wrote:

I've seen the follwing function several times,
where?
it converts data stored
in Big Endian (BE) format into host native format (LE on LE machines,
BE on BE machines):


it is wrong because if a char "x" is 8 bit then x<<8 == 0


No, it's not. Usual arithmetic promotions apply. Stop spreading
misinformation ; that's bad.

static short getShortBE (char *data)
{
return (short) ((data[0] << 8) | data[1]);
}


where is the "arithmetic promotion"? I don't know how it can be ok
data[0] is a char and do << 8 (it seems an error to me)
then or data[1]; then the promotion to short
[if it was return (short) (data[0]<<8) | data[1]; you are right]
but (short) ((data[0]<<8) | data[1])
seems to me if *data is 8 bit = (short) (data[1])
-Arthur


Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
4843
by: Jonas | last post by:
This works fine in Win XP but does not work at all in Win 98. Private WithEvents objIExplorer As InternetExplorer I have to do it like this to get it to work in Win 98 Dim objIExplorer As InternetExplorer But then I cant see when a user exit my objIExplorer and than an error will show up when I try to open a link in the IE window that does...
4
1958
by: pellepluttrick | last post by:
Hi, I thought I understood this stuff - but... This little program (taken from W. Richard Stevens Unix Network Programming Volume 1) determines a machine's endianess: #include <iostream> using namespace std; int main()
5
5453
by: SpOiLeR | last post by:
Hi. q1: Is std::bitset<N> part of standard or it's compiler extension? q2: Is std::bitset::to_string() part of standard? q3: My documentation say this about std::bitset::to_string(): "...each character is 1 if the corresponding bit is set, and 0 if it is not. In general, character position i corresponds to bit position N - 1 -
4
3469
by: Gernot Frisch | last post by:
Hi, does the ARM processors have different endians than x86 platform? For ints and/or floating points??? I get a really strange objec when loading it from a file. Creating manually works. -- -Gernot
8
4081
by: | last post by:
Well! Maybe I wrote this word incorrect but its not included im my e-dictionary! I have a structure with many int, short etc I execute this function: fread(&my_struct, sizeof(struct), 1, filepointer); The problem is this: In different endianess platforms from same file will be loaded different data in the class.
35
5428
by: Sunil | last post by:
Hi all, I am using gcc compiler in linux.I compiled a small program int main() { printf("char : %d\n",sizeof(char)); printf("unsigned char : %d\n",sizeof(unsigned char)); printf("short : %d\n",sizeof(short)); printf("unsigned short : %d\n",sizeof(unsigned short));...
1
3151
by: mathieu | last post by:
Hello, I am working on a project where I need to serialize my objects to a stream in a mixed ASCII/binary way. Using the c++ iostream was extremely straighforward and I quickly made progress. Unfortunately I need to handle endianess (reading big endian binary file on little endian architecture and vice versa). In order to do that I copy...
2
2350
by: dougloj | last post by:
Hi. I have an ASP.NET application written in C#. To log in, a user must provide their email address and password. I already give the user a "Remember my Email Address" check box. If they check it when logging in, I store the email address in a cookie and automatically display the address when they login again. I now want to give the...
2
4998
by: StanB | last post by:
I came across this weird problem: 1. Session state stops working after the app is deployed to another server because IE does not accept cookies. 2. It works if cookieless="true" in the web.config 3. Yes, I tried IE - Tools - Privacy - Accept All Cookies and also Override automatic cookie handling, Always allow session cookes
0
7835
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main...
0
7753
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8095
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8265
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7847
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8132
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5332
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3787
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2265
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.