57
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ The memory of the box. I'm not sure what you mean \$\endgroup\$ – Liam Jul 27 '16 at 23:38
  • 7
    \$\begingroup\$ I think this is an interesting challenge (and it's way out of the norm) but it's going to be extremely hard to test it. Especially since most of the code will end up being extremely platform specific. \$\endgroup\$ – DJMcMayhem Jul 27 '16 at 23:41
  • 7
    \$\begingroup\$ Yeah I think it'll be interesting because hopefully it's something that esolangs and golfing languages have a harder time with than 'normal' languages. I have a feeling that they won't be that hard to verify. Worst case they should be verifiable by hand \$\endgroup\$ – Liam Jul 27 '16 at 23:43
  • 2
    \$\begingroup\$ I guess an answer based on a specific machine code is forbidden? Otherwise B0 4C C3, which is mov al, 'L' / ret or unsigned char f(){ return 'L'; }, would be a valid x86 answer. \$\endgroup\$ – Margaret Bloom Jul 28 '16 at 7:28
  • 5
    \$\begingroup\$ Bravo! for a code golf challenge in which general-purpose programming languages can be competitive. \$\endgroup\$ – PellMell Jul 29 '16 at 21:16

38 Answers 38

1
\$\begingroup\$

Dyalog APL, 28 19 16 15 bytes

Both solutions below require ⎕IO←0, which is default on many systems.

In cooperation with my CTO:

'BL'⊃⍨⊃11⎕DR'⊢'

11⎕DR'⊢' interpret the character as 1 bit Boolean
  {0,0,1,0,0,0,1,0,1,0,1,0,0,0,1,0} on big-endian systems
  {1,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0} on little-endian systems

pick the first number, i.e. 0 on big endian systems, and 1 on little-endian systems

'BL'⊃⍨ use that to pick from the string

This one is a bit interesting, as it requires storing in the "Classic" character set to be only 15 bytes, but must be executed in the "Unicode" interpreter to get a two byte representation. However, this is not a contradiction as the Unicode interpreter can load Classic workspaces.


Old solution:

'LB'⊃⍨'E'⎕FPROPS⎕D⎕FCREATE 0

⎕D⎕FCREATE 0 create an APL component file with the name 0123456789
'E'⎕FPROPS query endian-ness (0=little, 1=big)
'LB'⊃⍨ pick from the string

\$\endgroup\$
1
\$\begingroup\$

GNU Utils, 35 bytes

lscpu|grep -o ':.*n'|grep -Eo 'L|B'

Tested on Linux kali 4.6.0-kali1-amd64 #1 SMP Debian 4.6.4-1kali1 (2016-07-21) x86_64 GNU/Linux, but I'm pretty sure the output should be fairly standard on most GNU/Linux distros.

\$\endgroup\$
1
\$\begingroup\$

Kotlin, 42 bytes

{(""+java.nio.ByteOrder.nativeOrder())[0]}

Pretty straightforward. Uses "native" Java API. This is a lambda of () -> Char type.

String templates don't help much, unfortunately (still 42 bytes):

{"${java.nio.ByteOrder.nativeOrder()}"[0]}

Function approach (48 bytes):

fun e()=(""+java.nio.ByteOrder.nativeOrder())[0]
\$\endgroup\$
0
\$\begingroup\$

Nim, 21 20 bytes

($cpuEndian)[0].echo

Takes the cpuEndian builtin (either littleEndian or bigEndian depending on the target CPU), stringifies it with $, takes its 0th character (l or b depending on the target CPU), and uses Nim's uniform function call syntax to echo the result.

\$\endgroup\$
0
\$\begingroup\$

Lua, 31 bytes

print(("H"):pack(19522):sub(2))

Lua 5.3 is required.

\$\endgroup\$
0
\$\begingroup\$

VBA, 151 Bytes

Type x
q As Long
End Type
Type z
w As Integer
End Type
Sub q()
Dim i As x, y As z
y.w=1
LSet i=y
Debug.? IIf(i.q=1,"L","B")
End Sub
\$\endgroup\$
0
\$\begingroup\$

Tcl, 46 bytes

puts [string index $tcl_platform(byteOrder) 0]

(Nothing fancy. Just in case someone wants to know how to do it in Tcl.)

\$\endgroup\$
0
\$\begingroup\$

C# 50 49 bytes

Works, but has to be marked as unsafe

char i(){var a=0x0001;return*(byte*)&a>0?'L':'B';}

Updated:

unsafe char i(){int a=1;return"BL"[*(byte*)&a];}
\$\endgroup\$
  • \$\begingroup\$ You can use a classic trick since this is really producing 0 or 1, and index into a string to get the character "BL"[...]. Given it has to be run in an unsafe context, it should probably start with "unsafe char", and the compiler flag might need to be counted (I think it would be 1 byte, I can't remember the rules on that)... I should think you don't need any of the "0x000" stuff, "int a=1" ought to do fine. \$\endgroup\$ – VisualMelon Jul 31 '16 at 12:41
  • \$\begingroup\$ @VisualMelon you're right, unsafe char i(){int a=1;return"BL"[*(byte*)&a];} is ok? \$\endgroup\$ – Daniel Norris Aug 2 '16 at 16:55
  • \$\begingroup\$ Yeah, that looks good (well remembered removing the space after the return, easy to miss that one) \$\endgroup\$ – VisualMelon Aug 2 '16 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.