comp.lang.ada
 help / color / mirror / Atom feed
* Ada95 - Determining Machine Byte Order
@ 1995-02-02  4:47 Timothy Halloran
       [not found] ` <3gvudv$k4b@liberator.et.tudelft.nl>
  0 siblings, 1 reply; 3+ messages in thread
From: Timothy Halloran @ 1995-02-02  4:47 UTC (permalink / raw)


Is there a way in Ada95 (i.e. in package System, Ada, or Standard) to
determine the byte ordering of a machine (big endian, little endian)?

Byte swapping Ada programmer,

Tim Halloran



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Ada95 - Determining Machine Byte Order
       [not found] ` <3gvudv$k4b@liberator.et.tudelft.nl>
@ 1995-02-06  3:34   ` Robert Dewar
  1995-02-08 12:36     ` Mark de Rooi
  0 siblings, 1 reply; 3+ messages in thread
From: Robert Dewar @ 1995-02-06  3:34 UTC (permalink / raw)


Not only is Mark's solution:

        int n = 1, little_endian = (*((char *)&n) == n);

not very clear (though if you study it for a second and can find your
way around * and & in C, you can see how it works), but more significantly
there is no guarantee that this will work, it is equivalent to the use of
unchecked conversion in Ada, and equally problematic. Mark if you don't
believe me, try seeing what this gives on a Cray, or if you want to take
the theoretical approach, tell me where in the ANSI standard it is written
that this will do something well defined.

All in all I think that the Ada way of doing things:

   Little_Endian : constant Boolean :=
        System.Default_Bit_Order = System.Low_Order_First;

maybe just a *little* bit clearer :-)





^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Ada95 - Determining Machine Byte Order
  1995-02-06  3:34   ` Robert Dewar
@ 1995-02-08 12:36     ` Mark de Rooi
  0 siblings, 0 replies; 3+ messages in thread
From: Mark de Rooi @ 1995-02-08 12:36 UTC (permalink / raw)


dewar@cs.nyu.edu (Robert Dewar) writes:

>Not only is Mark's solution:

>        int n = 1, little_endian = (*((char *)&n) == n);

>not very clear (though if you study it for a second and can find your
>way around * and & in C, you can see how it works), but more significantly
>there is no guarantee that this will work, it is equivalent to the use of
>unchecked conversion in Ada, and equally problematic. Mark if you don't
>believe me, try seeing what this gives on a Cray, or if you want to take
>the theoretical approach, tell me where in the ANSI standard it is written
>that this will do something well defined.

Unfortunately, I don't have a Cray handy, but I will take your word for
it that my suggestion won't work on one. As I mentioned in my followup,
it is required that sizeof(char) < sizeof(int) for this to work.
Is sizeof(char) == sizeof(int) for the compiler you use on your Cray?
I am curious as to the reason my (admittedly not very clear) solution
doesn't work on a Cray.

As for the ANSI C standard, well, _of course_ casting a pointer-to-int
to a pointer-to-char and then dereferencing it is machine- and compiler-
dependent. If it weren't, there would be no point to my obscure piece of
code!

Note that I specifically mentioned that it wouldn't work on *every*
machine. It works on every PC, Mac and Unix workstation I ever used,
but not on some DSPs, and apparently, on a Cray. I wasn't aware of the
Ada Way (tm) to handle this, so I just gave a quick fix to the guy who
asked, so he could get on with his work.


>All in all I think that the Ada way of doing things:

>   Little_Endian : constant Boolean :=
>        System.Default_Bit_Order = System.Low_Order_First;

>maybe just a *little* bit clearer :-)

No argument there ;-)

Regards,

Mark





^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~1995-02-08 12:36 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1995-02-02  4:47 Ada95 - Determining Machine Byte Order Timothy Halloran
     [not found] ` <3gvudv$k4b@liberator.et.tudelft.nl>
1995-02-06  3:34   ` Robert Dewar
1995-02-08 12:36     ` Mark de Rooi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox