[Question]
  [
Write a program or function that outputs an `L` if run on a little endian architecture or a `B` if run on a big endian architecture. Lower case output `l` or `b` is also acceptable.
There is no input.
Scoring is code golf, so the code with the fewest bytes wins.
**Edit:** *As per the comments below, I am clarifying that the entry must be able to run on either architecture.*
*I believe that there is only one answer that this affects, and that answer has clearly indicated that this is the case.*
  
[Answer]
# Python, 33 bytes
```
import sys
exit(sys.byteorder[0])
```
`sys.byteorder` is either `'little'` or `'big'` (and for those of you who will read this sentence without looking at the code, `[0]` means take the first character).
[Answer]
# C, 26 bytes
```
a=66<<24|76;f(){puts(&a);}
```
Assumes 32-bit `int` and ASCII characters. Tested on amd64 (little-endian) and mips (big-endian).
# GCC, 23 bytes
```
00000000: 613d 2742 0000 4c27 3b66 2829 7b70 7574  a='B..L';f(){put
00000010: 7328 2661 293b 7d                        s(&a);}
```
Suggested by feersum. The value of multi-character constants is implementation-dependent, but this seems to work in GCC. Tested on the same architectures.
[Answer]
# MATLAB / Octave, 24 bytes
```
[~,~,e]=computer;disp(e)
```
The [`computer`](http://es.mathworks.com/help/matlab/ref/computer.html) function gives information about, well, the computer it's running on. The third output is endianness: `L` or `B` for little- or big-endian respectively.
[Try it on Ideone](http://ideone.com/FpfrEb).
[Answer]
# JavaScript ES6, 50 bytes
```
_=>"BL"[new Int8Array(Int16Array.of(1).buffer)[0]]
```
I don’t know who decided it was a good idea for [TypedArray](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) objects to expose the native endianness of the interpreter, but here we are.
[Answer]
# C, ~~36~~ 35 bytes
**1 byte thanks to @algmyr and @owacoder.**
```
~~main(a){printf(\*(char\*)&a?"L":"B");}~~
main(a){putchar(66+10**(char*)&a);}
main(a){putchar("BL"[*(char*)&a]);}
```
* Version 1: [Ideone it!](http://ideone.com/uWicng)
* Version 2: [Ideone it!](http://ideone.com/OjpPrP)
* Version 3: [Ideone it!](http://ideone.com/DzmxIa)
Credits [here](https://stackoverflow.com/a/4181991/6211883).
Untested since I don't have a big-endian machine.
[Answer]
### Mathematica, 22
```
{B,L}[[$ByteOrdering]]
```
This probably breaks some rule about not using a built-in function but I'm not really interested in rube goldberging an alternative.
[Answer]
# C, 27
One byte longer, but here it is anyway:
```
f(){putchar(htons(19522));}
```
[Answer]
# PowerPC machine code - 20 bytes
PowerPC is bi-endian (the endianness can be set up at startup), so this code should conform to the challenge request of being able to run both on BE and LE machines. This is a function that *returns*1 `'L'` or `'B'` depending on the endianness currently set.
As a function, AFAICT it conforms the SVR4 PowerPC ABI (used by Linux on ppc32), the PowerOpen ABI (used e.g. by AIX) and the OS X ABI. In particular, it relies just on the fact that GPR 0 is a volatile scratch register, and GPR 3 is used to return "small" values.
```
00000000 <get_endian>:
   0:   7c 00 00 a6     mfmsr   r0
   4:   54 03 0f fe     rlwinm  r3,r0,1,31,31
   8:   1c 63 00 0a     mulli   r3,r3,10
   c:   38 63 00 42     addi    r3,r3,66
  10:   4e 80 00 20     blr
```
Now, it goes like this:
* the [MSR](https://en.wikipedia.org/wiki/Machine_state_register) is read into GP register 0; the MSR contains at bit 31 the endianness settings (0 = big endian; 1 = little endian);
* `rlwinm` extracts just that bit: it takes the value in GPR0, **r**otates **l**eft by 1 place (so that now is in position 0) and **m**asks it with 1 (the mask generated by those 31,312); the result is put into GP register 3;
* multiply the result by 10 and sum 66 (`'B'`) (10 is the difference between `'L'` and `'B'`)
* finally, return to the caller
---
### Notes
1. yep, the question does ask to *print*, but it's not clear how I should print stuff in assembly expected to run unmodified on different operating systems. =)
2. for those interested, see the [rlwinm](https://www.ibm.com/support/knowledgecenter/en/ssw_aix_71/com.ibm.aix.alangref/idalangref_rlwinm_rlinm_rtlwrdimm_instrs.htm) documentation; knowing next to nothing about PowerPC, I found this kind of instructions extremely interesting.
**2018 update**: Raymond Chen is publishing a series about the PowerPC architecture, you can find [here](https://blogs.msdn.microsoft.com/oldnewthing/20180810-00/?p=99465) his great post about `rlwinm` & friends.
[Answer]
# Java 8, ~~96, 64~~ 52 bytes
```
()->(""+java.nio.ByteOrder.nativeOrder()).charAt(0);
```
A golf based on [this SO answer](https://stackoverflow.com/a/9431652). All credit goes to @LeakyNun and @AlanTuning. Java keeps the loosing streak.
96 bytes:
```
 char e(){return java.nio.ByteOrder.nativeOrder().equals(java.nio.ByteOrder.BIG_ENDIAN)?'b':'l';}
```
64 bytes:
```
char e(){return(""+java.nio.ByteOrder.nativeOrder()).charAt(0);}
```
Untested b/c I don't have access to a big-endian machine.
[Answer]
# Perl, ~~38~~ 36 bytes
```
say+(pack"L",1 eq pack"V",1)?"L":"B"
```
Works by packing a number in the system's default byte order and comparing it to a number packed in little-endian byte order.
[Answer]
# (G)Forth, 24 bytes
```
here $4200004C , c@ emit
```
Assumes that cells are 32 bits (and that your Forth interpreter supports Gforth's [base prefixes](http://www.complang.tuwien.ac.at/forth/gforth/Docs-html/Number-Conversion.html#fnd-2)). It simply stores the number 0x4200004C in memory and then displays the byte at the low end.
[Answer]
# C, 34 bytes
This assumes ASCII character encoding
```
main(a){putchar(66+10**(char*)a);}
```
Call without argument.
Explanation:
On call, `a` will be 1. `*(char*)a` accesses the first byte of `a`, which on little-endian platforms will be 1, on big-endian platforms will be 0.
On big-endian platforms, this code will therefore pass 66 + 10\*0 = 66 to `putchar`. 66 is the ASCII code for `B`. On little-endian platforms, it will pass 66 + 10\*1 = 76, which is the ASCII code for `L`.
[Answer]
# C#, 60 52 bytes
C# is surprisingly competitive in this one.
```
char e=>System.BitConverter.IsLittleEndian?'L':'B';
```
Credits to Groo for the C#6 syntax.
60 bytes:
```
char e(){return System.BitConverter.IsLittleEndian?'L':'B';}
```
[Answer]
# Julia, ~~24~~ 19 bytes
```
()->ntoh(5)>>55+'B'
```
This is an anonymous function that takes no input and returns a `Char`. To call it, assign it to a variable. It assumes that integers are 64-bit.
The `ntoh` function converts the endianness of the value passed to it from network byte order (big endian) to that used by the host computer. On big endian machines, `ntoh` is the identity function since there's no conversion to be done. On little endian machines, the bytes are swapped, so `ntoh(5) == 360287970189639680`. Which is also, perhaps more readably, equal to: `0000010100000000000000000000000000000000000000000000000000000000` in binary.
If we right bit shift the result of `ntoh(5)` by 55, we'll get 0 on big endian machines and 10 on little endian machines. Adding that to the character constant `'B'`, we'll get `'B'` or `'L'` for big or little endian machines, respectively.
Saved 5 bytes thanks to FryAmTheEggman and Dennis!
[Answer]
# PHP 5.6, ~~41~~ 31 bytes (thx to isertusernamehere)
`<?=unpack(S,"\x01\x00")[1]-1?B:L;`
# PHP 7, 41 bytes
`echo unpack('S',"\x01\x00")[1]-1?'B':'L';`
Idea stolen from: <https://stackoverflow.com/a/24785578/2496717>
[Answer]
## Common Lisp, 27 bytes
*Tested on SBCL and ECL. For a portable approach, one should use [trivial-features](https://github.com/trivial-features/trivial-features/blob/master/SPEC.md).*
```
(lambda()'L #+big-endian'B)
```
The `#+` notation is a [read-time condition](http://clhs.lisp.se/Body/01_ebaa.htm), that *reads* the next form only if the conditional expression evaluates to true. Here the condition is the whole `#+big-endian` text which means that the test is satisfied if the `:big-endian` keyword belongs to the [`*FEATURES*`](http://clhs.lisp.se/Body/v_featur.htm) list (a list which contains among other things platform-specific information). The following expression is `'B`, which is either read or skipped according to the outcome of the test. 
If your platform is big-endian and you write the above form in the REPL, it is exactly as if you wrote the following:
```
CL-USER>(lambda()'L 'B)
```
 (NB.`CL-USER>` is the prompt)
A function's body is an implicit [`PROGN`](http://clhs.lisp.se/Body/s_progn.htm), meaning that only the evaluation of the last expression is returned. So the above actually returns symbol ̀`B`.
If however the read-time condition evaluates to false, the form reads as if you wrote:
```
CL-USER>(lambda()'L)
```
... which simply returns symbol `L`.
[Answer]
## ARMv6 and later: 20 bytes machine code
```
0xE10F1000 : MRS   r0,CPSR        ; read current status register
0xE3110C02 : TST   r0,#1<<9       ; set Z flag if bit 9 set
0x03A0004C : MOVEQ r0,#'L'        ; return 'L' if Z clear
0x13A00042 : MOVNE r0,#'B'        ; return 'B' if Z set
0xEBxxxxxx : BL    putchar        ; print it (relative branch)
```
Untested, as I don't have a suitable machine to hand. Bit 9 of the CPSR gives the current load/store endianness.
[Answer]
# Perl 5, 21 + 1 = 22 bytes
Run with `perl -E`.
```
say ord pack(S,1)?L:B
```
Tested on amd64 (little-endian) and mips (big-endian).
[Answer]
# R, ~~28~~ 23 bytes
```
substr(.Platform$endian,1,1)
```
`.Platform$endian` returns `big` if run on big endian and `little` if on little. Here, using `substr`, it returns the first letter of the output, so either `b` or `l`.
As `endian` is the only object starting with an `e` contained in object `.Platform` we can reduce it thanks to partial matching to the following 23 bytes code:
```
substr(.Platform$e,1,1)
```
[Answer]
# Clojure, ~~46~~ 44 bytes
```
#(nth(str(java.nio.ByteOrder/nativeOrder))0)
```
This is a function that uses the Java builtin to get the endianness of the machine. Then get the string representation of it which will be either `"LITTLE_ENDIAN"` or `"BIG_ENDIAN"` and take the first character of whichever string is chosen and return that.
Saved 2 bytes thanks to @[cliffroot](https://codegolf.stackexchange.com/users/53197/cliffroot).
[Answer]
# Ruby, ~~31~~30 chars.
```
puts [1].pack("s")>"\01"??L:?B
```
Hmm, must be a better way. Count 5 characters less if the char need not to be output as I think other solutions omit printing. i.e. remove the "puts " part if you don't want anything printed.
[Answer]
# Bash (and other unix shells), 32 (33) bytes
First and second attempt:
```
case `echo|od` in *5*)echo B;;*)echo L;;esac # portable
[[ `echo|od` =~ 5 ]]&&echo B||echo L         # non-portable
```
Thanks to Dennis, shorter version:
```
od<<<a|grep -q 5&&echo L||echo B  # non-portable
echo|od|grep -q 5&&echo B||echo L # portable
```
The `echo` utility outputs a newline, with hex value `0A`, and no other output. For `<<<a` it is `61 0A`.
The `od` utility, by default, interprets the input as two-byte words, zero-padded if the number of bytes is odd, and converts to octal. This results in the output of echo being interpred as `0A 00`, which is converted to `005000` as big-endian or `000012` in little-endian. `61 0A` becomes `005141` in little-endian and `060412` in big-endian. The full output of od also includes address and size data meaning we cannot use `0`, `1`, or `2` for the test.
The command is well-defined to expose the system's endianness. From [the standard](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/od.html):
> 
> The byte order used when interpreting numeric values is implementation-defined, but shall correspond to the order in which a constant of the corresponding type is stored in memory on the system.
> 
> 
> 
## Compatibility notes
I am not certain if putting `echo|od` in backquotes with no double quotes around them [which results in a three-word argument to `case`] is supported on all systems. I am not certain if all systems support shell scripts with no terminating newline. I am mostly certain but not 100% of the behavior of od with adding the padding byte on big-endian systems. If needed, `echo a` can be used for the portable versions. All of the scripts work in bash, ksh, and zsh, and the portable ones work in dash.
[Answer]
## PHP, 16 bytes
```
<?=pack(S,14)^B;
```
This uses two tricks not used in the two existing PHP answers:
1. The value passed to pack() or unpack() can be anything, not just 0 or 1.
2. The bitwise XOR operator (`^`) works on strings, and the result is only as long as the shorter string, avoiding the need for a string indexing or ternary operator.
[Answer]
# Node, 42 bytes
```
n=>require('os').endianness()[0]
```
Pro: there's a builtin. Con: property names are very long
[Answer]
# PowerShell, 44 bytes
```
[char](66+10*[BitConverter]::IsLittleEndian)
```
Character 66 is `B`, and 10 characters later is number 76, `L`. A Boolean value of "true" becomes `1` when cast to a number. `BitConverter` is a standard .NET class.
[Answer]
## Bash, 27 bytes
```
iconv -tucs2<<<䉌|head -c1
```
`䉌` ([U+424C](http://www.unicode.org/cgi-bin/GetUnihanData.pl?codepoint=424C)) is encoded as three UTF-8 bytes: `E4 89 8C`.
It is assumed that `iconv` uses the function from glibc and that a UTF-8 locale is used.
[Answer]
# Racket, ~~35~~ 28 bytes
```
(if(system-big-endian?)'B'L)
```
`'B` or `'L` respectively. Let me know if this is not specific enough :)
[Answer]
# PHP, 22 bytes
```
<?=ord(pack(S,1))?L:B;
```
[Answer]
## Haskell (using GHC-only size-restricted numeric types), 75 bytes
```
import Unsafe.Coerce
import GHC.Int
f="BL"!!fromEnum(unsafeCoerce 1::Int8)
```
(not tested on a big-endian architecture, but it seems like it *ought* to work!)
[Answer]
## K, 15 bytes
```
    ("bl")@*6h$-8!`
    ,"l"
```
Explanation;
```
    From right to left;
    -8!`       /serialises the back tick and returns (0x010000000a000000f500)
    6h$-8!`    /This casts the result to `int which is type 6h(could have used `int$-8!`) - the result is (1 0 0 0 10 0 0 0 245 0i)
    *6h$-8!`   /* means first, we take the first item which is 1. Since it will be either 1 or 0, we can use it to index (@) into the two element list on the left i.e. ("bl")1 returns "l" and ("bl")0 returns b
   
    
    
```
]