Sign in to follow this  
asdfwe

big endian vs small endian

Recommended Posts

can someone explain the working of this extremely small code int num = 1; if(*(char *)&num == 1) ---------- Little-Endian else ----------- Big-Endian\n"); thanks

Share this post


Link to post
Share on other sites
you are setting a multi byte value to a known state. then you are converting to a single byte datatype and checking to see if the bit is still set. if it is you are on a little endian system not a big endian system.

Share this post


Link to post
Share on other sites
Lets say that an int is 32-bits (that is 4 bytes) in length, regardless of platform (I know this isn't always true, but bear with me). Now on a big endian machine, those 4 bytes are ordered in memory from least to greatest, like so: [0, 1, 2, 3]. On a little endian machine they are the other way round, like this: [3, 2, 1, 0]. There are also other endian types, but those are the most common.

That code snippet there takes advantage of this fact to determine whether the current system is little or big endian. You should read up further here

Share this post


Link to post
Share on other sites
i still do not understand what does this line doing
if( *(char *) & num == 1)

can someone explain this?

Share this post


Link to post
Share on other sites
an int is typically 32 bit or 4 bytes. The number "1" encoded as an int can be stored in memory in two ways, either like this:

00000001 00000000 00000000 00000000 (Little-endian)

or like this:

00000000 00000000 00000000 00000001 (Big-endian)

What the code you've posted does is the following:

&num - This will retrieve the address of the "num" variable
(char*) &num - This will cast the address of the "num" variable to "pointer to char"

If you wrote

char* ptr = (char*) num;

then "ptr" would point to the first byte of "num".

In the last step, the "*" (dereference) operater is used to retrieve the value of the first byte of num. If this value is "1" then the system uses little-endian, if it is "0", the system uses big-endian.

Share this post


Link to post
Share on other sites
Quote:
Original post by asdfwe
i still do not understand what does this line doing
if( *(char *) & num == 1)

can someone explain this?


It is reinterpret thingy.
"num" is an integer variable, thus a 32-bit variable (on most systems)
So, num = 0x00000001 (hexadecimal)
by doing "&num" it converts the variable from "int" to "int*"

num describes the number (num=1)
&num describes the location of the number in memory.

by doing:
(char *) &num
It tells that from the 4 bytes in memory used for the variable "num", only use the first one. (It casts the variable as a char*, instead of int*. int = 4bytes; char = 1 byte)

And finally when doing:
*(char*)&num It tells to read the content of that byte in memory. If it is set to "1" we're on a little endian machine.

This is should be the memory content in both systems:
Little endian: 00 00 00 01
Big endian: 01 00 00 00

So, by reading the first byte we can tell if it is little- of big-endian.

We can't just do
if( (char)num == 1 )
Because it will automatically convert the value if the endianess is not the one we need. So such case will always be true using that code (with this modification of course)

Hope this helps
Dark Sylinc

Share this post


Link to post
Share on other sites
Quote:
Original post by asdfwe
i still do not understand what does this line doing
if( *(char *) & num == 1)

can someone explain this?


Since num is an integer, num is also equivalent (in storage terms) to this: char num[4];

So your statement there takes the address of num ( &num ), and casts it to a pointer to char ( (char *)&num ), the first byte of which can be dereferenced ( *(char *)&num ).

It might be easier to conceptualise if you use an union:

union {
int i;
char c[4];
} num;

num.i = 1;

if (num.c[0] == 1)
; // little endian
else if (num.c[3] == 1)
; // big endian
else
; // unknown endian

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this