Endianness of machine

M

Michael Mair

friend.05 said:
Code to check endianness of machine

You forgot to include your code.
Please make sure that it is a compiling piece of
code and explain your exact problem with it.

Cheers
Michael
 
S

Sreekanth

#include <stdio.h>

int main()
{
long x = 0x34333231;
char *y = (char *) &x;

if(strncmp(y,"1234",4))
printf("Big Endian");
else
printf("little Endian");
}
 
K

Keith Thompson

Sreekanth said:
#include <stdio.h>

int main()
{
long x = 0x34333231;
char *y = (char *) &x;

if(strncmp(y,"1234",4))
printf("Big Endian");
else
printf("little Endian");
}

Please provide context, even if the previous post was only one line.
You *have* read <http://cfaj.freeshell.org/google/>, haven't you?

The program makes the following unwarranted and unnecessary
assumptions:

ASCII character set

sizeof(long)==4

Big-endian and little-endian are the only possibilities

Output will appear even without a new-line

It's acceptable to fall off the end of main() without returning a
value (true in C99, but still poor style)

And a minor point: "int main()" is acceptable, but the more explicit
"int main(void)" is preferred.
 
S

Sreekanth

Keith said:
Please provide context, even if the previous post was only one line.
You *have* read <http://cfaj.freeshell.org/google/>, haven't you?

The program makes the following unwarranted and unnecessary
assumptions:

ASCII character set

sizeof(long)==4

Big-endian and little-endian are the only possibilities

Output will appear even without a new-line

It's acceptable to fall off the end of main() without returning a
value (true in C99, but still poor style)

And a minor point: "int main()" is acceptable, but the more explicit
"int main(void)" is preferred.


Sorry for the bad post. I am learning C programming been a java
programmer all my life. This is the first time I am using USENET group.
So that is not an excuse for a poor post. Anyway I will see to that it
doesnt repeat
 
B

Ben Bacarisse

#include <stdio.h>

int main()
{
long x = 0x34333231;
char *y = (char *) &x;

if(strncmp(y,"1234",4))
printf("Big Endian");
else
printf("little Endian");
}

Rather too many assumptions here (as already pointed out). Endian-ness is
essentially the relationship between byte addressing and artimetic
significance so you can investigate it like this:

#include <stdio.h>

/* Edit this if you have an old compiler: */
typedef unsigned long long int integer;

union {
integer number;
char bytes[sizeof(integer)];
} u;

int main(avoid)
{
int b;
u.number = (integer)0;
/* assign 1 to LSB, 2 to the next and so on... *.
for (b = 0; b < sizeof(integer); b++)
u.number |= (integer)(b + 1) << (8 * b);
/* convert byte numbers to digits and print in address order */
for (b = 0; b < sizeof(integer); b++)
printf("%c", '0' + u.bytes);
printf("\n");
return 0;
}

Although there n! possible results on an n-byte architecture, sanity and
engineering mean that you are unlikely to come across more than 3.
 
S

stathis gotsis

The program makes the following unwarranted and unnecessary
assumptions:

ASCII character set

sizeof(long)==4

I think sizeof(long)>=4.
Big-endian and little-endian are the only possibilities

Output will appear even without a new-line

It's acceptable to fall off the end of main() without returning a
value (true in C99, but still poor style)

And a minor point: "int main()" is acceptable, but the more explicit
"int main(void)" is preferred.

Also, the OP forgot to include <string.h>
 
S

stathis gotsis

Ben Bacarisse said:
Endian-ness is
essentially the relationship between byte addressing and artimetic
significance so you can investigate it like this:

Yes, allow me some comments.
#include <stdio.h>

/* Edit this if you have an old compiler: */
typedef unsigned long long int integer;

union {
integer number;
char bytes[sizeof(integer)];
} u;

int main(avoid)

Is this void or am i missing something?
{
int b;
u.number = (integer)0;
/* assign 1 to LSB, 2 to the next and so on... *.
for (b = 0; b < sizeof(integer); b++)
u.number |= (integer)(b + 1) << (8 * b);
/* convert byte numbers to digits and print in address order */
for (b = 0; b < sizeof(integer); b++)
printf("%c", '0' + u.bytes);


That will not print digits if sizeof(integer) exceeds 9.
 
B

Ben Bacarisse

Is this void or am i missing something?

:) Yes, void. It is my **** touchpad -- I need to have it switch off
when typing or I can insert random characters where the (mouse) cursor is
without noticing!
printf("%c", '0' + u.bytes);


That will not print digits if sizeof(integer) exceeds 9.


Good point, thanks. Can't think now why I didn't just write:

printf("%d ", u.bytes);
 
F

Flash Gordon

stathis said:
I think sizeof(long)>=4.

I know you are wrong. I've used a conforming implementation where
sizeof(long)==2 and sizeof(int)==1. It had 16 bit bytes. I have hear of
others with 24 bit bytes and 32 bit bytes.

So tell me, what is the endianness if
sizeof(long)==sizeof(int)==sizeof(short)==1

<snip>
--
Flash Gordon
Living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidlines and intro -
http://clc-wiki.net/wiki/Intro_to_clc
 
K

Keith Thompson

stathis gotsis said:
I think sizeof(long)>=4.

If you're saying that's a language requirement, it isn't.
If CHAR_BIT > 8, sizeof(long) can be less than 4.

If you're saying that the program merely assumes that sizeof(long)>=4,
consider what happens on a big-endian system with CHAR_BIT==8 and
sizeof(long)==8. The bytes composing the long value would then be
(0x00, 0x00, 0x00, 0x00, 0x34, 0x33, 0x32, 0x31). The program happens
to print "Big Endian" in that case, but only because the bugs cancel
each other out.
 
S

stathis gotsis

I meant that the program makes the assumption that sizeof(long)>=4, as
opposed to the assumption that sizeof(long)==4. Yes, sizeof(long)>=4 is not
necessary.
I've used a conforming implementation where
sizeof(long)==2 and sizeof(int)==1. It had 16 bit bytes. I have hear of
others with 24 bit bytes and 32 bit bytes.

That is interesting. Can you provide more details about what implentation
that was?
So tell me, what is the endianness if
sizeof(long)==sizeof(int)==sizeof(short)==1

In how many ways can a 1-byte value be stored into memory? 1?
 
S

stathis gotsis

If you're saying that the program merely assumes that sizeof(long)>=4,
consider what happens on a big-endian system with CHAR_BIT==8 and
sizeof(long)==8. The bytes composing the long value would then be
(0x00, 0x00, 0x00, 0x00, 0x34, 0x33, 0x32, 0x31). The program happens
to print "Big Endian" in that case, but only because the bugs cancel
each other out.

Would it work if the above system were little-endian?
 
K

Keith Thompson

stathis gotsis said:
Would it work if the above system were little-endian?

It seems to (assuming an ASCII character set and all the other
implicit assumptions actually hold).
 
F

Flash Gordon

stathis said:
I meant that the program makes the assumption that sizeof(long)>=4, as
opposed to the assumption that sizeof(long)==4. Yes, sizeof(long)>=4 is not
necessary.


That is interesting. Can you provide more details about what implentation
that was?

The implementation I used with sizeof(long)==2 and sizeof(int)==1 with
CHAR_BIT==16 was for the TMS320c2x DSP processor. The one I can remember
hearing of with CHAR_BIT==24 was a Motorola DSP. The implementations
I've heard of with CHAR_BIT==32 are also DSPs.

These days I'm doing Linux and Windows development with lots fo 3rd
party libraries and not doing any DSP work.
In how many ways can a 1-byte value be stored into memory? 1?

Yes, but do you call it big-endian or little-endian? If you want to make
a portable endianness test you have to decide.
--
Flash Gordon
Living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidlines and intro -
http://clc-wiki.net/wiki/Intro_to_clc
 
S

stathis gotsis

Flash Gordon said:
The implementation I used with sizeof(long)==2 and sizeof(int)==1 with
CHAR_BIT==16 was for the TMS320c2x DSP processor. The one I can remember
hearing of with CHAR_BIT==24 was a Motorola DSP. The implementations
I've heard of with CHAR_BIT==32 are also DSPs.

Thank you for the extra information.
Yes, but do you call it big-endian or little-endian? If you want to make
a portable endianness test you have to decide.

I do not know how it can be called since it conforms to both big and little
endian definitions (there is one single byte which can be regarded MSB or
LSB). Furthermore, i have not come up with a portable program that tests
endianness. At this point, i prefer to see the way bytes in a long are
stored into memory, and decide the endianness myself.
 
K

Keith Thompson

Flash Gordon said:
So tell me, what is the endianness if
sizeof(long)==sizeof(int)==sizeof(short)==1

What is the sound of one hand clapping?

Such a system has no defined endianness. A portable function to
determine endianness must allow for that possibility, just as it must
allow for exotic possibilites such as the PDP-11's "middle-endian"
representation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top