----- Original Message -----
From: "jagadeesh gobbur" <jgobbur@yahoo.com>
To: <uttara@yahoogroups.com>
Sent: Tuesday, April 27, 2004 9:03 AM
Subject: [UTTARA] How to Identify machine


>
> Hi Friends,
>
> Some one already raised this issue of give machine is little or big indian
> machine, just right one small program with using left shift operator. So I am
> not getting exactly, Could any one mail this problem which already discussion
> in uttara groups.
>
> Thanks in advance.
>
> --jagadeesh
>

    I think you meant "endian" by writing "indian"!

    Little endian:
            "Least significant byte of a multibyte numeric representation is
        stored in the lowest-memory address, which is the address of the data.
        PCs use this format, whether thay are running Linux or Windows."

    Big endian:
            "Within a given multi-byte numeric representation, the most
        significant byte has the lowest address (the word is stored 'big-end-
        first')."

    There are, at least, few ways to detect the endianess of a machine.
Following are two methods:

    *   This method might cause undefined behaviour; but, to learn a concept,
        this program is worth understanding.

        F:\Vijay\C> type extern.c
        #include <stdio.h>
        #include <stdlib.h>

        #include <stdio.h>
        #include <stdlib.h>

        long a;

        int main ( void )
        {
            extern void fun ();
            a = 40;
            fun ();
            if ( a == 50 )
                puts ( "Little endian" );
            else
                puts ( "Big endian" );
            return EXIT_SUCCESS;
        }
        F:\Vijay\C> type extern2.c
        extern short a;
        
        void
        fun ()
        {
            a = 50;
        }
        F:\Vijay\C> gcc extern.c extern2.c -Wall -ansi
        F:\Vijay\C> a.exe
        Little endian
        F:\Vijay\C>
        F:\Vijay\C> set PROCESSOR_ARCHITECTURE
        PROCESSOR_ARCHITECTURE=x86
        F:\Vijay\C>
        F:\Vijay\C> ver
        Microsoft Windows XP [Version 5.1.2600]


    *   The second method is:

        F:\Vijay\C> type endian.c

        #include <stdio.h>
        #include <stdlib.h>
        
        int
        main ( void )
        {
             unsigned short int_val = 0x0102;

             if ( * (char*) &int_val == 0x01 )
                 puts ( "Big Endian" );
             else
             if ( * (char*) &int_val == 0x02 )
                 puts ( "Little Endian" );
             else
                 puts ( "Unknown format" );

             return EXIT_SUCCESS;
        }

        There is one program in the Stevens also. But this one, I think, is
        simpler.  The funda of this program is quite simple:

             If the above two bytes (for that sake any number of bytes) appear
        in the memory as you can read in the program, the machine is Big Endian,
        else it should be Little Endian.  i.e, you read the word 0x0102 as 01
        and then 02 (in the natural reading order) and if it appears in memory
        as 01 and 02, the machine is Big endian.