# Thread: byte and bit order question

1. ## byte and bit order question

hi:

i am just learning about the stuff on byte/bit order and got a question.

following my pervious post, someone gave me a example of what +1. and -1. looks like in memroy in the little endian format

For instance (little endian), in memory - negative 1.0 looks like:
00 00 80 BF
while positive 1.0 looks like:
00 00 80 3F

ok, now, BF in binary is 10111111 and 3F is 00111111

am i correct that the sign bit in this case is the left most bit in the right most byte? if yes, am i correct that the byte order in this case is in little endian and the bit order in each bit is in big endian? this is very confusing to me, any good reference on the net that can help me understand this better?

many thanks

CHUN

2. little endian vs big endian just refers to the order the bytes are stored in memory. Let's say you have a 4 byte integer.

Code:
`int i = 0x12345678;`
On a big endian machine, i would be stored in memory as:
12 34 56 78
The most significant byte is stored first. Little endian would look like:
78 56 34 12
Here the most significant byte is stored last.

You can do a google search to read up more on this, but I found the following site explains it OK.
http://www.noveltheory.com/techpapers/endian.asp

3. Here's how you can test it:
Code:
```unsigned short word = 0x1234;
unsigned char * p = (unsigned char *) &word;
if ( p[0] == 0x12 )
printf ("Big Endian Machine\n");
else
printf ("Little Endian Machine\n";```

4. thanks guys, i will give that a try. am i correct that in general, the bit order is usually in big endian although small endian is also possibile?

cheers

CHUN

5. x86 processors are little endian. I'm not exactly sure what the demography of computers is in the world, but x86 processors are pretty abundant.

6. Originally Posted by itsme86
x86 processors are little endian. I'm not exactly sure what the demography of computers is in the world, but x86 processors are pretty abundant.
It is correct!
For example, a 32-bit integer value like 0x12345678 spans four 8-bit bytes. Intel x86 machines use the 'little-endian' order, which means the least significant byte is stored first. So the value 0x12345678 would be stored as the byte sequence 0x78, 0x56, 0x34, 0x12. Most machines that don't use little-endian use big-endian, which is exactly the opposite: the most significant byte is stored first. The same value would then be stored as 0x12, 0x34, 0x56, 0x78. Because protocol data can be transferred between machines with different byte ordering, a standard is needed to prevent the machines from interpreting the data the wrong way.
So when dealing with networks we're talking about network byte order:

network byte ordering
Because protocols like TCP/IP have to work between different type of systems with different type of byte ordering, the standard is that values are stored in big-endian format, also called network byte order. For example, a port number (which is a 16-bit number) like 12345 (0x3039) is stored with its most significant byte first (ie. first 0x30, then 0x39). A 32-bit IP address is stored in the same way, each part of the IP number is stored in one byte, and the first part is stored in the first byte. For example, 216.239.51.100 is stored as the byte sequence '216,239,51,100', in that order.

Hope this helps!

7. am i correct that in general, the bit order is usually in big endian although small endian is also possibile?
big endian and little endian refer to the byte order, not the bit order.

8. ok, so big/little endian refers to byte level only, not at bit level.

thanks

cheers

CHUN