473,608 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Subroutine to determine big/little endian

RRick
463 Recognized Expert Contributor
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 2621
Banfa
9,065 Recognized Expert Moderator Expert
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 Recognized Expert Contributor
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 Recognized Expert Moderator Expert
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
29006
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 under linux debian 3.0 woody Thank you for your help.
3
5184
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 or compiler? 2. In GNU gcc, { int a = {0, 1, 2, 3, 4}; bool b; float c; for (int i = 0; i < 5; i++) {
8
27452
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
2834
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, which way is the 16bit representationof 5 stored.. i.e 15 is 0000000000001111 representation in binary right? if the memory address increases in left to right direction with each memory address being capable of holding 8bits (i.e 1 byte)..can u...
2
6543
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
3140
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
3170
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 without compression). It has it's words and dwords stored in little- endian, so I do a conversion to big-endian when reading full words or dwords. I have done this because my system is big-endian. But now... what if one compiles the library in a...
19
6919
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 #else #define TARGET_IS_BIG_ENDIAN No way or some way? TIA.
23
7027
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
8498
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8478
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8341
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6817
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3962
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4025
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2474
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1598
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.