How to check whether a system is big endian or little endian?
-
1C: https://stackoverflow.com/questions/2100331/c-macro-definition-to-determine-big-endian-or-little-endian-machine C++ https://stackoverflow.com/questions/1001307/detecting-endianness-programmatically-in-a-c-program Shell: https://serverfault.com/questions/163487/how-to-tell-if-a-linux-system-is-big-endian-or-little-endian – Ciro Santilli OurBigBook.com Mar 24 '20 at 18:29
19 Answers
-
17Good answer, but this provides a nice diagram of what's going on: http://stackoverflow.com/a/12792301/803801 – gsgx Oct 15 '13 at 23:17
-
4Can you tell me why we can't just use `(char)n == 1` ? Why do we have to get the address, convert it to a `char` pointer and then dereference? Won't this be implicitly done `(char)n` is used? – J...S Dec 28 '18 at 11:52
-
@J...S Just using a char cast seems to work fine. I haven't tested it on a big-endian system though. – FelisPhasma Mar 03 '19 at 21:36
-
5Just using a char does not work. As casting to char causes the system to convert from an int to a char. It will always return true. Using a cast to pointer and de-reference places the pointer to the first byte of N and then de-references the first byte. – Garret Gang Feb 14 '20 at 18:31
-
2A cast isn't a conversion. If Big Endian, then the first byte of N is a zero - so how does that result in true? – belwood Feb 14 '20 at 19:19
-
A cast from `int` to `char` will not work; that will give you the least significant byte. That tells you nothing about which end the least significant byte came from. – jamesdlin Mar 17 '21 at 21:41
-
The OS/360 programmer who gave me this code > 25 years ago said it works because it's not casting from `int` to `char`, but rather casting address of int to address of char. @GarretGang comment above explains it. – belwood Apr 11 '22 at 13:55
-
I just wrote a bit about this and offered an example in Go with diagrams and such: https://pcolladosoto.github.io/2022/04/26/checking-endianness.html. Hope somebody finds it useful! – Collado Apr 26 '22 at 16:20
In Python:
from sys import byteorder
print(byteorder)
# will print 'little' if little endian

- 5,876
- 4
- 31
- 66
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.

- 1,612
- 7
- 29
- 47
-
3i think this is not standard compliant though: you may only read the field from an enum that you last have written to otherwise undefined behavior is possible, or am i mistaken? – worenga Mar 14 '16 at 11:53
-
1unions does not specify "justifying" in standard. This is not correct solution (it may work accidentally in specific OS/compiller). – Mariusz Jaskółka Dec 30 '16 at 19:46
-
Can some please explain the logic in the above program. If initialising the member variable i, with x.1=1, then how x.c[0] is turning out to be 1. Am I missing some point here? my understanding is that union take the memory of the largest data type and depending upon that we can access it. Only one member can be accessed. Any response will be highly appreaciated – Vivek Singh Feb 13 '18 at 08:44
-
1@VivekSingh as you said union take the memory of the largest data type and depending upon that we can access it. So the memory will be same for both the data types. So we are accessing as int and assigning it as 1. So in 4 byte only 1 byte part will have single 1. When we access as char it takes only 1 byte. – Neeraj Feb 20 '18 at 07:15
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";
}

- 25,259
- 5
- 41
- 83
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.

- 13,643
- 2
- 36
- 50
If you are using .NET: Check the value of BitConverter.IsLittleEndian
.

- 687,336
- 108
- 737
- 1,005
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

- 81
- 1
- 4
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 */
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";
}
In Rust (byteorder crate required):
use std::any::TypeId;
let is_little_endian = TypeId::of::<byteorder::NativeEndian>() == TypeId::of::<byteorder::LittleEndian>();

- 2,179
- 2
- 18
- 27
-
1You don't need a crate now: #[cfg(target_endian = "little")] or if cfg!((target_endian = "little") will work. – Squirrel Apr 23 '21 at 19:08
Using Macro,
const int isBigEnd=1;
#define is_bigendian() ((*(char*)&isBigEnd) == 0)

- 602
- 6
- 10
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.

- 121
- 6
-
1Hey, man, It is not your own code, you just copy and paste. You should at least note the source of the code. – Hu Xixi Apr 21 '20 at 11:27
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.

- 11,247
- 10
- 69
- 89
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

- 41
- 2
C logic to check whether your processor follows little endian or big endian
unsigned int i =12345;
char *c = (char *)&i; // typecast int to char* so that it points to first bit of int
if(*c != 0){ // If *c points to 0 then it is Big endian else Little endian
printf("Little endian");
}
else{
printf("Big endian");
}
Hope this helps. Was one of the question asked in my interview for the role of embedded software engineer role

- 29
- 5
-
1This is pretty similar in approach to the top answer and actually the same approach has been shown multiple times already. I don't see how this answer adds anything to what is already here. – DavidW Aug 12 '22 at 07:01
in C with GCC 9.4:
#include <endian.h>
#if __BYTE_ORDER == __LITTLE_ENDIAN
...
#endif
N.B. trap for the unwary : DON'T do this:
#ifdef __LITTLE_ENDIAN
because both __LITTLE_ENDIAN and __BIG_ENDIAN are defined in endian.h! Also LITTLE_ENDIAN / BIG_ENDIAN (no leading underscores are defined)

- 289
- 1
- 12
In case if you want to check it on a linux machine you can use below mentioned cmd
lscpu | grep "Byte Order"
After running this cmd you will get the output like mentioned below
Byte Order: Little Endian
Hope it helps

- 159
- 2
- 8