LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-16-2006, 08:52 AM   #1
indian
Member
 
Registered: Aug 2004
Posts: 137

Rep: Reputation: 15
problem in understanding little endian/big endian machine program


Hi, I saw a program in C which tells whether the machine is little endian or big endian but I amnot able to understand it.

Code:
#define LITTLE_ENDIAN 0
#define BIG_ENDIAN    1

int machineEndianness()
{
   int i = 1;
   char *p = (char *) &i;
   if (p[0] == 1) // Lowest address contains the least significant byte
      return LITTLE_ENDIAN;
   else
      return BIG_ENDIAN;
}
I am not able to understand the colored line. the code is here..
http://www.answers.com/topic/endianness

I also got one more code doing same thing which too Iam not able to understand.

Code:
#include <stdio.h>
main ()
{
   union
   {
      char c[4];
      int i;
   } stuff;

   stuff.i = 0;
   stuff.c[0] = 1;

   stuff.i <<= 8;

   if (stuff.i) printf("little endian\n");
   else printf("big endian\n");

}
I will really appreciate if someone can explain me what is happening.

Regards
 
Old 03-16-2006, 09:30 AM   #2
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
I'll try to explain the best I can:

On computers, integers are stored in (machine) words, units of four bytes.

Code:
+--+--+--+--+
|b1|b2|b3|b4|
+--+--+--+--+
(where each byte is 8 bits).

On little-endian machines, the least significant bits (that is, the one that code for 1, 2, 4, 8, ...) are in b1, whereas on big-endian machines the least significant bits are in b4.

The code assumes that the address of i is the same as the address of b1. So if you interpret &b1 as a char pointer, you're going to see b1 instead of (b1,b2,b3,b4). So by looking at b1, the code can detect whether the machine is little or big endian, by seeing if b1 is zero or not.
 
Old 04-19-2006, 11:47 AM   #3
tuxombie
Member
 
Registered: Oct 2004
Location: Chennai, India
Distribution: Slackware 10, Ubuntu, Debian Sarge
Posts: 53

Rep: Reputation: 15
hi

yeah in big endian the bytes are ordered differently. i ll try speculating on the first code bit here.

int i = 1;
char *p = (char *) &i;
if (p[0] == 1) // Lowest address contains the least significant byte
return LITTLE_ENDIAN;
else
return BIG_ENDIAN;

here we have the integer 1. in binary,this would be 01.

the least significant digit is 1 here. in little endian computers, we would read this as our first char.

a[0]=1, a[1]=0.

in big endian computers we will have

a[0]=0, a[1]=1.

This was a very nice post. I should have looked up lq before tearing myself apart with this endianness problem a few weeks ago . anyway, i am going to start a thread explaining my difficulties and how it has been overcome.

Last edited by tuxombie; 04-19-2006 at 11:50 AM.
 
Old 04-19-2006, 12:21 PM   #4
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
You are correct in what you are saying but the following can be taken the wrong way.

Quote:
...here we have the integer 1. in binary,this would be 01.

the least significant digit is 1 here. in little endian computers, we would read this as our first char.

a[0]=1, a[1]=0.

in big endian computers we will have

a[0]=0, a[1]=1.
Just so this doesn't confuse anybody who could be mistaken as if each bit is a char. Your example of the binary number 01 and then using chars which are 0 and 1 could have been clearer using another number something like 256 etc as this exstends beyond the first byte.

In addittion you should point out that your ints are 16bit ints.

Last edited by dmail; 04-19-2006 at 12:23 PM.
 
Old 04-19-2006, 01:27 PM   #5
MichaelZ
Member
 
Registered: Mar 2006
Location: Austria
Distribution: Ubuntu Breezy 5.10
Posts: 32

Rep: Reputation: 15
Hello,

Just a couple of documents that could be useful to better understand the little/big endian concept:

http://en.wikipedia.org/wiki/Endianness
http://www.webopedia.com/TERM/b/big_endian.html
http://www.noveltheory.com/TechPapers/endian.asp

Best wishes,
Michael
 
Old 04-19-2006, 02:42 PM   #6
tuxombie
Member
 
Registered: Oct 2004
Location: Chennai, India
Distribution: Slackware 10, Ubuntu, Debian Sarge
Posts: 53

Rep: Reputation: 15
[QUOTE=dmail]You are correct in what you are saying but the following can be taken the wrong way.


Yeah I know, and I am not too familiar with it myself, which was why I said 'speculate'. But the idea is that.

Two weeks ago, I had to transfer a bunch of binary files from my office Desktop to the super-computer where I run simulations on flames. Unfortunately, I have been here only a few months, and there is very little documentation explaining this problem. It is mentioned, but in a very rudimentary way, but there is little that tells how to solve it.

Now, as these binary input files exist on an intel comp, where presumably simulations had been run before, they were in little endian form. It took me a while to even discover the problem.

I guess, modifying a bit of code is the way to go, so that the bytes can be swapped there, but I needed (and still need!) quick results. Furthermore, several of these compilers have this option to give it as an option in the compiler flags. But the stupid IBM mplxf95 compiler doesn't.

I resolved the problem with the intel ifort compilers, which have this option. I very strongly recommend it for anyone doing fortran stuff. But there are catches which depend on the system, such as defining the type; and it took me a while and some trial and error and reading the manpages to get this to work.

real*8:: time, time_save, tstep
real*8,dimension(1216,244,1,5) :: yspecies
real*8, dimension(1216,244,1) :: temp, pressure

The reals in IBM are real*8. Just real will not work. The reader is referred to the intel fortran man pages on the details of achieving the conversion. Anyway, the story is to read the data with a small code as little endian and write it as big endian.
 
Old 04-19-2006, 02:50 PM   #7
tuxombie
Member
 
Registered: Oct 2004
Location: Chennai, India
Distribution: Slackware 10, Ubuntu, Debian Sarge
Posts: 53

Rep: Reputation: 15
I may also add that the icc and ifort compilers install 'easily' only for the rpm linii. I have Debian in the office computer and it was very difficult to install it. The conversion was done in an Suse machine.

We can make it install on a debian machine, but it is slightly non-trivial. There are posts explaining this (I am too lazy to search at the moment and put the links here).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
big endian little endian? blackzone Linux - Hardware 4 09-23-2004 06:04 AM
What is all this big endian-little endian stuff about? vdemuth Linux - Newbie 1 04-28-2004 02:16 AM
YMF724F PPC Big-Endian ravvar Linux - Hardware 0 10-25-2003 04:01 AM
ALSA 0.9.8 YMF724F PPC Big Endian ravvar Linux - Hardware 0 10-24-2003 09:59 AM
simple program big problem antken Programming 3 09-15-2002 10:39 AM


All times are GMT -5. The time now is 03:56 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration