Forum

> > Off Topic > Little Endian testing in C
ForenübersichtOff Topic-ÜbersichtEinloggen, um zu antworten

Englisch Little Endian testing in C

11 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt Little Endian testing in C

DannyDeth
User Off Offline

Zitieren
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

alt Re: Little Endian testing in C

sixpack
User Off Offline

Zitieren
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?

alt Re: Little Endian testing in C

DannyDeth
User Off Offline

Zitieren
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.
1× editiert, zuletzt 24.09.11 15:49:18

alt Re: Little Endian testing in C

Flacko
User Off Offline

Zitieren
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.

alt Re: Little Endian testing in C

DannyDeth
User Off Offline

Zitieren
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..?
1× editiert, zuletzt 24.09.11 17:13:53

alt Re: Little Endian testing in C

DannyDeth
User Off Offline

Zitieren
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?

alt Re: Little Endian testing in C

Flacko
User Off Offline

Zitieren
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)

alt Re: Little Endian testing in C

DannyDeth
User Off Offline

Zitieren
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.

alt Re: Little Endian testing in C

Banaan
User Off Offline

Zitieren
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

alt Re: Little Endian testing in C

Flacko
User Off Offline

Zitieren
user DannyDeth hat geschrieben
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.

alt Re: Little Endian testing in C

DannyDeth
User Off Offline

Zitieren
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.
1× editiert, zuletzt 25.09.11 15:48:31
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antwortenOff Topic-ÜbersichtForenübersicht