473,387 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

little endian or big endian ???

Write a small C program to determine whether a machine's type is
little-endian or big-endian.

Jun 28 '07 #1
23 14143
On Jun 28, 12:01 pm, guthena <r4r...@gmail.comwrote:
Write a small C program to determine whether a machine's type is
little-endian or big-endian.
It's a FAQ.

Jun 28 '07 #2
guthena wrote, On 28/06/07 20:01:
Write a small C program to determine whether a machine's type is
little-endian or big-endian.
Save yourself the trouble and use a machine where char, short, int and
long are all the same size.

Alternatively, do your own homework. Once you've posted your attempt
someone might help you with it.
--
Flash Gordon
Jun 28 '07 #3
In article <11*********************@j4g2000prf.googlegroups.c om>,
guthena <r4****@gmail.comwrites
>Write a small C program to determine whether a machine's type is
little-endian or big-endian.
When does this homework have to be completed by?

--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys.org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Jun 28 '07 #4
guthena wrote On 06/28/07 15:01,:
Write a small C program to determine whether a machine's type is
little-endian or big-endian.
This small C program handles other possibilities, too:

#include <stdio.h>
int main(void){int q=puts("Enter 1 for big-endian or"
" 2 for little-endian: ");fflush(0);switch(q=scanf(
"%d",&q)>0?q:0){defualt:puts("mixed-endian");break;
case 1:puts("big-endian");break;defau1t:puts("other"
"-endian");break;case 2:puts("little-endian");break;
default:puts("anti-endian");break;}return q?!q:q;}

Enjoy!

--
Er*********@sun.com
Jun 28 '07 #5
"guthena" writes:
Write a small C program to determine whether a machine's type is
little-endian or big-endian.
Take a look at this.
http://en.wikipedia.org/wiki/Big_endian
Jun 28 '07 #6
guthena <r4****@gmail.comwrites:
Write a small C program to determine whether a machine's type is
little-endian or big-endian.
Why?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 28 '07 #7
On Jun 28, 12:01 pm, guthena <r4r...@gmail.comwrote:
Write a small C program to determine whether a machine's type is
little-endian or big-endian.
Here you go..

int main()
{
int num=1;
char *cptr;

cptr = (char *)&num;

if (*cptr)
printf ("little endian\n");
else
printf ("big endian\n");

return 0;
}

Which company interview question is this ? :-D

Tejas Kokje

Jun 28 '07 #8
guthena wrote:
Write a small C program to determine whether a machine's type is
little-endian or big-endian.
No, thank you. Why are you giving out assignments?
Jun 28 '07 #9
guthena wrote:
Write a small C program to determine whether a machine's type is
little-endian or big-endian.
OK, I've done that.

--
Chris "it'ss /mine/, preciousss" Dollin

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Jun 29 '07 #10
In article <f6**********@murdoch.hpl.hp.com>, Chris Dollin
<ch**********@hp.comwrites
>guthena wrote:
>Write a small C program to determine whether a machine's type is
little-endian or big-endian.

OK, I've done that.
To save time please put Guthena's name at the top and email it direct
to his lecturer.

Further to save time can I email my homework direct to you in the
future?

Chris (The other one)

--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys.org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Jun 29 '07 #11
On 28 Jun, 20:01, guthena <r4r...@gmail.comwrote:
Write a small C program to determine whether a machine's type is
little-endian or big-endian.
No

Jun 29 '07 #12
Chris Hills wrote:
In article <f6**********@murdoch.hpl.hp.com>, Chris Dollin
<ch**********@hp.comwrites
>>guthena wrote:
>>Write a small C program to determine whether a machine's type is
little-endian or big-endian.

OK, I've done that.
To save time please put Guthena's name at the top and email it direct
to his lecturer.
You missed my signature, yes?
Further to save time can I email my homework direct to you in the
future?
No problem.
Chris (The other one)
I thought you were the /other/ other one. /The/ Chris, of course, being
Chris Torek.

--
Chris "not Hilly, but definitely not flat" Dollin

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Jun 29 '07 #13
Tejas Kokje <bi*************@gmail.comwrote:
On Jun 28, 12:01 pm, guthena <r4r...@gmail.comwrote:
Write a small C program to determine whether a machine's type is
little-endian or big-endian.

Here you go..

int main()
{
int num=1;
char *cptr;

cptr = (char *)&num;

if (*cptr)
printf ("little endian\n");
else
printf ("big endian\n");

return 0;
}

Which company interview question is this ? :-D
Congratulations. You just did his homework for him. Since it's a crappy
assignment, his teacher probably thinks he did a sterling job, he'll
probably pass the course on the back of your work, and he'll end up
holding a job as a programmer _still_ thinking that this is a good
program. Which it is not; it is flawed for several reasons. I presume
_you_ will be the one to clean up after his incompetent behind, not us?

Richard
Jun 29 '07 #14
In article <f6**********@murdoch.hpl.hp.com>, Chris Dollin
<ch**********@hp.comwrites
>Chris Hills wrote:
>In article <f6**********@murdoch.hpl.hp.com>, Chris Dollin
<ch**********@hp.comwrites
>>>guthena wrote:

Write a small C program to determine whether a machine's type is
little-endian or big-endian.

OK, I've done that.
To save time please put Guthena's name at the top and email it direct
to his lecturer.

You missed my signature, yes?
>Further to save time can I email my homework direct to you in the
future?

No problem.
>Chris (The other one)

I thought you were the /other/ other one. /The/ Chris, of course, being
Chris Torek.
Who?
Why?

--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys.org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Jun 29 '07 #15
On Jun 29, 3:22 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
Tejas Kokje <binarysemaph...@gmail.comwrote:
On Jun 28, 12:01 pm, guthena <r4r...@gmail.comwrote:
Write a small C program to determine whether a machine's type is
little-endian or big-endian.
Here you go..
int main()
{
int num=1;
char *cptr;
cptr = (char *)&num;
if (*cptr)
printf ("little endian\n");
else
printf ("big endian\n");
return 0;
}
Which company interview question is this ? :-D

Congratulations. You just did his homework for him. Since it's a crappy
assignment,
This most probably isn't a homework question. Its a typical question
thats asked mostly in interviews in India.
his teacher probably thinks he did a sterling job, he'll
probably pass the course on the back of your work, and he'll end up
holding a job as a programmer _still_ thinking that this is a good
program. Which it is not; it is flawed for several reasons.
Can you tell me how it is flawed? Is it because of casting from int*
to char* ?
Jun 29 '07 #16
Chris Hills wrote:
In article <f6**********@murdoch.hpl.hp.com>, Chris Dollin
<ch**********@hp.comwrites
>>I thought you were the /other/ other one. /The/ Chris, of course, being
Chris Torek.

Who?
You're /surely/ not serious.
Why?
You're /surely/ not serious.

--
Chris "!?" Dollin

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Jun 29 '07 #17
In article <f6**********@pc-news.cogsci.ed.ac.uk>,
Richard Tobin <ri*****@cogsci.ed.ac.ukwrote:
>*Any* test of endianness is bound to use implementation-defined or
undefined behaviour, otherwise all machines would be found to have the
same endianness!
Take an unsigned integral type. C promises no internal padding
and no trap representations. Initialize it. Take its address and cast the
address to unsigned char pointer. C defines this transformation
as being possible and that the resulting unsigned chars will
each have no internal non-value bits and no trap representations.
Now read through the unsigned char array. If the sizeof the
unsigned integral type exceeds the number of bits you initialized
to, then the value bits may be spread out in unusual ways,
most likely by all-0 unsigned char showing up, but perhaps more
complicated patterns are possible? (I'd have to go back and
re-check the values implementation clause.) But you could track
each bit by shifting it through the unsigned integral type and
seeing where it shows up in the unsigned char array.

Tracking down non-value bits and sign representation for signed
integral types -- that's harder.
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton
Jun 29 '07 #18
Chris Dollin wrote:
Chris Hills wrote:
>In article <f6**********@murdoch.hpl.hp.com>, Chris Dollin
<ch**********@hp.comwrites
>>I thought you were the /other/ other one. /The/ Chris, of course, being
Chris Torek.
Who?

You're /surely/ not serious.
>Why?

You're /surely/ not serious.
He is. And stop calling him Shirley.
--
clvrmnky <mailto:sp******@clevermonkey.org>

Direct replies will be blacklisted. Replace "spamtrap" with my name to
contact me directly.
Jun 29 '07 #19
Spade wrote:
>
.... snip ...
>
Can you tell me how it is flawed? Is it because of casting from
int* to char* ?
For example, with a 32 bit int, you have 32 places for the most
sig. bit, 31 for the next bit, etc. for 32! total possibilities.
Yes, some combinations are more likely, but that isn't detecting,
it is discriminating.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 29 '07 #20
Walter Roberson wrote:
>
.... snip ...
>
Take an unsigned integral type. C promises no internal padding
and no trap representations. Initialize it. Take its address and
cast the address to unsigned char pointer. C defines this
transformation as being possible and that the resulting unsigned
chars will each have no internal non-value bits and no trap
representations. ....
Nonsense.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 29 '07 #21
In article <46***************@yahoo.com>,
CBFalconer <cb********@maineline.netwrote:
>Walter Roberson wrote:
>Take an unsigned integral type. C promises no internal padding
and no trap representations. Initialize it. Take its address and
cast the address to unsigned char pointer. C defines this
transformation as being possible and that the resulting unsigned
chars will each have no internal non-value bits and no trap
representations. ....
>Nonsense.
Hmm? Which part is nonsense? Is it the part of C89 3.3 Expressions
that says, "An object shall have its stored value accessed only
by an lvalue that has one of the following types:
[...] - a character type" ?
--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
Jun 29 '07 #22
Walter Roberson wrote:
CBFalconer <cb********@maineline.netwrote:
>Walter Roberson wrote:
>>Take an unsigned integral type. C promises no internal padding
and no trap representations. Initialize it. Take its address and
cast the address to unsigned char pointer. C defines this
transformation as being possible and that the resulting unsigned
chars will each have no internal non-value bits and no trap
representations. ....
>Nonsense.

Hmm? Which part is nonsense? Is it the part of C89 3.3 Expressions
that says, "An object shall have its stored value accessed only
by an lvalue that has one of the following types:
[...] - a character type" ?
I think the following quote (from N869) is adequate:

6.2.6.2 Integer types

[#1] For unsigned integer types other than unsigned char,
the bits of the object representation shall be divided into
two groups: value bits and padding bits (there need not be
any of the latter). If there are N value bits, each bit
shall represent a different power of 2 between 1 and 2N-1,
so that objects of that type shall be capable of
representing values from 0 to 2N-1 using a pure binary
representation; this shall be known as the value
representation. The values of any padding bits are
unspecified.39)

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net
--
Posted via a free Usenet account from http://www.teranews.com

Jun 29 '07 #23
Spade <st***********@gmail.comwrote:
On Jun 29, 3:22 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
Tejas Kokje <binarysemaph...@gmail.comwrote:
On Jun 28, 12:01 pm, guthena <r4r...@gmail.comwrote:
Write a small C program to determine whether a machine's type is
little-endian or big-endian.
Here you go..
int main()
{
int num=1;
char *cptr;
cptr = (char *)&num;
if (*cptr)
printf ("little endian\n");
else
printf ("big endian\n");
return 0;
}
Which company interview question is this ? :-D
Congratulations. You just did his homework for him. Since it's a crappy
assignment,

This most probably isn't a homework question. Its a typical question
thats asked mostly in interviews in India.
It's also a typical question that's asked in many badly thought out
computing courses.
his teacher probably thinks he did a sterling job, he'll
probably pass the course on the back of your work, and he'll end up
holding a job as a programmer _still_ thinking that this is a good
program. Which it is not; it is flawed for several reasons.

Can you tell me how it is flawed?
I can, but I won't. The whole exercise is a wild goose chase, and I'm
not going to help you by telling you which other wild species of goose
you could chase. That would only make a futile pursuit differently
futile, not less.

Richard
Jul 3 '07 #24

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: hicham | last post by:
Hi, I am looking for help, i would like to know how can i use the endian.h and config.h to convert compiled files under solaris from BIG-ENDIAN to compiled files LITTLE-ENDIAN. I am working...
3
by: gary | last post by:
Hi, 1. About all C/C++ compilers, Does stack increase from high address to low address and heap grow increase from low to high? What on earth decides their increase direction, CPU architecture, OS...
8
by: Perception | last post by:
Hello all, If I have a C-like data structure such that struct Data { int a; //16-bit value char; //3 ASCII characters int b; //32-bit value int c; //24-bit value }
2
by: bhatia | last post by:
Hello all, If I have a C-like data structure such that struct Data { int a; //16-bit value char; //3 ASCII characters int b; //32-bit value int c; //24-bit value }
5
by: manishster | last post by:
I keep getting the following in my output file , regardless of whether I convert endian-ness or not . How do i get "01 02 03 04" .... Mahamannu output : 04 03 02 01 b0 00 00
3
RRick
by: RRick | last post by:
This was a question that showed up in a job interview once. (And to answer your next question: No, I didn't :)) Write a subroutine that returns a bool on whether a system supports big endian...
6
by: Javier | last post by:
Hello people, I'm recoding a library that made a few months ago, and now that I'm reading what I wrote I have some questions. My program reads black and white images from a bitmap (BMP 24bpp...
2
by: Ramesh | last post by:
Hi I have a structure as below on big endian based system typedef struct { unsigned long LedA:5; unsigned long LedB:4; unsigned long LedC:8;
23
by: Niranjan | last post by:
I have this program : void main() { int i=1; if((*(char*)&i)==1) printf("The machine is little endian."); else printf("The machine is big endian."); }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.