Introduction
This article will address the problems we face, when we switch between little endian and big endian platforms.
External Binary files
If we store binary data in an external file in one platform and use the same data in another platform (with different endian-ness), then the data read back will not be the same.
Casting Issues
If you type cast one data type to another, the behavior in little endian system and big endian system will be the same.
For example see the following code,
#Make file written by boby Thomas Pazheparampil
#21-5-2006
int iTmp = 1;
if((char)iTmp)
cout<<"Not zero after casting";
else
cout<<"Zero after casting";
If we run the above piece of code in a little endian system and a big endian system, the result will be the same.
<v:shapetype id="_x0000_t63" coordsize="21600,21600" o:spt="63" path="wr0,,21600,21600@15@16@17@18l@21@22xe" adj="1350,25920"><v:stroke joinstyle="miter"><v:formulas><v:f eqn="val #0"><v:f eqn="val #1"><v:f eqn="sum 10800 0 #0"><v:f eqn="sum 10800 0 #1"><v:f eqn="atan2 @2 @3"><v:f eqn="sumangle @4 11 0"><v:f eqn="sumangle @4 0 11"><v:f eqn="cos 10800 @4"><v:f eqn="sin 10800 @4"><v:f eqn="cos 10800 @5"><v:f eqn="sin 10800 @5"><v:f eqn="cos 10800 @6"><v:f eqn="sin 10800 @6"><v:f eqn="sum 10800 0 @7"><v:f eqn="sum 10800 0 @8"><v:f eqn="sum 10800 0 @9"><v:f eqn="sum 10800 0 @10"><v:f eqn="sum 10800 0 @11"><v:f eqn="sum 10800 0 @12"><v:f eqn="mod @2 @3 0"><v:f eqn="sum @19 0 10800"><v:f eqn="if @20 #0 @13"><v:f eqn="if @20 #1 @14"><v:path o:connecttype="custom" textboxrect="3163,3163,18437,18437" o:connectlocs="10800,0;3163,3163;0,10800;3163,18437;10800,21600;18437,18437;21600,10800;18437,3163;@21,@22"><v:handles><v:h position="#0,#1"><v:shape id="_x0000_s1026" style="MARGIN-TOP: 4.55pt; Z-INDEX: 1; LEFT: 0px; MARGIN-LEFT: 378pt; WIDTH: 90pt; POSITION: absolute; HEIGHT: 36pt; TEXT-ALIGN: left" type="#_x0000_t63" adj="-10260,35460" fillcolor="yellow"><v:textbox>
In a Big endian system, the data will look like this.
0x11111111 0x11111112 0x11111113 0x11111114
In a little endian system, the data will look like this.
0x11111111 0x11111112 0x11111113 0x11111114
<v:shape id="_x0000_s1027" style="MARGIN-TOP: 2.05pt; Z-INDEX: 2; LEFT: 0px; MARGIN-LEFT: 117pt; WIDTH: 90pt; POSITION: absolute; HEIGHT: 36pt; TEXT-ALIGN: left" type="#_x0000_t63" adj="-13320,-11340" fillcolor="yellow"><v:textbox>
The casting in the above case (char)iTmp will give you the value true in both the cases. The reason for this is, in both the cases, compiler knows the fact that the data type he is going to cast is integer. That means he has to cast an integer (4 byte long for 32 bit systems) to a character (1 byte long). So he will take least significant byte in both the cases and assign the value to the character variable created. That means the value 1 will be assigned to the character in both cases. No issues in porting the code from one system to another. If you agree with me, look at the code below.
€
#Make file written by boby Thomas Pazheparampil
#21-5-2006
int iTmp = 1;
if(*(char *)&iTmp)
cout<<"Not zero after casting";
else
cout<<"Zero after casting";
If we run the above piece of code in a little endian system and a big endian system, the result will be different.
Just watch the operations going on here.
#Make file written by boby Thomas Pazheparampil
#21-5-2006
&iTmp -> Get reference to iTmp. It’s a pointer to the address 0x11111111.
(char *)&iTmp -> Cast the pointer to a pointer of type char. Now we have a character pointer pointing to 0x11111111.
*(char *)&iTmp -> Get the content of the character pointer pointing to 0x11111111.
Now you know, the result will be different in both the platforms. In little endian system, the value will be 01 since the pointer is pointing to the location 0x11111111. In the case of big endian, the pointer is pointing to 0x11111111. But the value is 0 in this case.
Conclusion
If there is casting of one pointer type to another, the behavior will be different in the case of little endian system and big endian system. This can lead to difference in behavior when we move from one platform to another.