byte order and bit order 
Author Message
 byte order and bit order

Quote:

>After reading the C reference manual by H&S(pp 124-126), I got the
>impression that in both left-to-right( big-endian ) and right-to-left
>meory addressing architectures, the most significant byte/bit(MSB) is
>the left-most one, and the least significant byte/bit(LSB) is the
>right-most one.
>Am I right ?

No.  If you walk around to the other side of your computer, you should
note that right and left have been reversed, but the computer functions
exactly as it did before.

The point is that "right" and "left" are typographic conventions used
when people write down graphical representations for numbers; they are
not inherent in computer architectures.  On the other hand, the "least"
and "most" significant bits in a word do have inherent structural
properties based on the wiring of ALUs etc.

This is somewhat confused by electrical engineers who have never thought
about the issues but always assume that everybody writes down number
representations with the LSB on the right, as is taught in most schools
in Western cultures.  This assumption is embedded in opcode names such
as "arithmetic shift right", which really should be called "arithmetic
shift toward lower significance".

If you use the hardware to sequentially access an array of bytes, say
        0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
you can consider that the hardware's "natural byte order".  If instead
you sequentially access an array of words sharing the same storage area,
        A B C D
you can consider that the hardware's "natural word order".  The terms
"big endian" and "little endian" refer to the two most common ways in
which the natural byte order is mapped onto the words.  Supposing that
word "A" overlaps bytes 0, 1, 2, and 3, that bytes consist of 8 bits
each, and that we agree to write numerical representations with the LSB
on the LEFT (just to make the point that this is purely conventional),
a "big endian" machine would be one such that the word value
        word A : hexadecimal 11335577
when interpreted as the overlapping bytes 0, 1, 2, and 3 (in natural
order) would have
        byte 0 : hexadecimal 77
        byte 1 : hexadecimal 55
        byte 2 : hexadecimal 33
        byte 3 : hexadecimal 11
while if the machine had been little-endian, the bytes would have been
(in natural order)
        byte 0 : hexadecimal 11
        byte 1 : hexadecimal 33
        byte 2 : hexadecimal 55
        byte 3 : hexadecimal 77

Questions about the order of bits within a byte are meaningless unless
some way to determine the "natural bit order" is specified; most
machines do not allow individual bit addressing.  In American hardware
documentation, most often bit layout is shown in diagrams with the LSB
on the right, in accordance with traditional Western arithmetic
conventions, but vendors number the bits in such diagrams in various
ways, choosing arbitrarily whichever one the documenter happens to like.
The only purpose of such numbers is to provide labels that the
documentation can use to describe details of machine operations.
Most mathematicians would probably like to see the bits numbered 0, 1,
... starting at the LSB, but in fact 0, 1, ... seem to be assigned to
bits starting at the MSB more frequently by vendors, particularly those
with big-endian architectures, and sometimes the bits are numbered
starting at 1 instead of at 0.

Quote:
>Aslo, what's the byte order for a binary UNIX disk file ?  In other words,
>if a 4-byte integer( for example) is written to a disk file by using
>write/fwrite, what's the byte order of the interger as stored in the disk
>file ?   Is byte-swapping necessary if the integer is going to be retrieved by
>using read/fread ?

write/fwrite/read/fread all deal with arrays of bytes, and a UNIX file
is organized as a sequence of bytes.  You cannot write a 4-byte integer,
all you can do is write an array of 4 bytes.  When you convert the
address of the integer to the starting address of the array, the C
language requires that the "lowest addressed" byte be pointed at as a
result of the pointer conversion.  For integer "A" in the above example
this would be byte "0".

If you're sloppy and don't properly convert the int pointer before
it is seen by the I/O function, then all bets are off.  I know of
architectures where the representation for a word pointer does not
access the "lowest addressed" byte within the integer if interpreted
as a byte pointer.  In some cases it doesn't make sense as a byte
pointer at all.  An explicit conversion is required.



Sun, 06 Dec 1992 20:49:17 GMT  
 byte order and bit order

Quote:

>After reading the C reference manual by H&S(pp 124-126), I got the
>impression that in both left-to-right( big-endian ) and right-to-left
>meory addressing architectures, the most significant byte/bit(MSB) is
>the left-most one, and the least significant byte/bit(LSB) is the
>right-most one.

This is essentially the definition of "most (least) significant byte/bit".
However, this says nothing about whether the leftmost byte is the first in
memory, for example.

Quote:
>Aslo, what's the byte order for a binary UNIX disk file ?  In other words,
>if a 4-byte integer( for example) is written to a disk file by using
>write/fwrite, what's the byte order of the interger as stored in the disk
>file ? ...

This is machine-dependent, and there are at least three different byte
orders in use on different Unix machines.  Then, of course, there's the
problem that Unix runs on 16-bit, 32-bit, and 64-bit machines, so it's
not at all trivial to read a binary file in a portable way.  In general
you are better off making your files ASCII unless there is compelling
need for binary.
--
As a user I'll take speed over|     Henry Spencer at U of Toronto Zoology



Sun, 06 Dec 1992 23:27:14 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Writting low-order or high-order bytes

2. byte and bit order

3. bit order in a byte

4. Load order and DLL entry order of implicitly linked DLLs, Environment variables

5. High order/low order problem

6. Socket Program(Host Order/Network Order)

7. byte order

8. Byte order and portability

9. Byte-Ordering

10. Strange byte order using fread() and fwrite()

11. char * in network byte order to unsigned long int

12. Byte Order for double

 

 
Powered by phpBB® Forum Software