469,323 Members | 1,567 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

How to find out if a processor is "little endian" or "big endian"

1
Hi all,

How to find out if a processor is "little endian" or "big endian" by writing C code???
Jun 5 '07 #1
11 23323
Silent1Mezzo
208 100+
Hi all,

How to find out if a processor is "little endian" or "big endian" by writing C code???
Just ask your processor which it is...wouldn't this be easiest?

Basically what you should do is research the subject, think of how you're going to code this, start coding it....THEN come back here and ask about any problems you have.

Thanks
Jun 5 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
Put a 1 in an unsigned int.

Add three bytes to the address of the int.

Assign the contents of that address to an unsigned char.

If your 1 is there, you are big endian.
Jun 5 '07 #3
RedSon
5,000 Expert 4TB
Put a 1 in an unsigned int.

Add three bytes to the address of the int.

Assign the contents of that address to an unsigned char.

If your 1 is there, you are big endian.
Explanation needed !
Jun 5 '07 #4
AdrianH
1,251 Expert 1GB
Put a 1 in an unsigned int.

Add three bytes to the address of the int.

Assign the contents of that address to an unsigned char.

If your 1 is there, you are big endian.
Not necessarily, you could just have a random 1 on the stack.

Try using a union with a 4 byte char and a long. Put 1 in the long and then check where the one showed up on the 4 byte char. Zeroth byte is little endian, third byte is big endian.

BTW, this trick only sorta works. There are processors that you can map different regions of memory to be big or little endian. The hardware does everything for you.

Also there are some processors out there that do it on a bit rather then on a byte level. I don't know which, but there is literature stating that this design exists.

To further complicate things, apparently floating point and integer endianness don't have to coincide.


Adrian
Jun 5 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
BTW, this trick only sorta works. There are processors that you can map different regions of memory to be big or little endian. The hardware does everything for you.
True. But at the byte level there is no endian-ess.

Your union solution is a good one. It is equivalent to my suggestion.

I didn't know floating point had its own endian.
Jun 5 '07 #6
AdrianH
1,251 Expert 1GB
True. But at the byte level there is no endian-ess.
Yes, usually HW does magic on what is actually stored.

Your union solution is a good one. It is equivalent to my suggestion.
Now that I think about it, your right. I think that the union is somewhat cleaner, but they are equivialant.

I didn't know floating point had its own endian.
It apperently can, I read that somewhere. Maybe on wikipidia.


Adrian
Jun 5 '07 #7
i also want the reply interms of c or c++ code format
Jun 28 '07 #8
weaknessforcats
9,208 Expert Mod 8TB
Just follow the steps I outlined. The code is simple and is the same for C and C++.
Jun 28 '07 #9
aalam
4
int a = 10;
char *ch = (char*)a;
printf("%d",*ch);

Execute this code, if output is "10" then your processor is "Little Endian" and if output is "0" then your processor is "Big Endian".
Jun 28 '07 #10
JosAH
11,448 Expert 8TB
There are a few nice macros/functions available: hton*() and ntoh*(). The following
does the job for you:

Expand|Select|Wrap|Line Numbers
  1. #define BIGENDIAN (htons(1) == 1)
  2.  
kind regards,

Jos
Jun 28 '07 #11
archonmagnus
113 100+
Here's a code that'll do it for you (for educational purposes):

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main (int argc, char *argv[])
  6. {
  7.     unsigned int i;
  8.     unsigned char *p = (unsigned char *)&i;
  9.  
  10.     cout<<"Enter an unsigned integer value: ";
  11.     cin>>i;
  12.  
  13.     cout<<"i = "<<i<<endl
  14.         <<"Comparison: &i = "<<&i<<endl
  15.         <<"            *p = "<<(int *)p<<endl<<endl
  16.         <<"Byte 1: "<<(int)p[0]<<endl
  17.         <<"Byte 2: "<<(int)p[1]<<endl
  18.         <<"Byte 3: "<<(int)p[2]<<endl
  19.         <<"Byte 4: "<<(int)p[3]<<endl;
  20.  
  21.     unsigned int foo = ((i % (1 << 24)) % (1 << 16)) % (1 << 8);
  22.  
  23.     if ((unsigned int)p[0] == foo)
  24.         cout<<endl<<"    === Little Endian ==="<<endl;
  25.     else
  26.         cout<<endl<<"    === Big Endian ==="<<endl;
  27.  
  28.     cout<<endl;
  29.  
  30.     return 0;
  31. }
  32.  
Jun 28 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

14 posts views Thread by J. Campbell | last post: by
5 posts views Thread by Taylor Howell | last post: by
2 posts views Thread by Steven T. Hatton | last post: by
388 posts views Thread by maniac | last post: by
26 posts views Thread by Michel Rouzic | last post: by
3 posts views Thread by Pablo Gutierrez | last post: by
134 posts views Thread by jacob navia | last post: by
5 posts views Thread by Peter Hansen | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.