Forum

> > Off Topic > Little Endian testing in C
Forums overviewOff Topic overviewLog in to reply

English Little Endian testing in C

11 replies
To the start Previous 1 Next To the start

old Little Endian testing in C

DannyDeth
User Off Offline

Quote
Heyo everyone,

I have been arguing with someone that you can check if a machine is big or little endian with this code:
1
2
3
4
5
6
7
8
#include <stdio.h>
#include <stdbool.h>

const bool __LITTLE_ENDIAN__ = ( 0x00FF > 0x0100 );

int main(void){
	printf("%d",__LITTLE_ENDIAN__);
}
To me it makes perfect sense, but I'm not exactly THAT sure.

Any answers are appreciated,
Danny

old Re: Little Endian testing in C

sixpack
User Off Offline

Quote
Makes sense to me but I am not so sure, I mean there are some chances it might not work, I think it depends on something else, you better try it yourself.

P.S. Do you know any CS2D players that know C?

old Re: Little Endian testing in C

DannyDeth
User Off Offline

Quote
Me, Lee perhaps, Darkbyte ( i believe it is failbit now )... not really sure to be honest.

EDIT: It should work.. Eish! If only I had a Little Endian machine.

EDIT2: Myah... I'm getting more sure of myself the more I think. i believe it works just fine.
edited 1×, last 24.09.11 03:49:18 pm

old Re: Little Endian testing in C

Flacko
User Off Offline

Quote
Theres a wikipedia article about endianness that shows an example about this.

I think it involved casting an int to a char*

EDIT:
1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
int main(void)
{
	int i = 1;
	char *p = (char *) &i;
	if ( p[0] == 1 )
		printf("Little Endian\n");
	else
		printf("Big Endian\n");
	return 0;
}

EDIT 2: Soz, your code did not work. I guess integer constants (even hex ones) get transformed into the machine's endianness at compile time.

old Re: Little Endian testing in C

DannyDeth
User Off Offline

Quote
Man, compilers these days are annoying! How could they convert it into the machines endianness?? Evil. Pure evil.

Anyway, it looks like your code is perfect. ( *sniff* )

Still wondering why they would convert the hexdec values into the machines own endianness... so annoying.

EDIT: WAAAIIIT one friggin' second! If they were converting the hexdec values to the machines endianness, how would they know which value was little/big endian??

EDIT2: And surely it would make no difference..?
edited 1×, last 24.09.11 05:13:53 pm

old Re: Little Endian testing in C

DannyDeth
User Off Offline

Quote
Coz, Flacko, just think about it:
0x100 in Big Endian is 256, 0x100 in Little Endian is 1 (provided that it is a short..). 0xFF in Big Endian is 255, in Little Endian it is some number around 60k. So if the machine was Big Endian, 0x100 < 0xFF != true and in Little Endian 0x100 < 0xFF = true... so why would conversion change anything?

old Re: Little Endian testing in C

Flacko
User Off Offline

Quote
Yes, but on a little endian machine 0x0100 probably gets converted to 0x0001 as well as 0x00FF to 0xFF00
0xFF00 > 0x0001 = 0 (on little endian machines only)

While the same operation will return 1 (or true) on big endian machines, the numbers don't get transformed
0x00FF > 0x0100 = 0 (on big endian machines only)

old Re: Little Endian testing in C

DannyDeth
User Off Offline

Quote
why would the machine do that? if it is a little endian machine it will store all values as little endian, it won't go and convert it into big endian. that is just a really bad idea and no hacker would ever do this.

EDIT: and Little Endian stores the least significant byte first, not the other way 'round.

old Re: Little Endian testing in C

Banaan
User Off Offline

Quote
My C skills aren't that good (yet?), so all I can really do for you is test...

According to
1
2
const int i = 1;
#define   is_bigendian()   ((*(char*)&i)==0)
my machine is little endian. Your code outputs 0

old Re: Little Endian testing in C

Flacko
User Off Offline

Quote
user DannyDeth has written
why would the machine do that? if it is a little endian machine it will store all values as little endian, it won't go and convert it into big endian. that is just a really bad idea and no hacker would ever do this.

EDIT: and Little Endian stores the least significant byte first, not the other way 'round.


Little/big endian machines do integer comparisons according to their corresponding endianness.
Therefore, 256, 0x100, 0400 will always be greater than 255, 0xFF, 0377 no matter how you write those numbers.

No, a LE machine won't store values as BE. I guess you misunderstood me?

And yes, that's right, LE stores LSB first. BE is the other way round.

old Re: Little Endian testing in C

DannyDeth
User Off Offline

Quote
Ah! I see now. Do you think this could possibly work? ::
1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
#include <stdint.h>

int main(void){
	uint16_t shr = 1;
	if( ((shr >> 8) & 0x00FF) == 0x01) {
		printf("Little Endian\n");
	} else {
		printf("Big Endian\n");
	}
}
I'm guessing I may be falling into the same trap here. Gonna try this out according to Banaan's code.

EDIT: It weks! Finally found a way to test this shit out.
edited 1×, last 25.09.11 03:48:31 pm
To the start Previous 1 Next To the start
Log in to replyOff Topic overviewForums overview