473,386 Members | 1,846 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,386 software developers and data experts.

Subroutine to determine big/little endian

RRick
463 Expert 256MB
This was a question that showed up in a job interview once. (And to answer your next question: No, I didn't :))

Write a subroutine that returns a bool on whether a system supports big endian numbers (true) or little endian (false). I tried something like the following, but never did find out if it was correct.

There are two assumptions here: First, big endian direction from small to large is ordered like a string. Second, endian is byte based. On a PC, I get a true value. Does anyone know what goes on with the PC architecture?

Expand|Select|Wrap|Line Numbers
  1. bool isBigEndian( void)
  2. {
  3.     union Endian
  4.     {
  5.         int iValue;
  6.         char bValue;
  7.     };
  8.  
  9.     Endian endian;
  10.     endian.iValue = 1;
  11.     return endian.bValue;
  12. }
  13.  
Feb 16 '07 #1
3 2600
Banfa
9,065 Expert Mod 8TB
On a PC, I get a true value. Does anyone know what goes on with the PC architecture?
Depends on the processor but Intel is little endian.

Your code returns the direct inverse of what it says it returns. In big endian the most significant byte comes first in memory (in the lowest location) and your most significant byte will be 0 for a value of 1 so bValue will be 1 on a little endian system.
Feb 16 '07 #2
RRick
463 Expert 256MB
Does big/little endian always occur at the byte level? I thought I heard of endian also occuring on the 16-bit work level.
Feb 17 '07 #3
Banfa
9,065 Expert Mod 8TB
The thing to remember is that there are more than 2 ways of byte ordering. It is just that most significant to least significant and least significant to most significant are the most common 2 ways.

If you think about it if I have a 4 byte value (DWORD) calling the least significant byte byte 1 and and most significant byte 4 then in the memory map of a little endian processor they will appear

1 2 3 4

on a big endian prcessor it will be

4 3 2 1

not only are the bytes swapped but the words are swapped as well.
Feb 18 '07 #4

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

Similar topics

2
by: hicham | last post by:
Hi, I am looking for help, i would like to know how can i use the endian.h and config.h to convert compiled files under solaris from BIG-ENDIAN to compiled files LITTLE-ENDIAN. I am working...
3
by: gary | last post by:
Hi, 1. About all C/C++ compilers, Does stack increase from high address to low address and heap grow increase from low to high? What on earth decides their increase direction, CPU architecture, OS...
8
by: Perception | last post by:
Hello all, If I have a C-like data structure such that struct Data { int a; //16-bit value char; //3 ASCII characters int b; //32-bit value int c; //24-bit value }
18
by: Abhishek | last post by:
lets consider that I have defined an integer like this. int a=5; now taking the fact that an integer is allocated 2 bytes in memory and the memory address increases in the left to right direction,...
2
by: bhatia | last post by:
Hello all, If I have a C-like data structure such that struct Data { int a; //16-bit value char; //3 ASCII characters int b; //32-bit value int c; //24-bit value }
5
by: manishster | last post by:
I keep getting the following in my output file , regardless of whether I convert endian-ness or not . How do i get "01 02 03 04" .... Mahamannu output : 04 03 02 01 b0 00 00
6
by: Javier | last post by:
Hello people, I'm recoding a library that made a few months ago, and now that I'm reading what I wrote I have some questions. My program reads black and white images from a bitmap (BMP 24bpp...
19
by: perry.yuan | last post by:
How could I determine the endianness of my compile environment at compile time, instead of run time? I need a macro ("some_expression"), i.e. #if some_expression #define TARGET_IS_LITTLE_ENDIAN...
23
by: Niranjan | last post by:
I have this program : void main() { int i=1; if((*(char*)&i)==1) printf("The machine is little endian."); else printf("The machine is big endian."); }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.