Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How does C handle issues arising out of Endianness?

Reply
Thread Tools

How does C handle issues arising out of Endianness?

 
 
Indian.croesus@gmail.com
Guest
Posts: n/a
 
      12-17-2006
Hi,
If I am right Endianness is CPU related. I do not know if the
question is right in itself but if it is then how does C handle issues
arising out of Endianness.

I understand that if we pass structures using sockets across platforms,
we need to take care of Endianness issues at the application level. But
for example, for the code using bitwise AND to figure out if a number
is odd or even, how does C know the LSB position?

Thanks,
IC

 
Reply With Quote
 
 
 
 
Tim Prince
Guest
Posts: n/a
 
      12-17-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi,
> If I am right Endianness is CPU related. I do not know if the
> question is right in itself but if it is then how does C handle issues
> arising out of Endianness.
>
> I understand that if we pass structures using sockets across platforms,
> we need to take care of Endianness issues at the application level. But
> for example, for the code using bitwise AND to figure out if a number
> is odd or even, how does C know the LSB position?
>

C relies on the implementor to define each operator for each native data
type for each platform. For an example, you could look up the
gcc/config/*/*.md (machine description) files.
Standard C has rules against data type punning under which your odd/even
code would break with a change of endianness. C can't necessarily
prevent you from breaking those rules.
 
Reply With Quote
 
 
 
 
Indian.croesus@gmail.com
Guest
Posts: n/a
 
      12-17-2006
Thanks. I will check it out.

> C relies on the implementor to define each operator for each native data
> type for each platform.


So why does it not do the same with structs? Why should the programmer
take care of it while passing it across platforms? Is it more of a
"rationale" related question?

Thanks,
IC

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      12-17-2006
(E-Mail Removed) wrote:
> Hi,
> If I am right Endianness is CPU related. I do not know if the
> question is right in itself but if it is then how does C handle issues
> arising out of Endianness.


By ignoring them.

> I understand that if we pass structures using sockets across platforms,
> we need to take care of Endianness issues at the application level. But
> for example, for the code using bitwise AND to figure out if a number
> is odd or even, how does C know the LSB position?


On any particular implementation, the LSB of the unknown
value being tested is in the same position as the LSB of the
constant 1 you are ANDing with it. Problem solved.

Problems can occur when you exchange data between dissimilar
implementations, because they may disagree about endianness. They
may disagree about other matters of representation, too: one
platform might represent an int with sixteen bits while the other
uses thirty-two, one might use IEEE floating-point while the other
uses the S/360 format, the two might insert padding in structures
differently, and so on. Endianness is just one of a number of
representational issues you must consider when communicating
between different systems.

One approach that has proven widely useful is to invent a
"wire format" for the data to be exchanged, a format that does
not depend on the peculiarities of the machines. Each machine
then needs two routines: One to read "wire format" and convert
it to native representation, and one to convert the native form
to "wire format." For obvious reasons, many extrememly popular
"wire formats" use textual representations: If you want to send
the value forty-two, you transmit the two characters '4' and '2',
possibly followed by a delimiter like '\n' or ';' or some such.
This doesn't solve every possible problem (because the encoding
of characters can also vary from machine to machine), but it solves
a great many of them and usually leaves a fairly tractable remnant
to deal with.

--
Eric Sosman
(E-Mail Removed)lid
 
Reply With Quote
 
Indian.croesus@gmail.com
Guest
Posts: n/a
 
      12-17-2006

> > I understand that if we pass structures using sockets across platforms,
> > we need to take care of Endianness issues at the application level. But
> > for example, for the code using bitwise AND to figure out if a number
> > is odd or even, how does C know the LSB position?

>
> On any particular implementation, the LSB of the unknown
> value being tested is in the same position as the LSB of the
> constant 1 you are ANDing with it. Problem solved.


Thanks. Now that you have explained it that was pretty stupid of me.

Are shift operators better examples of the question I have?

As in the following snippet (please do let me know if I need to follow
any norms while adding code snippets.)
-------
int x = 10;
int y;

y = x << 2;
-------

Thanks,
IC

 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      12-17-2006
(E-Mail Removed) wrote:

> Hi,
> If I am right Endianness is CPU related. I do not know if the
> question is right in itself but if it is then how does C handle issues
> arising out of Endianness.
>
> I understand that if we pass structures using sockets across
> platforms, we need to take care of Endianness issues at the
> application level. But for example, for the code using bitwise AND to
> figure out if a number is odd or even, how does C know the LSB
> position?


C doesn't, but the implementation creator did.




Brian
 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      12-17-2006



<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
>
>> > I understand that if we pass structures using sockets across platforms,
>> > we need to take care of Endianness issues at the application level. But
>> > for example, for the code using bitwise AND to figure out if a number
>> > is odd or even, how does C know the LSB position?

>>
>> On any particular implementation, the LSB of the unknown
>> value being tested is in the same position as the LSB of the
>> constant 1 you are ANDing with it. Problem solved.

>
> Thanks. Now that you have explained it that was pretty stupid of me.
>
> Are shift operators better examples of the question I have?
>
> As in the following snippet (please do let me know if I need to follow
> any norms while adding code snippets.)
> -------
> int x = 10;
> int y;
>
> y = x << 2;
>

The shift operator assumes that the bits are arrayed from left to right,
with the most significant at the left.
This may or may not have anything to do with the physical location of the
bits in memory. *(unsigned char *)x; will read the top byte of x, which is
probably either 10 or zero, but could be anything.
--
www.personal.leeds.ac.uk/~bgy1mm
freeware games to download.


 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      12-17-2006
Malcolm wrote:

> *(unsigned char *)x; will read the top byte of x


.... if "top" means "lowest addressed"

--
pete
 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      12-17-2006



"pete" <(E-Mail Removed)> wrote in message
> Malcolm wrote:
>
>> *(unsigned char *)x; will read the top byte of x

>
> ... if "top" means "lowest addressed"
>

If Microsoft take over the world they might make us all store out bytes at
the little end.
--
www.personal.leeds.ac.uk/~bgy1mm
freeware games to download.


 
Reply With Quote
 
sam_cit@yahoo.co.in
Guest
Posts: n/a
 
      12-17-2006

Just to add, as to how to determine the nature of Endianness,

#define LITTLE_ENDIAN 0
#define BIG_ENDIAN 1
int machineEndianness()
{
int i = 1;
char *p = (char *) &i;
if (p[0] == 1) // Lowest address contains the least significant
byte
return BIG_ENDIAN;
else
return LITTLE_ENDIAN;
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Problems Arising When Migrating to New Server Will Chamberlain ASP .Net 2 10-06-2005 05:36 PM
Re: Problem arising from Outlook Jonathan N. Little HTML 3 06-12-2005 04:34 PM
Re: Problem arising from Outlook Toby Inkster HTML 0 06-12-2005 07:49 AM
File Handle Reading Blues: Rereading a File Handle for Input Dietrich Perl 1 07-22-2004 10:02 AM



Advertisments