Endianness

The term endianness refers to the order in which the bits and bytes of data are stored in memory.  It was coined in 1980 by the computer scientist Danny Cohen who was a pioneer in computer networking.

The value of a number is reckoned by assigning place values to its binary digits.  Even a single-byte character is encoded according to the numeric value of its bits.  When a binary number is written on paper, its digits are sequenced left-to-right in descending order of place value.

The bits within a byte in computer memory are numbered from 0 to 7 to measure their offsets from the beginning of the byte.  Therefore, each bit in memory is identified by the address of its byte and its offset number within the byte.

In addition, each bit of a single-byte or multiple-byte field stored in memory is identified by a bit number which is the exponent of its binary place value.  For a single-byte character, the bit numbers range from 0 to 7.  For a 32-bit integer, the bit numbers range from 0 to 31.  A binary number that exceeds 8 bits is stored in multiple bytes at consecutive memory addresses.  The field’s bit numbers are consecutive across the consecutive bytes.

In some computer architectures, a field is stored in memory so that the highest-numbered bit is at the lowest address/offset.  In this case, the big end of the binary number is the beginning of the field, and memory is in big-endian format.

In other computer architectures, a field is stored in memory so that the lowest-numbered bit is at the lowest address/offset.  In this case, the little end of the binary number is the beginning of the field, and memory is in little-endian format.

In a chart of computer memory, byte addresses and bit offsets within bytes are sequenced left-to-right in ascending order.  In a chart of memory in big-endian format, the bits of an integer appear in the same order as they would be written on paper.  In a chart of memory in little-endian format, they appear in the opposite order, as a mirror image of the way they would be written on paper.  This is illustrated below.

Integer 0x1a2b3c4d
 

             Big-Endian Format

address      a      a + 1    a + 2    a + 3
offset   01234567 01234567 01234567 01234567

hex         1a       2b       3c       4d
binary   00011010 00101011 00111100 01001101

bit num  11111111 11111111 00000000 00000000
         fedcba98 76543210 fedcba98 76543210
    Little-Endian Format

    a      a + 1    a + 2    a + 3
01234567 01234567 01234567 01234567

   b2       3c       d4       58
10110010 00111100 11010100 01011000

00000000 00000000 11111111 11111111
01234567 89abcdef 01234567 89abcdef

Bit-shifting operators are equivalent in big-endian and little-endian architectures.  The direction of shift is understood to apply to the bits as they would be written on paper.  Therefore, left shift moves the bits to higher place values, and right shift moves them to lower place values.

The following routine converts an integer to the endian format of the host system by reversing it’s bits.

unsigned int reversebits(unsigned int int_in)
{
    unsigned int int_out = 0u;

    for (int n = 8 * sizeof(int_in); n > 0; n--) {
        int_out <<= 1;
        int_out |= int_in & 1u;
        int_in >>= 1;
    }

    return int_out;
}

Copyright © 2018 The Stevens Computing Services Company, Inc.  All rights reserved.