473,785 Members | 2,299 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
23 14187
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.securityfoc us.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********@mai neline.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
transformati on as being possible and that the resulting unsigned
chars will each have no internal non-value bits and no trap
representation s. ....
>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********@mai neline.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
transformatio n as being possible and that the resulting unsigned
chars will each have no internal non-value bits and no trap
representatio ns. ....
>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.securityfoc us.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.comwrot e:
On Jun 28, 12:01 pm, guthena <r4r...@gmail.c omwrote:
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
29014
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 under linux debian 3.0 woody Thank you for your help.
3
5193
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 or compiler? 2. In GNU gcc, { int a = {0, 1, 2, 3, 4}; bool b; float c; for (int i = 0; i < 5; i++) {
8
27465
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
6551
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
3149
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
2629
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 numbers (true) or little endian (false). I tried something like the following, but never did find out if it was correct. There are two assumptions here: First, big endian direction from small to large is ordered like a string. Second, endian is...
6
3180
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 without compression). It has it's words and dwords stored in little- endian, so I do a conversion to big-endian when reading full words or dwords. I have done this because my system is big-endian. But now... what if one compiles the library in a...
2
4058
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
7050
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
10356
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10100
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5396
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5528
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4061
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2893
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.