byte and bit order question

This is a discussion on byte and bit order question within the C Programming forums, part of the General Programming Boards category; hi: i am just learning about the stuff on byte/bit order and got a question. following my pervious post, someone ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    23

    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. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    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. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    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";
    Last edited by Micko; 11-07-2004 at 12:18 AM.

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    23
    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. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    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.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Quote 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!
    Read this:
    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. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    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. #8
    Registered User
    Join Date
    Nov 2004
    Posts
    23
    ok, so big/little endian refers to byte level only, not at bit level.

    thanks

    cheers

    CHUN

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Device Register Access
    By jacob12 in forum C Programming
    Replies: 35
    Last Post: 10-14-2008, 10:51 AM
  2. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  3. byte order change
    By onebrother in forum C Programming
    Replies: 1
    Last Post: 08-06-2007, 05:40 AM
  4. quick question about bit manipulation
    By PJYelton in forum C++ Programming
    Replies: 7
    Last Post: 01-10-2003, 12:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21