0
votes

i work on a 64 bit intel processor...i was learning about big and little endian and what i understood was that these are byte orderings within a word such that in a 64 bit data, msb will have lowest address in big endian form and the highest address in little endian form...now i have a problem:

I wrote this code
to determine whether my processor was little or big endian...
I input

0102030405060708 (this is in hex) 

and hoped to get 08 and 07 and 06 and... and 01 as answer

but instead got 0 and 25 and 50 and -125 and -13 and 501 and -41 and 66.
when I wrote the same code taking 's' as 2 byte(short), the output for 0102 was 2 and 1 (which is in accordance with little endian)...so what went wrong here?

2
When you write your question, note that it shows a preview of what it's going to look like. Please use that to ensure that your question looks readable. Thank you :)jalf
What do you expect to be the bit-code of a double that represents 0x0102030405060708?Nobody moving away from SE
See here for a question containing a method to find the endianess.Some programmer dude
char is a signed type. Use unsigned char if you don't want the negative numbers.hellork

2 Answers

3
votes

You are storing your input value as a double, which stores the value as a floating point value. Try using a long long instead, which is a 64 bit integer, and should store the value as you expect.

2
votes

Taking a hex number into a (double) is not likely to do what you expect; it's a floating point value consisting of a base 2 mantissa and exponent. You might find (long) or (long long) to be closer to what you intended.