Hi Guys,
How to find out the given processor is a big endian or small endian.??
Hi Guys,
How to find out the given processor is a big endian or small endian.??
Read it's reference manual ?
http://c-faq.com/misc/endiantest.html
What about those processors which are neither?
What about those processors which can change endian on the fly?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
hmmm, what is an Endian? i thought he meant indian or sumthin lol!
can anybody give me a brief description of what an endian is? thanks!
It is not who I am inside but what I do that defines me.
Haven't you ever heard of a search engine? Or are you just too lazy? [/rhetorical]
Quzah.
Hope is the first step on the road to disappointment.
Try this code to find out your processor is big or small endian
Code:main() { int num = 1; if(*(char *)&num == 1) { printf("\nLittle-Endian\n"); } else { printf("Big-Endian\n"); } }
Little Endian means that the lower order byte of the number is stored in memory at the lowest address, and the higher order byte is stored at the highest address. That is, the little end comes first.
For example, a 4 byte, 32-bit integer
Byte3 Byte2 Byte1 Byte0
will be arranged in memory as follows:
Base_Address+0 Byte0
Base_Address+1 Byte1
Base_Address+2 Byte2
Base_Address+3 Byte3
Example :Intel processors use "Little Endian" byte order.
"Big Endian" means that the higher order byte of the number is stored in memory at the lowest address, and the lower order byte at the highest address. The big end comes first.
Base_Address+0 Byte3
Base_Address+1 Byte2
Base_Address+2 Byte1
Base_Address+3 Byte0
Motorola, Solaris processors use "Big Endian" byte order.
Mips?
Arm?
Pdp-11?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
ok. i think i got the general idea. now how does this endian thingy work? i mean, wuts the difference between using big and lil endian byte orders?
It is not who I am inside but what I do that defines me.
Using for what?wuts the difference between using
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
> wuts the difference between using big and lil endian byte orders?
For 99% of the programs, none at all.
For anything involving network communication, there are functions like htonl() and ntohl() which convert between network byte order and the byte order of the local machine.
The only other place you're likely to come across it is say picking apart image files (say .bmp) which store sizes in a specific byte order.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Hi,
I was searching for the same problem, and this thread proved to be very helpful. Thanks to everyone.
I have just one more query, how can I find whether the computer is little endian or big endian using a union.
I fully understand the code provided by Swaugh (thanks for it) but I also need to find out how I can accomplish the same using a union.
Thanks in anticipation,
regards,
koodoo
You shouldn't bump old threads, especially a thread as old as this one. You can start a new thread and link to an old thread if you want to.
BTW, it's [color=red]text[/color], not [red]text[/red].
This might work, though I'm not too sure about it:I have just one more query, how can I find whether the computer is [red]little endian[/red] or [red]big endian[/red] using a union.
Code:union { short snum; unsigned char cnum; } test; test.snum = 1; if(test.cnum == 1) puts("little edian"); else puts("bug edian");
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
I'll remember that, the next I have such a query.
Thanks for that too, actually I'm used to some other forums where that kinda thing works
I think it will thanks for the help. I must also apologize coz I posted so very early (without putting in much effort myself, I was kinda in a hurry).
I searched and found a similar solution.
The following code :
would print 256 0 1 on a lil endian machine, while 256 1 0 on a big endian machine.Code:#include <stdio.h> void main() { union s { int i; char ch[2]; }; union s obj; obj.i=256; printf("%d %d %d\n",obj.i,obj.ch[0],obj.ch[1]); }
Thanks for all the help
The code that you found has a few problems with it. First of all, void main() is a bad idea, and one should use int main(). Secondly, I'm not sure if it would work when ints were different sizes or chars were signed versus unsigned.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
yeah, thanks again.
I don't know why I am in such a hurry and messing up things.
In the discussion from where I got this code, the first error pointed out was that of using void main() which of course is a bad practise, I forgot to correct it while copying and pasting code.
Moreover, the link posted earlier in this thread http://c-faq.com/misc/endiantest.html
describes both the methods (using pointers & unions) It deals with varied sized ints, however it should have chosen unsigned char as you have suggested.
P.S. On more query, this might be a bit silly, but I could not find how to add a signature to my profile. I tried the USER CP and edit profile links, but it was not there...I feel completely stupid as of now.