🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #3678

Re: Help: How to determine big/little endian?

#3678
From: Pete Forman
Date: Tue, 08 Aug 1995 08:42
104 lines
3877 bytes
In article <40762c$nh7@masala.cc.uh.edu> ckc31739@Bayou.UH.EDU (chun chan) writes:
>   I would like to know how to determine a machine is big-endian or not.
> All helps are appreciated!!

Here's a bit of a program that I use to explore the characteristics of
different compilers.

>>>================================ cctest.c ===================================
#include <stdio.h>

#if defined(__STDC__)
int main(int argc, char **argv)
#else
int main(argc, argv)
    int argc;
    char **argv;
#endif
{
    int            i = -9, j = 5;
    unsigned short us = 3;
    long           zero = 0;
    union {
        unsigned long l;
        unsigned char c[4];
    } ul4;
    union {
        unsigned long l;
        unsigned char c[8];
    } ul8;

    
    if ((i / us) < zero) {
        (void) printf("ANSI C integral promotion (value-preserving)\n");
    }
    else {
        (void) printf("Old C integral promotion (unsigned-preserving)\n");
    }

    if (2147483648 > zero) {
        (void) printf("ANSI C integral constant typing rules\n");
    }
    else {
        (void) printf("Old C integral constant typing rules\n");
    }

    if (i / j == -1) {
        (void) printf("Negative integral division truncates towards zero\n");
    }
    else {
        (void) printf("Negative integral division truncates towards minus infinity\n");
    }

    (void) printf("Sizes of short, int, long are %d, %d, %d\n",
                  sizeof(short), sizeof(int), sizeof(long));

    switch (sizeof(unsigned long)) {
    case 4:
        ul4.l = 0x04030201;
        (void) printf("Byte order is %i%i%i%i", ul4.c[0], ul4.c[1], ul4.c[2], ul4.c[3]);
        if (ul4.c[0] == 4 && ul4.c[1] == 3 && ul4.c[2] == 2 && ul4.c[3] == 1) {
            (void) printf(" (big-endian)\n");
        }
        else if (ul4.c[0] == 1 && ul4.c[1] == 2 && ul4.c[2] == 3 && ul4.c[3] == 4) {
            (void) printf(" (little-endian)\n");
        }
        else if (ul4.c[0] == 3 && ul4.c[1] == 4 && ul4.c[2] == 1 && ul4.c[3] == 2) {
            (void) printf(" (pdp-endian)\n");
        }
        else {
            (void) printf("\n");
        }
        break;
        
    case 8:
        /* avoid warnings on 32 bit machines */
        ul8.l = 0x08070605;
        ul8.l = ul8.l << 16;
        ul8.l = ul8.l << 16;
        ul8.l += 0x04030201;
        (void) printf("Byte order is %i%i%i%i%i%i%i%i", ul8.c[0], ul8.c[1],
                      ul8.c[2], ul8.c[3], ul8.c[4], ul8.c[5], ul8.c[6], ul8.c[7]);
        if (ul8.c[0] == 8 && ul8.c[1] == 7 && ul8.c[2] == 6 && ul8.c[3] == 5 &&
            ul8.c[4] == 4 && ul8.c[5] == 3 && ul8.c[6] == 2 && ul8.c[7] == 1) {
            (void) printf(" (big-endian)\n");
        }
        else if (ul8.c[0] == 1 && ul8.c[1] == 2 && ul8.c[2] == 3 && ul8.c[3] == 4 &&
                 ul8.c[4] == 5 && ul8.c[5] == 6 && ul8.c[6] == 7 && ul8.c[7] == 8) {
            (void) printf(" (little-endian)\n");
        }
        else {
            (void) printf("\n");
        }
        break;

    default:
        (void) printf("Can't deal with sizeof(unsigned long) = %d\n",
                      sizeof(unsigned long));
    }
}
=============================================================================<<<
-- 
Pete Forman
Western Geophysical
pete.forman@bedford.waii.com
Technical Details
Message-ID: <GSEZ020.95Aug8094207@mandy.bedford.waii.com>
Path: arc1-ca.pugleaf.net!airgun.wg.waii.com!gsez020
References: <40762c$nh7@masala.cc.uh.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!airgun.wg.waii.com!gsez020
From: gsez020@mandy.bedford.waii.com (Pete Forman)
Newsgroups: comp.std.c
Subject: Re: Help: How to determine big/little endian?
Date: 08 Aug 1995 08:42:05 GMT
Organization: Western Geophysical, Div. of Western Atlas Int'l, Houston, TX
Message-ID: <GSEZ020.95Aug8094207@mandy.bedford.waii.com>
References: <40762c$nh7@masala.cc.uh.edu>
NNTP-Posting-Host: mandy.bedford.waii.com
In-reply-to: ckc31739@Bayou.UH.EDU's message of 8 Aug 1995 08:08:44 GMT