By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

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

P: 1
Hi all,

How to find out if a processor is "little endian" or "big endian" by writing C code???
Jun 5 '07 #1
Share this Question
Share on Google+
11 Replies


100+
P: 208
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
Expert Mod 5K+
P: 9,197
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
Expert 5K+
P: 5,000
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
Expert 100+
P: 1,251
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
Expert Mod 5K+
P: 9,197
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
Expert 100+
P: 1,251
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

P: 3
i also want the reply interms of c or c++ code format
Jun 28 '07 #8

weaknessforcats
Expert Mod 5K+
P: 9,197
Just follow the steps I outlined. The code is simple and is the same for C and C++.
Jun 28 '07 #9

P: 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

Expert 10K+
P: 11,448
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
100+
P: 113
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.