37

I'm going though a computers system course and I'm trying to establish, for sure, if my AMD based computer is a little-endian machine? I believe it is because it would be Intel-compatible.

Specifically, my processor is an AMD 64 Athlon x2.

I understand that this can matter in C programming. I'm writing C programs and a method I'm using would be affected by this. I'm trying to figure out if I'd get the same results if I ran the program on an Intel based machine (assuming that is little endian machine).

Finally, let me ask this: Would any and all machines capable of running Windows (XP, Vista, 2000, Server 2003, etc) and, say, Ubuntu Linux desktop be little endian?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Frank V
  • 25,141
  • 34
  • 106
  • 144

9 Answers9

78

All x86 and x86-64 machines (which is just an extension to x86) are little-endian.

You can confirm it with something like this:

#include <stdio.h>
int main() {
    int a = 0x12345678;
    unsigned char *c = (unsigned char*)(&a);
    if (*c == 0x78) {
       printf("little-endian\n");
    } else {
       printf("big-endian\n");
    }
    return 0;
}
Techwolf
  • 1,228
  • 13
  • 32
Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
16

An easy way to know the endiannes is listed in the article Writing endian-independent code in C

const int i = 1;
#define is_bigendian() ( (*(char*)&i) == 0 )
FCo
  • 457
  • 3
  • 9
12

Assuming you have Python installed, you can run this one-liner, which will print "little" on little-endian machines and "big" on big-endian ones:

python -c "import struct; print 'little' if ord(struct.pack('L', 1)[0]) else 'big'"
Ben Hoyt
  • 10,694
  • 5
  • 60
  • 84
9

"Intel-compatible" isn't very precise.

Intel used to make big-endian processors, notably the StrongARM and XScale. These do not use the IA32 ISA, commonly known as x86.

Further back in history, Intel also made the little-endian i860 and i960, which are also not x86-compatible.

Further back in history, the prececessors of the x86 (8080, 8008, etc.) are not x86-compatible either. Being 8-bit processors, endianness doesn't really matter...

Nowadays, Intel still makes the Itanium (IA64), which is bi-endian: normal operation is big-endian, but the processor can also run in little-endian mode. It does happen to be able to run x86 code in little-endian mode, but the native ISA is not IA32.

To my knowledge, all of AMD's processors have been x86-compatible, with some extensions like x86_64, and thus are necessarily little-endian.

Ubuntu is available for x86 (little-endian) and x86_64 (little-endian), with less complete ports for ia64 (big-endian), ARM(el) (little-endian), PA-RISC (big-endian, though the processor supports both), PowerPC (big-endian), and SPARC (big-endian). I don't believe there is an ARM(eb) (big-endian) port.

ephemient
  • 198,619
  • 38
  • 280
  • 391
  • Wow, thank you for the detail. This is great support information. – Frank V Jun 22 '09 at 16:13
  • 2
    Two minor corrections: endianness matters also for 8 bit processors as some instructions refer to 16 bit quantities like addresses (`LDA $1234` (loading a byte from address $1234) will be coded `AD 34 12` on the 6502. And AMD did have another architecture than x86 it was the 29000 series RISC processors that was very popular in embedded designs like laser printers. – Patrick Schlüter Mar 13 '10 at 08:39
  • @tristopia Thanks for the info, I wasn't aware of all of that. – ephemient Mar 13 '10 at 09:31
4

The below snippet of code works:

#include <stdio.h>

int is_little_endian() {
  short x = 0x0100; //256
  char *p = (char*) &x;
  if (p[0] == 0) {
    return 1;
  }
  return 0;
}

int main() {
  if (is_little_endian()) {
    printf("Little endian machine\n");
  } else printf("Big endian machine\n");
  return 0;
}

The "short" integer in the code is 0x0100 (256 in decimal) and is 2 bytes long. The least significant byte is 00, and the most significant is 01. Little endian ordering puts the least significant byte in the address of the variable. So it just checks whether the value of the byte at the address pointed by the variable's pointer is 0 or not. If it is 0, it is little endian byte ordering, otherwise it's big endian.

tf3
  • 447
  • 1
  • 4
  • 16
3

In answer to your final question, the answer is no. Linux is capable of running on big endian machines like e.g., the older generation PowerMacs.

1800 INFORMATION
  • 131,367
  • 29
  • 160
  • 239
  • Is that distribution, that I linked to, able to run on big endian. Thank you for address that part of my question. – Frank V Jun 21 '09 at 23:11
  • I think they're asking if those operating systems can run on little endian machines, which they can. In fact, I think they have to make special versions for the older generation PowerMacs because the PowerPC architecture is big-endian. – mnuzzo Jun 21 '09 at 23:12
  • 2
    Now that Ubuntu has ARM support, its possible for "Ubuntu" to run on a big endian processor. Recent ARM cores can run in either little or big endian mode. – Mark Jun 21 '09 at 23:15
  • PowerPC are bi-endian: that is they can run in big or little endian mode - [see here](http://en.wikipedia.org/wiki/Endianness) - just fore completeness of information. Cheers – GMasucci Aug 05 '13 at 10:56
  • Linux also runs/ran on MC68K (68000, 68010, etc.) processors, which are little endian. – Jiminion Apr 11 '22 at 18:20
2

You have to download a version of Ubuntu designed for big endian machines. I know only of the PowerPC versions. I'm sure you can find some place which has a more generic big-endian implementation.

mnuzzo
  • 3,529
  • 4
  • 26
  • 29
  • Ubuntu's second class ports include ia64, armel, hppa, powerpc, and sparc. In earlier releases, PowerPC was a first class port, and there was one release where SPARC was up there too. – ephemient Jun 22 '09 at 01:27
1
/* by Linas Samusas  */

#ifndef _bitorder 
#define _bitorder 0x0008

#if (_bitorder > 8)
#define BE
#else
#define LE
#endif

and use this

#ifdef LE
#define Function_Convert_to_be_16(value)  real_function_to_be_16(value)
#define Function_Convert_to_be_32(value)  real_function_to_be_32(value)
#define Function_Convert_to_be_64(value)  real_function_to_be_64(value)
#else
#define Function_Convert_to_be_16
#define Function_Convert_to_be_32
#define Function_Convert_to_be_64
#endif

if LE

unsigned long number1 = Function_Convert_to_be_16(number2);

*macro will call real function and it will convert to BE

if BE

unsigned long number1 = Function_Convert_to_be_16(number2);

*macro will be defined as word not a function and your number will be between brackets

Linas
  • 11
  • 1
-1

We now have std::endian!

constexpr bool is_little = std::endian::native == std::endian::little;

https://en.cppreference.com/w/cpp/types/endian

sleep
  • 4,855
  • 5
  • 34
  • 51
  • 1
    Cool, but this question isn't actually about how to determine / detect endianness at compile time. There's very likely another Q&A where this would be more useful. (A comment on this question with a link to such a Q&A would be good.) – Peter Cordes Feb 10 '22 at 16:25