473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 23883
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

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

Similar topics

14
by: J. Campbell | last post by:
I posted a question some time back about accessing a char array as an array of words. In order not to overrun the char array, I padded it with enough 0x00 bytes to ensure that when accessed as...
5
by: Taylor Howell | last post by:
Hello all, I have a delima. I have 8 5bit numbers that I need to pack into one (or more) variables. They then must be written (exactly 40bits (5Bytes)) to a file and have the ability to be put...
2
by: Steven T. Hatton | last post by:
I'm still not completely sure what's going on with C++ I/O regarding the extractors and inserters. The following document seems a bit inconsistent:...
388
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's...
26
by: Michel Rouzic | last post by:
I have a binary file used to store the values of variables in order to use them again. I easily know whether the file exists or not, but the problem is, in case the program has been earlier...
3
by: Pablo Gutierrez | last post by:
I have a C# method that reads Binary data (BLOB type) from a database and returns the data an array of bytes (i.e byte outbyte = new byte;). The BLOB column is saved into the database by a C...
134
by: jacob navia | last post by:
Hi Suppose you have somewhere #define BOOL int and somewhere else typedef BOOL int;
5
by: Peter Hansen | last post by:
I'm investigating a puzzling problem involving an attempt to generate a constant containing an (IEEE 754) "infinity" value. (I understand that special float values are a "platform-dependent...
27
by: duli | last post by:
Hi: I would like recommendations for books (in any language, not necessarily C++, C, python) which have walkthroughs for developing a big software project ? So starting from inception, problem...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
0
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...
0
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...

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.