40

Does anyone know of a command that reports whether a system is Big Endian or Little Endian, or is the best option a technique like this using Perl or a string of commands?

Perl

# little
$ perl -MConfig -e 'print "$Config{byteorder}\n";'
12345678

# big
$ perl -MConfig -e 'print "$Config{byteorder}\n";'
87654321

od | awk

# 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

References

11
  • What's wrong with the od method? It's simple and works everywhere. It's what I thought of before reading the body of your question. Aug 31, 2013 at 10:57
  • @Gilles - nothing really, feels a little like a hack (at least to me). True it would appear to be portable on other systems such as Solaris + AIX, but it seemed like a system's Endianness should be a little more explicitly determined like 32-bit vs. 64-bit, so I was a little surprised that it wasn't. The newer lscpu method is more what I would come to expect.
    – slm
    Aug 31, 2013 at 12:11
  • Endianness is in practice easier to determine than word size, because you'll have a hard time finding platforms that aren't either little-endian or big-endian (at least for integers, floats are another matter) whereas there are plenty of mixes between 32-bit and 64-bit (CPU, kernel, userland, a given process). Aug 31, 2013 at 12:15
  • @Gilles - yes my view of the world is probably slighted since I primarily grew up with either Solaris or Linux. Not so much beyond that.
    – slm
    Aug 31, 2013 at 12:26
  • the od approach should work on most open systems, not only linux, which would be the case with using lscpu. So what is "best" depends on the circumstances.
    – MattBianco
    Aug 8, 2014 at 11:06

5 Answers 5

49

lscpu

The lscpu command shows (among other things):

Byte Order:            Little Endian

Systems this is known to work on

  • CentOS 6
  • Ubuntu (12.04, 12.10, 13.04, 13.10, 14.04)
  • Fedora (17,18,19)
  • ArchLinux 2012+
  • Linux Mint Debian (therefore assuming Debian testing as well).

Systems this is known to not work on

  • Fedora 14
  • CentOS 5 (assuming RHEL5 because of this)

Why the apparent differences across distros?

After much digging I found out why. It looks like version util-linux version 2.19 was the first version that included the feature where lscpu shows you the output reporting your system's Endianness.

As a test I compiled both version 2.18 and 2.19 on my Fedora 14 system and the output below shows the differences:

util-linux 2.18

$ util-linux-ng-2.18/sys-utils/lscpu 
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
CPU(s):                4
Thread(s) per core:    2
Core(s) per socket:    2
CPU socket(s):         1
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 37
Stepping:              5
CPU MHz:               1199.000
Virtualization:        VT-x
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              3072K
NUMA node0 CPU(s):     0-3

util-linux 2.19

$ util-linux-2.19/sys-utils/lscpu 
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                4
On-line CPU(s) list:   0-3
Thread(s) per core:    2
Core(s) per socket:    2
CPU socket(s):         1
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 37
Stepping:              5
CPU MHz:               2667.000
BogoMIPS:              5320.02
Virtualization:        VT-x
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              3072K
NUMA node0 CPU(s):     0-3

The above versions were downloaded from the kernel.org website.

9
  • Thanks David, I missed that when I was greping that file. Must be going blind 8-)
    – slm
    Aug 31, 2013 at 0:28
  • I see why I missed it. My Fedora 14 system's lscpu doesn't show that value, however my Ubuntu 12.10 system does. If you don't mind I might take your answer and split it into sections for the different systems and ways to do it on each.
    – slm
    Aug 31, 2013 at 0:31
  • @slm Sure, go ahead. For reference, lscpu works on Archlinux too. Aug 31, 2013 at 0:33
  • Which version of Arch?
    – slm
    Aug 31, 2013 at 0:35
  • Arch doesn't have versions; it's a rolling release...
    – jasonwryan
    Aug 31, 2013 at 0:57
10

Using python:

$ python -c "import sys;print sys.byteorder"
little

or:

printf '\1' | od -dAn
1

where 1 is for little endian and 00256 for big endian.

Or using a shorter perl version:

$ perl -V:byteorder
byteorder='12345678';
7

One method I found on Debian/Ubuntu systems is to run this command:

$ dpkg-architecture
DEB_BUILD_ARCH=amd64
DEB_BUILD_ARCH_BITS=64
DEB_BUILD_ARCH_CPU=amd64
DEB_BUILD_ARCH_ENDIAN=little
DEB_BUILD_ARCH_OS=linux
DEB_BUILD_GNU_CPU=x86_64
DEB_BUILD_GNU_SYSTEM=linux-gnu
DEB_BUILD_GNU_TYPE=x86_64-linux-gnu
DEB_BUILD_MULTIARCH=x86_64-linux-gnu
DEB_HOST_ARCH=amd64
DEB_HOST_ARCH_BITS=64
DEB_HOST_ARCH_CPU=amd64
DEB_HOST_ARCH_ENDIAN=little
DEB_HOST_ARCH_OS=linux
DEB_HOST_GNU_CPU=x86_64
DEB_HOST_GNU_SYSTEM=linux-gnu
DEB_HOST_GNU_TYPE=x86_64-linux-gnu
DEB_HOST_MULTIARCH=x86_64-linux-gnu

This will show you the words little or big depending on the architecture your system is comprised of:

$ dpkg-architecture | grep -i end
DEB_BUILD_ARCH_ENDIAN=little
DEB_HOST_ARCH_ENDIAN=little
5

A POSIX Shell & C solution:

cat << EOF > foo.c

#include <endian.h>
#include <stdio.h>

int main() {
  printf("Byte Order: ");
  if (BYTE_ORDER == LITTLE_ENDIAN) 
    printf("little");
  else {
    if (BYTE_ORDER == BIG_ENDIAN)
      printf("big");
    else
      printf("unknown");
  }
  printf(" endian.\n");
  return 0;
}
EOF

gcc -D__USE_POSIX foo.c
./a.out
2

If you are on a system that does not have endian.h:

#include <stdio.h>

int main() {
  int test = 0;
  char *bytes = (char *) &test;
  *bytes = 0x1;

  printf("Byte Order: ");
  if (test == 1){
    printf("little");
  }
  else {
      printf("big");
  }
  printf(" endian.\n");
  return 0;
}
2
  • 2
    What no love for VAX middle-endian?
    – thrig
    Mar 29, 2017 at 17:43
  • Well spotted, I was so absorbed in my current Intel->PowerPC issues, I had not conceived of something so horrible. Apr 1, 2017 at 9:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .