How to check whether a system is big endian or little endian?

Endianness

Endianness Problem Overview


How to check whether a system is big endian or little endian?

Endianness Solutions


Solution 1 - Endianness

In C, C++

int n = 1;
// little endian if true
if(*(char *)&n == 1) {...}

See also: Perl version

Solution 2 - Endianness

In Python:

from sys import byteorder
print(byteorder)
# will print 'little' if little endian

Solution 3 - Endianness

Another C code using union

union {
	int i;
	char c[sizeof(int)];
} x;
x.i = 1;
if(x.c[0] == 1)
	printf("little-endian\n");
else	printf("big-endian\n");

It is same logic that belwood used.

Solution 4 - Endianness

A one-liner with Perl (which should be installed by default on almost all systems):

perl -e 'use Config; print $Config{byteorder}'

If the output starts with a 1 (least-significant byte), it's a little-endian system. If the output starts with a higher digit (most-significant byte), it's a big-endian system. See documentation of the Config module.

Solution 5 - Endianness

In C++20 use std::endian:

#include <bit>
#include <iostream>

int main() {
    if constexpr (std::endian::native == std::endian::little)
        std::cout << "little-endian";
    else if constexpr (std::endian::native == std::endian::big)
        std::cout << "big-endian";
    else
        std::cout << "mixed-endian";
}

Solution 6 - Endianness

If you are using .NET: Check the value of BitConverter.IsLittleEndian.

Solution 7 - Endianness

In Linux,

static union { char c[4]; unsigned long mylong; } endian_test = { { 'l', '?', '?', 'b' } };
#define ENDIANNESS ((char)endian_test.mylong)

if (ENDIANNESS == 'l') /* little endian */
if (ENDIANNESS == 'b') /* big endian */

Solution 8 - Endianness

In Rust (no crates or use statements required)

In a function body:

if cfg!(target_endian = "big") {
    println!("Big endian");
} else {
    println!("Little endian");
}

Outside a function body:

#[cfg(target_endian = "big")]
fn print_endian() {
    println!("Big endian")
}

#[cfg(target_endian = "little")]
fn print_endian() {
    println!("Little endian")
}

This is what the byteorder crate does internally: https://docs.rs/byteorder/1.3.2/src/byteorder/lib.rs.html#1877

Solution 9 - Endianness

A C++ solution:

namespace sys {

const unsigned one = 1U;

inline bool little_endian()
{
	return reinterpret_cast<const char*>(&one) + sizeof(unsigned) - 1;
}

inline bool big_endian()
{
	return !little_endian();
}

} // sys

int main()
{
	if(sys::little_endian())
		std::cout << "little";
}

Solution 10 - Endianness

Using Macro,

const int isBigEnd=1;
#define is_bigendian() ((*(char*)&isBigEnd) == 0)

Solution 11 - Endianness

In C

#include <stdio.h> 

/* function to show bytes in memory, from location start to start+n*/
void show_mem_rep(char *start, int n) 
 { 
      int i; 
      for (i = 0; i < n; i++) 
	  printf("%2x ", start[i]); 
      printf("\n"); 
 } 

/*Main function to call above function for 0x01234567*/
int main() 
{ 
   int i = 0x01234567; 
   show_mem_rep((char *)&i, sizeof(i));  
   return 0; 
} 

When above program is run on little endian machine, gives “67 45 23 01” as output , while if it is run on big endian machine, gives “01 23 45 67” as output.

Solution 12 - Endianness

In Rust (byteorder crate required):

use std::any::TypeId;

let is_little_endian = TypeId::of::<byteorder::NativeEndian>() == TypeId::of::<byteorder::LittleEndian>();

Solution 13 - Endianness

A compilable version of the top answer for n00bs:

#include <stdio.h>

int main() {
    int n = 1;

    // little endian if true
    if(*(char *)&n == 1) {
        printf("Little endian\n");
    } else {
        printf("Big endian\n");
    }   
}

Stick that in check-endianness.c and compile and run:

$ gcc -o check-endianness check-endianness.c
$ ./check-endianness

This whole command is a copy/pasteable bash script you can paste into your terminal:

cat << EOF > check-endianness.c
#include <stdio.h>
int main() {
    int n = 1;
    // little endian if true
    if(*(char *)&n == 1) {
        printf("Little endian\n");
    } else {
        printf("Big endian\n");
    }   
}
EOF

gcc -o check-endianness check-endianness.c \
 && ./check-endianness \
 && rm check-endianness check-endianness.c

The code is in a gist here if you prefer. There is also a bash command that you can run that will generate, compile, and clean up after itself.

Solution 14 - Endianness

In Nim,

echo cpuEndian

It is exported from the system module.

Solution 15 - Endianness

In Powershell

[System.BitConverter]::IsLittleEndian

Solution 16 - Endianness

In bash (from How to tell if a Linux system is big endian or little endian?):

endian=`echo -n "I" | od -to2 | head -n1 | cut -f2 -d" " | cut -c6`

if [ "$endian" == "1" ]; then
  echo "little-endian"
else
  echo "big-endian"
fi

Solution 17 - Endianness

All the answers using a program to find endianess at runtime is wrong! The fact whether a machine is big endian or little endian is hidden from the programmer, by the compiler. On a big-endian machine the typecast will again return 1, because the compiler knows that the machine is big endian and the casting will fetch the higher memory address. Only way to find the endianess is to fetch the system's configuration or environment variable. Similar to some of the answers above like the one liner perl answer etc.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionanandView Question on Stackoverflow
Solution 1 - EndiannessbelwoodView Answer on Stackoverflow
Solution 2 - EndiannessNikita PestrovView Answer on Stackoverflow
Solution 3 - EndiannessNeerajView Answer on Stackoverflow
Solution 4 - EndiannessweibeldView Answer on Stackoverflow
Solution 5 - EndiannessEvgView Answer on Stackoverflow
Solution 6 - EndiannessGuffaView Answer on Stackoverflow
Solution 7 - EndiannessRigel HsuView Answer on Stackoverflow
Solution 8 - EndiannessQuinnView Answer on Stackoverflow
Solution 9 - EndiannessGalikView Answer on Stackoverflow
Solution 10 - EndiannessEswaran PandiView Answer on Stackoverflow
Solution 11 - EndiannessRamkumar lodhiView Answer on Stackoverflow
Solution 12 - EndiannessmakerjView Answer on Stackoverflow
Solution 13 - EndiannessFreedom_BenView Answer on Stackoverflow
Solution 14 - EndiannessmodesittView Answer on Stackoverflow
Solution 15 - EndiannessJuan M. EloseguiView Answer on Stackoverflow
Solution 16 - EndiannessrchekalukView Answer on Stackoverflow
Solution 17 - EndiannessHemanthView Answer on Stackoverflow