See Boss,
1st know what is ENDIAN.
It tells about way of storage of of values occupying greater than 1 byte.
2 types of endian ness are there :
    1.    LITTLE ENDIAN
    2.    BIG ENDIAN
In LITTLE ENDIAN the "Lower" byte is stored at Lower memory location, and 
Higher byte is stored at higher memory location
In BIG ENDIAN the Higher byte is stored at Lower memory location and vice-versa.
Now what you are facing is that the lower byte is stored at Lower memory 
location and vice versa, which is the case of Little endian ness.
Here you are entering the value 0x01 at lower location(y[0]) and 0x15( 0d21 ) 
at higher memory (y[1])location.Then you are getting the output as 
0x1501(0d5377)..... implicating LITTLE ENDIANESS
Isnt it simple ??
 
Karmennevaya Dhikaraste, Maaphaaleshu Kadaachanah



----- Original Message ----
From: Anup Joshi <[EMAIL PROTECTED]>
To: [email protected]
Sent: Saturday, 4 August, 2007 10:49:41 PM
Subject: Re: [c-prog] 2 8bit to 1 16bit variable

thank you all of you for your responses but i still cant get one thing, i was 
successfully able to compile my recent code on gcc v3.42 also, here is the code 
again:

=====
#include <stdio.h>

union vals {
unsigned short x;
unsigned char y[2];
} myvals;

int main() {
myvals.y[0] = 1; //lsb
myvals.y[1] = 21; //msb

printf("%d", myvals.x);
printf("\n %d", sizeof(myvals) );
}

compiled using command: gcc union.c -o myunion.exe
====
and output again is 
5377 
2
=====
so according to Vic i havent solved the endian problem but i cant understand 
where the problem actually is in this code ???

second thing is the sizeof union shows up to be 2 but i was expecting it to be 
3 (i thought the array is of 3 bytes though it may contain only two elements) 
because i changed the array from uchar y[1](sizeof gave 2) to uchar y[2](size 2 
again)!!!! why does this happen???

and now if union is the feature of c itself why cant i use it to solve brians 
problem???

my questions might be silly becoz im a newbie hence i have turned up to you 
gurus for clarifying my doubts. thanks for patiently responding me 

with regards

anup

------------ --------- --------- ---
Boardwalk for $500? In 2007? Ha! 
Play Monopoly Here and Now (it's updated for today's economy) at Yahoo! Games.

[Non-text portions of this message have been removed]





      Get the freedom to save as many mails as you wish. To know how, go to 
http://help.yahoo.com/l/in/yahoo/mail/yahoomail/tools/tools-08.html

[Non-text portions of this message have been removed]

Reply via email to