Saturday, December 5, 2026
HomeC ProgrammingMassive-Endians and Little-Endians | C For Dummies Weblog

Massive-Endians and Little-Endians | C For Dummies Weblog


Jonathan Swift’s satirical novel Gulliver’s Travels describes a political rift within the kingdom of Lilliput. The 2 factions are based mostly on which finish of an egg to separate open first: the massive finish or the little finish. The argument escalates till the purpose the place the Bid-Endians are declared rebels and by the dominating Little-Endians. These descriptions discovered their means into the pc realm to settle one other foolish argument.

In 1980, pc scientist Danny Cohen wrote a memorandum describing one of many classic “holy battle” arguments in computing: Easy methods to retailer a multibyte integer digitally. Ought to the byte order begin on the high of reminiscence or the underside? The highest is the little-endian, the underside is the big-endian. The method describes the “endianness” of a saved worth.

Take into account the worth 0x1234. The worth is represented by two bytes in storage. One byte shops the 0x12 have of the 16-bit worth and the opposite shops 0x34. Collectively they construct the complete 16-bit worth 0x1234. The order through which the bytes are saved in reminiscence determines their endianness.

If the bytes are organized in reminiscence as 0x34 on the lowest handle adopted by 0x12 at subsequent handle, they’re little-endian. In order single-byte values, a program reads 0x34 first after which 0x12. However as a 16-bit worth, the pc understands its endianness and reads it as 0x1234.

If the bytes are organized in reminiscence as 0x12 on the lowest handle adopted by 0x34, they’re big-endian. The ending worth is “on high.”

Take into account a bigger worth, say 32-bits. Within the little-endian format, the worth 0x11223344 is saved in reminiscence as 0x44, 0x33, 0x22, 0x11. The worth 0x44 occupies the bottom byte handle in reminiscence, with different values saved in subsequent bytes. That is the format utilized by the Intel and AMD processors utilized in PCs.

These particulars are labored out on the processor stage. Your code needn’t concern itself with how the values are saved, big-endian or little-endian, as the interpretation is made for you. Nonetheless, while you need to do bit twiddling on uncooked reminiscence, figuring out the order is vital. In the event you’re curious, you may run the next program to substantiate the system’s endianness:

2026_12_05-Lesson.c


#embrace <stdio.h>

int major()
{
    union {
        int a;
        char alpha[4];
    } instance;
    int x;

    instance.a = 0x41424344;
    printf("The worth %X is saved as ",instance.a);
    for( x=0; x<4; x++ )
        putchar(instance.alpha[x]);
    putchar('n');

    return 0;
}

The union instance shops each an integer and a 4-byte character array in the identical reminiscence. The worth 0x41424344 is saved in int variable instance.a. Then the contents of char array instance.alpha[] are output, revealing how the bytes are saved in reminiscence:

The worth 41424344 is saved as DCBA

The primary byte saved is 0x44, uppercase letter ‘D’. The rest of the worth is saved in little-endian order.

The order is additional proven by debugging this system. Right here is the output from the GNU debugger (gdb) when the reminiscence at instance.a is dumped:

(gdb) x/4xb &instance.a
0x7fffffffdee8: 0x44 0x43 0x42 0x41

Once more, you see the little-endian order.

Again once I code on 8-bit computer systems, the endianness was known as MSB/LSB, Most Vital Byte and Least Vital Byte. The byte order on these methods was most important byte (MSB) then the least vital byte (LSB). These phrases are nonetheless used at the moment, although within the 8-bit world with a 16-bit handle, there was just one MSB and LSB per worth. Right this moment, MSB and LSB seek advice from first and final bytes of any worth saved in reminiscence. For the worth 0x41424344, the MSB is 0x41 because it’s saved within the highest reminiscence handle in little-endian format; the LSB is 0x44 because it’s saved within the lowest reminiscence handle.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments