|
Prev: Instruction Reduction
Next: The Case For Biquinary
From: Motaz K. Saad on 2 Mar 2005 13:34 hi every body i wrote a c languge program ti determine ur machine whether big-edian or little-endian, and i tried it on my machine (little-endian machine), can any one please try it on big-edian machine and email me the result. thanx. here is the program: /****************************************************************/ /*************************************************************************** * Copyright (C) 2005 by Motaz K. Saad * * motsad(a)yahoo.com * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ /*This program is valid for 32bit machine*/ /*You can adapt it for 64bit machine*/ #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { unsigned int var = 0xff333300;//declare & intialize unsigned 32bit integer unsigned char * cPtr;//declare an unsigned byte (8bit) void * ptr; /******************************************************************/ //test if the machine is 32bit if ( sizeof(int) != 4) { printf("This program is valid for 32 bit machine only.\n"); printf("Press return to exit..."); getchar(); return EXIT_FAILURE; } /******************************************************************/ ptr = &var;//save the start address of the integer cPtr = ptr;//let a byte (8bit) type pointer point to the start address /******************************************************************/ /*test if the start address point to the least signficant byte*/ if ( (*cPtr) == 0x00 ) printf( "Your Machine is LITTLE-ENDIAN\n");//if so, it's LITTLE-ENDIAN /******************************************************************/ /*test if the start address point to the most signficant byte*/ if ( (*cPtr) == 0xff ) printf( "Your Machine is BIG-ENDIAN\n");//if so, it's BIG-ENDIAN /******************************************************************/ printf("Press return to exit..."); getchar(); return EXIT_SUCCESS; } /****************************************************************/ _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ Motaz Khalid Saad. My Personal WebSite: http://www.geocities.com/motsad E-Mail: motsad(a)yahoo.com _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
From: Terje Mathisen on 2 Mar 2005 14:32 Motaz K. Saad wrote: > hi every body > i wrote a c languge program ti determine ur machine whether big-edian > or little-endian, > and i tried it on my machine (little-endian machine), > can any one please try it on big-edian machine and email me the result. Many such programs have been written previously, some of them probably better than yours. :-) Some points: 1) There's no need to make it 32-bit only, or even power-of-two limited. 2) You should be able to determine strange variations as well, such as DEC 'middle-endian' which is a combination of little-endian 16-bit words and big-endian 32-bit doublewords (or the opposite?). 3) Casting pointers between void, char and int really doesn't need to work the way you think it should. :-( I believe a union of int and array of char might give you better odds of working on more cpus/compilers. This doesn't mean that I haven't written pretty much equivalent (to your program) code myself. :-( Terje -- - <Terje.Mathisen(a)hda.hydro.com> "almost all programming can be viewed as an exercise in caching"
From: Goran Larsson on 2 Mar 2005 16:38 In article <1109788440.565377.315870(a)o13g2000cwo.googlegroups.com>, Motaz K. Saad <motsad(a)yahoo.com> wrote: > //test if the machine is 32bit > if ( sizeof(int) != 4) The comment and the if statement does not match. The if statement tests if the int type of the machine has 4 * CHAR_BIT bits, not if it has 32 bits. Also, even if the int type of the machine is 32 bits the test will not find this, for example, if CHAR_BIT is 16 or 32. -- Gýran Larsson http://www.mitt-eget.com/
From: Ricardo Bugalho on 2 Mar 2005 18:02 On Wed, 02 Mar 2005 20:32:42 +0100, Terje Mathisen wrote: > > 3) Casting pointers between void, char and int really doesn't need to work > the way you think it should. :-( > I believe a union of int and array of char might give you better odds of > working on more cpus/compilers. Unions don't have to work either. Reading from an union element that wasn't the last to be written to is undefined. I think that using memcpy is the correct way. -- Ricardo
From: bybell on 2 Mar 2005 19:01
Terje Mathisen wrote: > This doesn't mean that I haven't written pretty much equivalent (to your > program) code myself. :-( Does anyone use the rpc/xdr.h functions/macros anymore? I thought they were supposed to solve these kinds of problems. In the very least you'd be able to use them to determine the correct ordering for a possibly more efficient implementation (as in an x86 bswap or ppc lwbrx) if need be. As you point out, there are more than big and little endian...3412, ouch. #define LITTLE_ENDIAN 1234 /* least-significant byte first (vax) */ #define BIG_ENDIAN 4321 /* most-significant byte first (IBM, net) */ #define PDP_ENDIAN 3412 /* LSB first in word, MSW first in long (pdp) */ ....byte ordering can vary in floats/doubles too, which is something that I haven't seen much mention of either. -t |