How to find a machine is little or big endian?

Introduction

This article explains the methods used to find out whether a machine is big or little endian

Endianness

Endianness is the order of sequencing of bytes in computer memory. It can be either Little endian or Big endian.
  • If the LSB is stored first, its little-endian
  • If the MSB is stored first , its big-endian
Its is nicely explained in https://en.wikipedia.org/wiki/Endianness
Image credit to Wikipedia
How data is stored. Thank you Wikipedia!

How to find it in a Linux machine?

From shell

# little
$ echo -n I | od -to2 | awk 'FNR==1{ print substr($2,6,1)}'
1

# big
$ echo -n I | od -to2 | awk 'FNR==1{ print substr($2,6,1)}'
0

Using tools

bala@IND68FL4H2-L:~/study$ lscpu | grep Endian
Byte Order: Little Endian

If you don't have tools?

If you don't have the above tools, a simple program would do the job

#include <stdio.h>
int main()
{
    int a = 1;
    char *c = (char *) &a;

    if (*c)
        puts("Little indian");
    else
        puts("Big indian");
}



Memory view

When you print the value of pointer *c, if it prints 1 Its a little endian. If it prints 0, its a big endian.

Credits

  • https://en.wikipedia.org/wiki/Endianness
  • https://unix.stackexchange.com/questions/88934/is-there-a-system-command-in-linux-that-reports-the-endianness

Comments

Popular posts from this blog

High Assurance boot in i.MX6 (Secure boot)

Enable log in U-Boot