Host introspection: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added Rust)
m (→‎{{header|Wren}}: Minor tidy)
 
(25 intermediate revisions by 13 users not shown)
Line 1: Line 1:
{{task|Programming environment operations}}
{{task|Programming environment operations}}
{{omit from|6502 Assembly|without 16 bit data registers you can't prove this without already knowing the answer}}
{{omit from|Brlcad}}
{{omit from|Brlcad}}
{{omit from|E}}
{{omit from|E}}
Line 12: Line 13:


See also: [[Variable size/Get]]
See also: [[Variable size/Get]]
=={{header|68000 Assembly}}==
It's not possible to get the word size without knowing it in advance. But the 68000's big-endian nature can easily be proven even if the programmer didn't know that it was big-endian already.
Code is called as a subroutine, i.e. <code>JSR TestEndianness</code>. Hardware-specific print routines are unimplemented.
<syntaxhighlight lang="68000devpac">TestEndianness:
LEA UserRam,A0
MOVE.L #$0000FFFF,(A0)
MOVE.B (A0),D0 ;read the 0th byte stored
BEQ isBigEndian ;if this was little endian, the bytes would be stored FF FF 00 00
;must have been little-endian. Spoiler alert: execution will never reach here
LEA LittleEndianMessage,A3
JSR PrintString
rts
isBigEndian:
LEA BigEndianMessage,A3
JSR PrintString
rts

BigEndianMessage:
DC.B "BIG-ENDIAN",0
EVEN
LittleEndianMessage:
DC.B "LITTLE-ENDIAN",0
EVEN</syntaxhighlight>
=={{header|8086 Assembly}}==
As with [[68000 Assembly]], there's no way to "prove" the word size without knowing it in advance. But endianness can still be tested for quite easily.
<syntaxhighlight lang="asm"> .model small
.stack 1024

.data

UserRam BYTE 256 DUP (0)

.code
start:

mov ax,@data ;assembler calculates this offset for us
mov ds,ax ;the 8086 can only load segment registers from other registers, not directly from immediate values.

mov ax,@code
mov es,ax

mov ax,3422h
mov word ptr [ds:UserRam],ax
mov bl, byte ptr [ds:UserRam]
call doMonitor ;a routine that prints the contents of
;the 8086's registers to the screen

mov ax,4C00h
int 21h ;return to MS-DOS

end start</syntaxhighlight>

If the 8086 is little-endian, BX will equal 0022, since we loaded the low byte of UserRam into BL (the low half of BX). If it's big-endian, BX will equal 0034.

{{out}}
<pre>
Monitor tools created by Keith of Chibiakumas
AX:3422 BX:0022 CX:00FF DX:0192
F :------I--------- IP:0018
SP:03FA BP:091C DI:0400 SI:0388
CS:01A2 DS:01EC ES:01A2 SS:0425
</pre>

From this we conclude that the 8086 is indeed a little-endian CPU.

=={{header|Action!}}==
<syntaxhighlight lang="action!">PROC Main()
PrintE("All Atari 8-bit computers use little-endian word of 16-bits size.")
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Host_introspection.png Screenshot from Atari 8-bit computer]
<pre>
All Atari 8-bit computers use little-endian word of 16-bits size.
</pre>


=={{header|Ada}}==
=={{header|Ada}}==
<lang ada>with Ada.Text_IO; use Ada.Text_IO;
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
with System; use System;
with System; use System;


Line 21: Line 97:
Put_Line ("Word size" & Integer'Image (Word_Size));
Put_Line ("Word size" & Integer'Image (Word_Size));
Put_Line ("Endianness " & Bit_Order'Image (Default_Bit_Order));
Put_Line ("Endianness " & Bit_Order'Image (Default_Bit_Order));
end Host_Introspection;</lang>
end Host_Introspection;</syntaxhighlight>


{{out|Sample output on a Pentium machine}}
{{out|Sample output on a Pentium machine}}
Line 34: Line 110:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}}
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of FORMATted transput}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of FORMATted transput}}
<lang algol68>INT max abs bit = ABS(BIN 1 SHL 1)-1;
<syntaxhighlight lang="algol68">INT max abs bit = ABS(BIN 1 SHL 1)-1;
INT bits per char = ENTIER (ln(max abs char+1)/ln(max abs bit+1));
INT bits per char = ENTIER (ln(max abs char+1)/ln(max abs bit+1));
INT bits per int = ENTIER (1+ln(max int+1.0)/ln(max abs bit+1));
INT bits per int = ENTIER (1+ln(max int+1.0)/ln(max abs bit+1));
Line 57: Line 133:
int byte order +:= REPR(abcdi OVER (max abs bit+1) ** shift MOD (max abs char+1))
int byte order +:= REPR(abcdi OVER (max abs bit+1) ** shift MOD (max abs char+1))
OD;
OD;
printf(($"int byte order: "g,", Hex:",16r8dl$,int byte order, BIN abcdi))</lang>
printf(($"int byte order: "g,", Hex:",16r8dl$,int byte order, BIN abcdi))</syntaxhighlight>
{{out}} (Intel i686):
{{out}} (Intel i686):
<pre>
<pre>
Line 84: Line 160:


=={{header|Applesoft BASIC}}==
=={{header|Applesoft BASIC}}==
<lang ApplesoftBasic>1 DATA248,169,153,24,105,1,48
<syntaxhighlight lang="applesoftbasic">1 DATA248,169,153,24,105,1,48
2 DATA6,24,251,144,2,251,56
2 DATA6,24,251,144,2,251,56
3 DATA216,105,0,133,251,96
3 DATA216,105,0,133,251,96
Line 95: Line 171:
10 IF M THEN PRINT M$
10 IF M THEN PRINT M$
11 PRINT "ENDIANNESS: ";
11 PRINT "ENDIANNESS: ";
12 PRINT "LITTLE-ENDIAN"</lang>
12 PRINT "LITTLE-ENDIAN"</syntaxhighlight>

=={{header|ARM Assembly}}==
The word size of the ARM is 32-bit, which can't really be proven without knowing it ahead of time.

The ARM CPU's endianness can be set to either little-endian or big-endian. Not all ARM CPUs have this feature, but this test will work regardless of whether the endian switch features exist on any particular model or not. The easiest way to test endianness is to write a word to RAM, then read the 0th byte from that memory location and see what it is. (The example below uses VASM syntax.)


<syntaxhighlight lang="arm assembly">EndianTest:
mov r0,#0xFF
mov r1,#0x02000000 ;an arbitrary memory location on the Game Boy Advance.
;(The GBA is always little-endian but this test doesn't use that knowledge to prove it.)
str r0,[r1] ;on a little-endian CPU a hexdump of 0x02000000 would be: FF 00 00 00
;on a big-endian CPU it would be: 00 00 00 FF
ldrB r0,[r1] ;load just the byte at 0x02000000. If the machine is big-endian this will load 00; if little-endian, 0xFF.
cmp r0,#0
beq isBigEndian
;else, do whatever is needed to display "little-endian" to the screen. This part isn't implemented.</syntaxhighlight>
=={{header|Babel}}==
=={{header|Babel}}==
<lang babel>main :
<syntaxhighlight lang="babel">main :
{ "Word size: " << msize 3 shl %d << " bits" cr <<
{ "Word size: " << msize 3 shl %d << " bits" cr <<
"Endianness: " << { endian } { "little" } { "big" } ifte cr << }</lang>
"Endianness: " << { endian } { "little" } { "big" } ifte cr << }</syntaxhighlight>


=={{header|BBC BASIC}}==
=={{header|BBC BASIC}}==
<lang bbcbasic> DIM P% 8
<syntaxhighlight lang="bbcbasic"> DIM P% 8
!P% = -1
!P% = -1
I% = 0 : REPEAT I% += 1 : UNTIL P%?I%=0
I% = 0 : REPEAT I% += 1 : UNTIL P%?I%=0
Line 109: Line 200:
!P% = 1
!P% = 1
IF P%?0 = 1 THEN PRINT "Little-endian"
IF P%?0 = 1 THEN PRINT "Little-endian"
IF P%?(I%-1) = 1 THEN PRINT "Big-endian"</lang>
IF P%?(I%-1) = 1 THEN PRINT "Big-endian"</syntaxhighlight>
The 'word size' is reported as the number of bytes accessed by the ! indirection operator, which is 4 in all current versions of BBC BASIC.
The 'word size' is reported as the number of bytes accessed by the ! indirection operator, which is 4 in all current versions of BBC BASIC.


=={{header|C}}==
=={{header|C}}==
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <stddef.h> /* for size_t */
#include <stddef.h> /* for size_t */
#include <limits.h> /* for CHAR_BIT */
#include <limits.h> /* for CHAR_BIT */
Line 134: Line 225:
printf("big endian\n");
printf("big endian\n");
return 0;
return 0;
}</lang>
}</syntaxhighlight>


On POSIX-compatible systems, the following also tests the endianness (this makes use of the fact that network order is big endian):
On POSIX-compatible systems, the following also tests the endianness (this makes use of the fact that network order is big endian):
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <arpa/inet.h>
#include <arpa/inet.h>


Line 146: Line 237:
else
else
printf("little endian\n");
printf("little endian\n");
}</lang>
}</syntaxhighlight>


=={{header|C sharp}}==
=={{header|C sharp}}==
<lang csharp>static void Main()
<syntaxhighlight lang="csharp">static void Main()
{
{
Console.WriteLine("Word size = {0} bytes,",sizeof(int));
Console.WriteLine("Word size = {0} bytes,",sizeof(int));
Line 157: Line 248:
else
else
Console.WriteLine("Big-endian.");
Console.WriteLine("Big-endian.");
}</lang>
}</syntaxhighlight>

=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <bit>
#include <iostream>

int main()
{
std::cout << "int is " << sizeof(int) << " bytes\n";
std::cout << "a pointer is " << sizeof(int*) << " bytes\n\n";

if (std::endian::native == std::endian::big)
{
std::cout << "platform is big-endian\n";
}
else
{
std::cout << "host is little-endian\n";
}
}</syntaxhighlight>
{{out}}
<pre>
int is 4 bytes
a pointer is 8 bytes

host is little-endian
</pre>


=={{header|Caché ObjectScript}}==
=={{header|Caché ObjectScript}}==
Line 168: Line 285:


=={{header|Clojure}}==
=={{header|Clojure}}==
<lang clojure>(println "word size: " (System/getProperty "sun.arch.data.model"))
<syntaxhighlight lang="clojure">(println "word size: " (System/getProperty "sun.arch.data.model"))
(println "endianness: " (System/getProperty "sun.cpu.endian"))</lang>
(println "endianness: " (System/getProperty "sun.cpu.endian"))</syntaxhighlight>


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
Line 176: Line 293:


The [http://www.lispworks.com/documentation/HyperSpec/Body/c_enviro.htm Environment] has some implementation-specific functions that might provide a good hint, e.g.,
The [http://www.lispworks.com/documentation/HyperSpec/Body/c_enviro.htm Environment] has some implementation-specific functions that might provide a good hint, e.g.,
<lang lisp>(machine-type) ;; => "X86-64" on SBCL here</lang>
<syntaxhighlight lang="lisp">(machine-type) ;; => "X86-64" on SBCL here</syntaxhighlight>


The [http://www.cliki.net/features *features*] list also provides useful information, e.g., some compilers declare :LITTLE-ENDIAN there.
The [http://www.cliki.net/features *features*] list also provides useful information, e.g., some compilers declare :LITTLE-ENDIAN there.
Line 183: Line 300:


=={{header|D}}==
=={{header|D}}==
<lang d>void main() {
<syntaxhighlight lang="d">void main() {
import std.stdio, std.system;
import std.stdio, std.system;


writeln("Word size = ", size_t.sizeof * 8, " bits.");
writeln("Word size = ", size_t.sizeof * 8, " bits.");
writeln(endian == Endian.littleEndian ? "Little" : "Big", " endian.");
writeln(endian == Endian.littleEndian ? "Little" : "Big", " endian.");
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Word size = 64 bits.
<pre>Word size = 64 bits.
Line 194: Line 311:


=={{header|Delphi}}==
=={{header|Delphi}}==
<lang Delphi>program HostIntrospection ;
<syntaxhighlight lang="delphi">program HostIntrospection ;


{$APPTYPE CONSOLE}
{$APPTYPE CONSOLE}
Line 203: Line 320:
Writeln('word size: ', SizeOf(Integer));
Writeln('word size: ', SizeOf(Integer));
Writeln('endianness: little endian'); // Windows is always little endian
Writeln('endianness: little endian'); // Windows is always little endian
end.</lang>
end.</syntaxhighlight>


=={{header|Erlang}}==
=={{header|Erlang}}==
To find the word size:
To find the word size:
<lang erlang>1> erlang:system_info(wordsize).
<syntaxhighlight lang="erlang">1> erlang:system_info(wordsize).
4</lang>
4</syntaxhighlight>


In the case of endianness, Erlang's bit syntax by default has a 'native' option which lets you use what is supported natively.
In the case of endianness, Erlang's bit syntax by default has a 'native' option which lets you use what is supported natively.
Line 214: Line 331:
However, one could write one by using bit syntax, setting endianness and then comparing to the native format:
However, one could write one by using bit syntax, setting endianness and then comparing to the native format:


<lang erlang>1> <<1:4/native-unit:8>>.
<syntaxhighlight lang="erlang">1> <<1:4/native-unit:8>>.
<<1,0,0,0>>
<<1,0,0,0>>
2> <<1:4/big-unit:8>>
2> <<1:4/big-unit:8>>
<<0,0,0,1>>
<<0,0,0,1>>
3> <<1:4/little-unit:8>>.
3> <<1:4/little-unit:8>>.
<<1,0,0,0>></lang>
<<1,0,0,0>></syntaxhighlight>


And so the following function would output endianness:
And so the following function would output endianness:


<lang erlang>endianness() when <<1:4/native-unit:8>> =:= <<1:4/big-unit:8>> -> big;
<syntaxhighlight lang="erlang">endianness() when <<1:4/native-unit:8>> =:= <<1:4/big-unit:8>> -> big;
endianness() -> little.</lang>
endianness() -> little.</syntaxhighlight>

=={{header|F_Sharp|F#}}==
A lot of research before I finally came up with an answer to this that isn't dependent on the machine it was compiled on. Works on Win32 machines only (obviously, due to the interop). I think that strictly speaking, I should be double checking the OS version before making the call to wow64Process, but I'm not worrying about it.
<syntaxhighlight lang="fsharp">open System
open System.Runtime.InteropServices
open System.Diagnostics

[<DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)>]
extern bool IsWow64Process(nativeint hProcess, bool &wow64Process);

let answerHostInfo =
let Is64Bit() =
let mutable f64Bit = false;
IsWow64Process(Process.GetCurrentProcess().Handle, &f64Bit) |> ignore
f64Bit
let IsLittleEndian() = BitConverter.IsLittleEndian
(IsLittleEndian(), Is64Bit())</syntaxhighlight>


=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>USING: alien.c-types alien.data io layouts ;
<syntaxhighlight lang="factor">USING: alien.c-types alien.data io layouts ;
"Word size: " write cell 8 * .
"Word size: " write cell 8 * .
"Endianness: " write little-endian? "little" "big" ? print</lang>
"Endianness: " write little-endian? "little" "big" ? print</syntaxhighlight>


=={{header|Forth}}==
=={{header|Forth}}==
<lang forth>: endian
<syntaxhighlight lang="forth">: endian
cr 1 cells . ." address units per cell"
cr 1 cells . ." address units per cell"
s" ADDRESS-UNIT-BITS" environment? if cr . ." bits per address unit" then
s" ADDRESS-UNIT-BITS" environment? if cr . ." bits per address unit" then
cr 1 here ! here c@ if ." little" else ." big" then ." endian" ;</lang>
cr 1 here ! here c@ if ." little" else ." big" then ." endian" ;</syntaxhighlight>
This relies on '''c@''' being a byte fetch (4 chars = 1 cells). Although it is on most architectures, ANS Forth only guarantees that 1 chars <= 1 cells. Some Forths like OpenFirmware have explicitly sized fetches, like b@.
This relies on '''c@''' being a byte fetch (4 chars = 1 cells). Although it is on most architectures, ANS Forth only guarantees that 1 chars <= 1 cells. Some Forths like OpenFirmware have explicitly sized fetches, like b@.


=={{header|Fortran}}==
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
{{works with|Fortran|90 and < 2018}}
<lang fortran> integer :: i
<syntaxhighlight lang="fortran"> integer :: i
character(len=1) :: c(20)
character(len=1) :: c(20)
equivalence (c, i)
equivalence (c, i)
Line 252: Line 386:
ELSE
ELSE
WRITE(*,*) "Little Endian"
WRITE(*,*) "Little Endian"
END IF</lang>
END IF</syntaxhighlight>
{{works with|Fortran| 77 and later}}
<syntaxhighlight lang="fortran">
PROGRAM endianness
IMPLICIT NONE
INTEGER(KIND=4) :: i = 1

!ISHFT(INTEGER, SHIFT) : Left shift if SHIFT > 0
!ISHFT(INTEGER, SHIFT) : Right shift if SHIFT < 0
IF (ISHFT(i,1) .EQ. 0) THEN
WRITE(*,FMT='(A)') 'Architechture is Big Endian'
ELSE
WRITE(*,FMT='(A)') 'Architecture is Little Endian'
END IF

RETURN

STOP
END PROGRAM endianness
</syntaxhighlight>


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>' FB 1.05.0 Win64 (so little endian, 8 byte word size, expected)
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64 (so little endian, 8 byte word size, expected)


' uses intrinsic defines, set by the compiler
' uses intrinsic defines, set by the compiler
Line 271: Line 424:
#EndIf
#EndIf


Sleep</lang>
Sleep</syntaxhighlight>


{{out}}
{{out}}
Line 280: Line 433:


=={{header|Frink}}==
=={{header|Frink}}==
<lang frink>
<syntaxhighlight lang="frink">
println["Word size: " + callJava["java.lang.System", "getProperty", "sun.arch.data.model"]]
println["Word size: " + callJava["java.lang.System", "getProperty", "sun.arch.data.model"]]
println["Endianness: " + callJava["java.lang.System", "getProperty", "sun.cpu.endian"]]
println["Endianness: " + callJava["java.lang.System", "getProperty", "sun.cpu.endian"]]
</syntaxhighlight>
</lang>


=={{header|F_Sharp|F#}}==
A lot of research before I finally came up with an answer to this that isn't dependent on the machine it was compiled on. Works on Win32 machines only (obviously, due to the interop). I think that strictly speaking, I should be double checking the OS version before making the call to wow64Process, but I'm not worrying about it.
<lang fsharp>open System
open System.Runtime.InteropServices
open System.Diagnostics

[<DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)>]
extern bool IsWow64Process(nativeint hProcess, bool &wow64Process);

let answerHostInfo =
let Is64Bit() =
let mutable f64Bit = false;
IsWow64Process(Process.GetCurrentProcess().Handle, &f64Bit) |> ignore
f64Bit
let IsLittleEndian() = BitConverter.IsLittleEndian
(IsLittleEndian(), Is64Bit())</lang>
=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 372: Line 509:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 405: Line 542:
</pre>
</pre>
Alternative technique:
Alternative technique:
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 421: Line 558:
fmt.Println(f.FileHeader.ByteOrder)
fmt.Println(f.FileHeader.ByteOrder)
f.Close()
f.Close()
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 429: Line 566:
=={{header|Groovy}}==
=={{header|Groovy}}==
Solution follows [[Java]]:
Solution follows [[Java]]:
<lang groovy>println "word size: ${System.getProperty('sun.arch.data.model')}"
<syntaxhighlight lang="groovy">println "word size: ${System.getProperty('sun.arch.data.model')}"
println "endianness: ${System.getProperty('sun.cpu.endian')}"</lang>
println "endianness: ${System.getProperty('sun.cpu.endian')}"</syntaxhighlight>


{{out}}
{{out}}
Line 437: Line 574:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Data.Bits
<syntaxhighlight lang="haskell">import Data.Bits
import ADNS.Endian -- http://hackage.haskell.org/package/hsdns
import ADNS.Endian -- http://hackage.haskell.org/package/hsdns


Line 444: Line 581:
putStrLn $ "Endianness: " ++ show endian
putStrLn $ "Endianness: " ++ show endian
where
where
bitsize = show $ bitSize (undefined :: Int)</lang>
bitsize = show $ bitSize (undefined :: Int)</syntaxhighlight>


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==


<lang unicon>procedure main()
<syntaxhighlight lang="unicon">procedure main()
write(if 0 = ishift(1,-1) then "little" else "big"," endian")
write(if 0 = ishift(1,-1) then "little" else "big"," endian")
if match("flags",line := !open("/proc/cpuinfo")) then # Unix-like only
if match("flags",line := !open("/proc/cpuinfo")) then # Unix-like only
write(if find(" lm ",line) then 64 else 32," bits per word")
write(if find(" lm ",line) then 64 else 32," bits per word")
else write("Cannot determine word size.")
else write("Cannot determine word size.")
end</lang>
end</syntaxhighlight>


Sample run:
Sample run:
Line 466: Line 603:
=={{header|J}}==
=={{header|J}}==


<lang j> IF64 {32 64
<syntaxhighlight lang="j"> IF64 {32 64
64</lang>
64</syntaxhighlight>


This returns <code>32</code> in 32 bit J.
This returns <code>32</code> in 32 bit J.

This value could also be calculated, exercising left shift of bits in a 2s complement fixed width integer:<syntaxhighlight lang="j"> 2+2^.>./1&(33 b.)^:a:1
64</syntaxhighlight>


Note that this mechanism is testing the interpreter, and not the OS or Hardware. (Though, of course, you cannot run a 64 bit interpreter on a machine that does not support it.)
Note that this mechanism is testing the interpreter, and not the OS or Hardware. (Though, of course, you cannot run a 64 bit interpreter on a machine that does not support it.)
Line 475: Line 615:
That said, this does not deal with endianness. For the most part, J programs do not need to know their own endianness. When converting to and from binary format you can specify "native", "little endian" and "big endian", and it's rare that you have an interface which would need anything else. That said, you can inspect the binary representation of a simple constant:
That said, this does not deal with endianness. For the most part, J programs do not need to know their own endianness. When converting to and from binary format you can specify "native", "little endian" and "big endian", and it's rare that you have an interface which would need anything else. That said, you can inspect the binary representation of a simple constant:


<lang j> ":&> (|: 32 64 ;"0 big`little) {"_1~ 2 2 #: 16b_e0 + a. i. 0 { 3!:1 ''
<syntaxhighlight lang="j"> ":&> (|: 32 64 ;"0 big`little) {"_1~ 2 2 #: 16b_e0 + a. i. 0 { 3!:1 ''
64
64
little</lang>
little</syntaxhighlight>


=={{header|Java}}==
=={{header|Java}}==
Line 484: Line 624:
{{works with|Java|1.4}}
{{works with|Java|1.4}}


<lang java>import java.nio.ByteOrder;
<syntaxhighlight lang="java">import java.nio.ByteOrder;


public class ShowByteOrder {
public class ShowByteOrder {
Line 491: Line 631:
System.out.println(ByteOrder.nativeOrder());
System.out.println(ByteOrder.nativeOrder());
}
}
}</lang>
}</syntaxhighlight>


Some JVMs also have system properties for the word size and byte order.
Some JVMs also have system properties for the word size and byte order.


<lang java>System.out.println("word size: "+System.getProperty("sun.arch.data.model"));
<syntaxhighlight lang="java">System.out.println("word size: "+System.getProperty("sun.arch.data.model"));
System.out.println("endianness: "+System.getProperty("sun.cpu.endian"));</lang>
System.out.println("endianness: "+System.getProperty("sun.cpu.endian"));</syntaxhighlight>


=={{header|Julia}}==
=={{header|Julia}}==
<code>Julia</code> creates <code>ENDIAN_BOM</code> a 32 bit unsigned integer out of an array of 4 8 bit unsigned integers to serve as an endianness marker.
<code>Julia</code> creates <code>ENDIAN_BOM</code> a 32 bit unsigned integer out of an array of 4 8 bit unsigned integers to serve as an endianness marker.
<syntaxhighlight lang="julia">
<lang Julia>
print("This host's word size is ", WORD_SIZE, ".")
print("This host's word size is ", WORD_SIZE, ".")
if ENDIAN_BOM == 0x04030201
if ENDIAN_BOM == 0x04030201
Line 509: Line 649:
println("ENDIAN_BOM = ", ENDIAN_BOM, ", which is confusing")
println("ENDIAN_BOM = ", ENDIAN_BOM, ", which is confusing")
end
end
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 518: Line 658:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
The following is not guaranteed to work on all JVMs but is working fine on my x64 Windows 10 machine:
The following is not guaranteed to work on all JVMs but is working fine on my x64 Windows 10 machine:
<lang scala>// version 1.0.6
<syntaxhighlight lang="scala">// version 1.0.6


fun main(args: Array<String>) {
fun main(args: Array<String>) {
println("Word size : ${System.getProperty("sun.arch.data.model")} bits")
println("Word size : ${System.getProperty("sun.arch.data.model")} bits")
println("Endianness: ${System.getProperty("sun.cpu.endian")}-endian")
println("Endianness: ${System.getProperty("sun.cpu.endian")}-endian")
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 530: Line 670:
Endianness: little-endian
Endianness: little-endian
</pre>
</pre>

=={{header|Lua}}==
Pure/native Lua can't do this (and essentially doesn't care). However, Lua is often used in a scripting environment, where such issues may be important, and any needed support would be expected to be provided by the host or some other external library. Here using ffi:
<syntaxhighlight lang="lua">ffi = require("ffi")
print("size of int (in bytes): " .. ffi.sizeof(ffi.new("int")))
print("size of pointer (in bytes): " .. ffi.sizeof(ffi.new("int*")))
print((ffi.abi("le") and "little" or "big") .. " endian")</syntaxhighlight>
{{out}}
<pre>size of int (in bytes): 4
size of pointer (in bytes): 8
little endian</pre>

=={{header|M2000 Interpreter}}==
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module CheckIt {
Module CheckIt {
\\ Always run in Little-endian, 32 bits (in Wow64 in 64 bit os)
\\ Always run in Little-endian, 32 bits (in Wow64 in 64 bit os)
Line 561: Line 713:
}
}
Checkit
Checkit
</syntaxhighlight>
</lang>

=={{header|MACRO-10}}==
<syntaxhighlight lang="macro-10">
title Host Introspection
subttl PDP-10 assembly (MACRO-10 on TOPS-20). KJX 2022.
search monsym,macsym

comment \
The wordsize is detected by putting 1 into a re-
gister, counting the leading zeros (resulting in
wordsize-1) and adding 1 to the result.

Endianness doesn't really apply, as the PDP-10 is
a 36bit word-adressable computer, and the handling
of characters is peculiar enough that it would get
out of hand if I'd dive into the details here.
\

a=:1 ;Define three accumulators.
b=:2
c=:3

start:: reset% ;Initialize process.

movei a,1 ;Set A to 1.
jffo a,.+1 ;B = leading zeros of A.
aos b ;Add 1 to B. -> wordsize.

movei a,.priou ;Print B on standard output
movei c,^d10 ;in base 10.
nout%
jfcl

haltf% ;Halt program.
jrst start ;Allow continue-command.

end start
</syntaxhighlight>

=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<lang Mathematica>If[$ByteOrdering > 0, Print["Big endian"], Print["Little endian" ]]
<syntaxhighlight lang="mathematica">If[$ByteOrdering > 0, Print["Big endian"], Print["Little endian" ]]
$SystemWordLength "bits"</lang>
$SystemWordLength "bits"</syntaxhighlight>


{{out}} x86
{{out}} x86
Line 576: Line 767:
The concept of "word size" is not meaningful in Matlab and Octave, uint64 is also available on 32bit-platforms, and there are no pointers. Endianity can be tested with the function below:
The concept of "word size" is not meaningful in Matlab and Octave, uint64 is also available on 32bit-platforms, and there are no pointers. Endianity can be tested with the function below:


<lang MATLAB> function [endian]=endian()
<syntaxhighlight lang="matlab"> function [endian]=endian()
fid=tmpfile();
fid=tmpfile();
fwrite(fid,1:8,'uint8');
fwrite(fid,1:8,'uint8');
Line 593: Line 784:
else endian='little';
else endian='little';
end;
end;
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 601: Line 792:
endian = little</pre>
endian = little</pre>


=={{header|MIPS Assembly}}==
This uses Keith S.'s tutorial at [https://www.chibialiens.com/mips/ Chibialiens.com] to print memory and show register contents.
As I've come to find out, MIPS is a bi-endian architecture (meaning its endianness is implementation-defined rather than a constant trait of the CPU.) In particular, the PlayStation 1 is little-endian, and the Nintendo 64 is big-endian. This can be proven with the test below. (Hardware-specific routines <code>MonitorA0A1RAPC</code> and <code>MemDump</code> are omitted just to keep things brief.)

<syntaxhighlight lang="mips"> jal Cls ;Zero Graphics cursor position
nop ;on the PlayStation, the instruction AFTER a branch gets executed BEFORE the branch actually occurs.
;The Nintendo 64 didn't have this "feature" but for compatibility's sake
; it's staying in regardless of which version of the code I'm using.

la a2,TestData ;Load address of TestData
lw a0,(a2) ;Load Word into A0 from address in A2
addiu a2,4 ;pointer arithmetic to load the next word.
lw a1,(a2)
move t6,ra
jal MonitorA0A1RAPC
nop
li t6,2 ;Line Count - 2 lines = 16 bytes
jal MemDump ;Dump Ram to screen
nop
halt:
j halt ;loop forever
nop




TestData:
.byte 0xF3,0xF2,0xF1,0xF0 ;this will load as F0F1F2F3 on little-endian machines, and as-is on big-endian
.word 0xF0F1F2F3 ;this will load as F0F1F2F3 regardless of endianness.
</syntaxhighlight>
{{out}}
Register Dump of PlayStation 1:
<pre>a0:F0F1F2F3 a1:F0F1F2F3</pre>

Register Dump of Nintendo 64:
<pre>a0:F3F2F1F0 a1:F0F1F2F3</pre>

It also seems the registers are 32-bit even on the N64. I wouldn't have expected that to be honest...


=={{header|Modula-3}}==
=={{header|Modula-3}}==
<lang modula3>MODULE Host EXPORTS Main;
<syntaxhighlight lang="modula3">MODULE Host EXPORTS Main;


IMPORT IO, Fmt, Word, Swap;
IMPORT IO, Fmt, Word, Swap;
Line 614: Line 848:
IO.Put("Endianness: Little\n");
IO.Put("Endianness: Little\n");
END;
END;
END Host.</lang>
END Host.</syntaxhighlight>


{{out}} (on an x86):
{{out}} (on an x86):
Line 627: Line 861:
C support file, host-introspection.c
C support file, host-introspection.c


<lang C>/* Return wordsize to Neko */
<syntaxhighlight lang="c">/* Return wordsize to Neko */
/* From Rosetta Code, C entry, with Neko marshalling */
/* From Rosetta Code, C entry, with Neko marshalling */


Line 642: Line 876:
}
}
/* Expose symbol to Neko loader */
/* Expose symbol to Neko loader */
DEFINE_PRIM(wordsize, 0);</lang>
DEFINE_PRIM(wordsize, 0);</syntaxhighlight>


Neko caller, host-introspection.neko
Neko caller, host-introspection.neko


<syntaxhighlight lang="actionscript">/**
<lang ActionScript>/**
Host introspection, in Neko
Host introspection, in Neko
*/
*/
Line 661: Line 895:


var wordsize = $loader.loadprim("native@wordsize", 0)
var wordsize = $loader.loadprim("native@wordsize", 0)
$print("wordsize: ", wordsize(), " bits\n")</lang>
$print("wordsize: ", wordsize(), " bits\n")</syntaxhighlight>


{{out}}
{{out}}
Line 674: Line 908:
{{trans|Java}}
{{trans|Java}}
NetRexx can access this information from the [[Java]] virtual machine in the same way as the [[#Java|Java]] sample above.
NetRexx can access this information from the [[Java]] virtual machine in the same way as the [[#Java|Java]] sample above.
<lang NetRexx>/* NetRexx */
<syntaxhighlight lang="netrexx">/* NetRexx */
options replace format comments java crossref savelog symbols nobinary
options replace format comments java crossref savelog symbols nobinary


Line 682: Line 916:
say ' word size:' wordSize
say ' word size:' wordSize
say 'endianness:' endian
say 'endianness:' endian
</syntaxhighlight>
</lang>


=={{header|Nim}}==
=={{header|Nim}}==
In Nim, "int" type has the size of the word. So, to find the word size in bits, just multiply the "int" size in bytes by eight.
<lang nim>import math
echo cpuEndian
<syntaxhighlight lang="nim">echo cpuEndian
echo round(log2(float(int.high))) + 1</lang>
echo sizeof(int) * 8</syntaxhighlight>


=={{header|Objective-C}}==
=={{header|Objective-C}}==
Endianness:
Endianness:
<lang objc>switch (NSHostByteOrder()) {
<syntaxhighlight lang="objc">switch (NSHostByteOrder()) {
case NS_BigEndian:
case NS_BigEndian:
NSLog(@"%@", @"Big Endian");
NSLog(@"%@", @"Big Endian");
Line 701: Line 935:
NSLog(@"%@", @"endianness unknown");
NSLog(@"%@", @"endianness unknown");
break;
break;
} </lang>
} </syntaxhighlight>


Architecture:
Architecture:
(works on Mac OS X 10.6+)
(works on Mac OS X 10.6+)
<lang objc>switch ([NSRunningApplication currentApplication].executableArchitecture) {
<syntaxhighlight lang="objc">switch ([NSRunningApplication currentApplication].executableArchitecture) {
case NSBundleExecutableArchitectureI386:
case NSBundleExecutableArchitectureI386:
NSLog(@"%@", @"i386 32-bit");
NSLog(@"%@", @"i386 32-bit");
Line 725: Line 959:
NSLog(@"%@", @"Unknown");
NSLog(@"%@", @"Unknown");
break;
break;
}</lang>
}</syntaxhighlight>


=={{header|OCaml}}==
=={{header|OCaml}}==


<lang ocaml>Printf.printf "%d\n" Sys.word_size; (* Print word size *)
<syntaxhighlight lang="ocaml">Printf.printf "%d\n" Sys.word_size; (* Print word size *)
Printf.printf "%s\n" Sys.os_type; (* Print operating system *)</lang>
Printf.printf "%s\n" Sys.os_type; (* Print operating system *)</syntaxhighlight>


{{works with|OCaml|4.00+}}
{{works with|OCaml|4.00+}}
<lang ocaml>(* Print endianness *)
<syntaxhighlight lang="ocaml">(* Print endianness *)
Printf.printf "%s\n" (if Sys.big_endian then "big endian" else "little endian");</lang>
Printf.printf "%s\n" (if Sys.big_endian then "big endian" else "little endian");</syntaxhighlight>


On OCaml 3 and below, there are tricks to get endianness. For example in Linux or Unix variants,
On OCaml 3 and below, there are tricks to get endianness. For example in Linux or Unix variants,
one may use the [http://unixhelp.ed.ac.uk/CGI/man-cgi?uname uname] shell command :
one may use the [http://unixhelp.ed.ac.uk/CGI/man-cgi?uname uname] shell command :


<lang ocaml>let uname arg =
<syntaxhighlight lang="ocaml">let uname arg =
let arg = if arg = "" then "-" else arg in
let arg = if arg = "" then "-" else arg in
let ic = Unix.open_process_in ("uname -" ^ arg) in
let ic = Unix.open_process_in ("uname -" ^ arg) in
Line 746: Line 980:


# uname "sm";;
# uname "sm";;
- : string = "Linux i686"</lang>
- : string = "Linux i686"</syntaxhighlight>


In most cases, endianness can be infered from informations given by uname.
In most cases, endianness can be infered from informations given by uname.
Line 752: Line 986:
One may also read files in the /proc directory in order to get informations about the host, only under linux :
One may also read files in the /proc directory in order to get informations about the host, only under linux :


<lang ocaml>(* Reading all the lines from a file.
<syntaxhighlight lang="ocaml">(* Reading all the lines from a file.
If the loop is implemented by a recursive auxiliary function, the try...with breaks
If the loop is implemented by a recursive auxiliary function, the try...with breaks
tail recursion if not written carefully *)
tail recursion if not written carefully *)
Line 785: Line 1,019:
"VmallocChunk: 109320 kB"; "HugePages_Total: 0";
"VmallocChunk: 109320 kB"; "HugePages_Total: 0";
"HugePages_Free: 0"; "HugePages_Rsvd: 0";
"HugePages_Free: 0"; "HugePages_Rsvd: 0";
"HugePages_Surp: 0"; "Hugepagesize: 4096 kB"]</lang>
"HugePages_Surp: 0"; "Hugepagesize: 4096 kB"]</syntaxhighlight>


Same methods can be used to get the results of commands lshw, dmidecode...
Same methods can be used to get the results of commands lshw, dmidecode...


=={{header|Pascal}}==
=={{header|Pascal}}==
<lang pascal>program HostIntrospection(output);
<syntaxhighlight lang="pascal">program HostIntrospection(output);
begin
begin
writeln('Pointer size: ', SizeOf(Pointer), ' byte, i.e. ', SizeOf(Pointer)*8, ' bit.');
writeln('Pointer size: ', SizeOf(Pointer), ' byte, i.e. ', SizeOf(Pointer)*8, ' bit.');
Line 798: Line 1,032:
else
else
writeln('This host is little endian.');
writeln('This host is little endian.');
end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 808: Line 1,042:
=={{header|Perl}}==
=={{header|Perl}}==
Most basic example:
Most basic example:
<lang perl>use Config;
<syntaxhighlight lang="perl">use Config;
print "UV size: $Config{uvsize}, byte order: $Config{byteorder}\n";</lang>
print "UV size: $Config{uvsize}, byte order: $Config{byteorder}\n";</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 816: Line 1,050:


More verbose example:
More verbose example:
<lang perl>use 5.010;
<syntaxhighlight lang="perl">use 5.010;
use Config;
use Config;
my ($size, $order, $end) = @Config{qw(uvsize byteorder)};
my ($size, $order, $end) = @Config{qw(uvsize byteorder)};
Line 824: Line 1,058:
default { $end = 'mixed' }
default { $end = 'mixed' }
}
}
say "UV size: $size, byte order: $order ($end-endian)";</lang>
say "UV size: $size, byte order: $order ($end-endian)";</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 835: Line 1,069:
UV size: 8, byte order: 87654321 (big-endian)
UV size: 8, byte order: 87654321 (big-endian)
</pre>
</pre>
=={{header|Perl 6}}==
Endian detection translated from C. {{works with|Rakudo|2018.03}}
<lang perl6>use NativeCall;
say $*VM.config<ptr_size>;
my $bytes = nativecast(CArray[uint8], CArray[uint16].new(1));
say $bytes[0] ?? "little-endian" !! "big-endian";</lang>
{{out}}
<pre>8
little-endian</pre>
Note: Rakudo 2018.12 is introducing the endian-sensitive<code>read-int16</code> method,
which makes endian detection a little easier:
<lang perl6>say blob8.new(1,0).read-int16(0) == 1 ?? "little-endian" !! "big-endian"</lang>


=={{header|Phix}}==
=={{header|Phix}}==
Note that machine_word() and machine_bits() test the interpreter or compiled executable, rather than the OS or hardware.<br>
Note that machine_word() and machine_bits() test the interpreter or compiled executable, rather than the OS or hardware.<br>
Also, all known implementations of Phix are currently little-endian. See also platform(), which yields WINDOWS or LINUX.
Also, all known implementations of Phix are currently little-endian. See also platform(), which yields WINDOWS/LINUX/JS.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>function endianness()
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
atom m4 = allocate(4)
<span style="color: #008080;">function</span> <span style="color: #000000;">endianness</span><span style="color: #0000FF;">()</span>
poke4(m4,#01020304)
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
integer b1 = peek1s(m4)
<span style="color: #008080;">return</span> <span style="color: #008000;">"n/a (web browser)"</span>
free(m4)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if b1=#01 then
<span style="color: #004080;">atom</span> <span style="color: #000000;">m4</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">allocate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)</span>
return "big-endian"
<span style="color: #7060A8;">poke4</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">#01020304</span><span style="color: #0000FF;">)</span>
elsif b1=#04 then
<span style="color: #004080;">integer</span> <span style="color: #000000;">b1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">peek1s</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m4</span><span style="color: #0000FF;">)</span>
return "little-endian"
<span style="color: #7060A8;">free</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m4</span><span style="color: #0000FF;">)</span>
else
<span style="color: #008080;">if</span> <span style="color: #000000;">b1</span><span style="color: #0000FF;">=</span><span style="color: #000000;">#01</span> <span style="color: #008080;">then</span>
return "???"
<span style="color: #008080;">return</span> <span style="color: #008000;">"big-endian"</span>
end if
<span style="color: #008080;">elsif</span> <span style="color: #000000;">b1</span><span style="color: #0000FF;">=</span><span style="color: #000000;">#04</span> <span style="color: #008080;">then</span>
end function
<span style="color: #008080;">return</span> <span style="color: #008000;">"little-endian"</span>

<span style="color: #008080;">else</span>
printf(1,"Endianness: %s\n",{endianness()})
<span style="color: #008080;">return</span> <span style="color: #008000;">"???"</span>
printf(1,"Word size: %d bytes/%d bits\n",{machine_word(),machine_bits()})</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Endianness: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">endianness</span><span style="color: #0000FF;">()})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Word size: %d bytes/%d bits\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">machine_word</span><span style="color: #0000FF;">(),</span><span style="color: #7060A8;">machine_bits</span><span style="color: #0000FF;">()})</span>
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 876: Line 1,104:
Endianness: little-endian
Endianness: little-endian
Word size: 8 bytes/64 bits
Word size: 8 bytes/64 bits
</pre>
or
<pre>
Endianness: n/a (web browser)
Word size: 4 bytes/32 bits
</pre>
</pre>


Line 883: Line 1,116:
other contributions to this task) only tells how the binary was
other contributions to this task) only tells how the binary was
compiled/assembled/linked, not necessarily the nature of the underlying system.
compiled/assembled/linked, not necessarily the nature of the underlying system.
<lang PicoLisp>(in (cmd) # Inspect ELF header
<syntaxhighlight lang="picolisp">(in (cmd) # Inspect ELF header
(rd 4) # Skip "7F" and 'E', 'L' and 'F'
(rd 4) # Skip "7F" and 'E', 'L' and 'F'
(prinl
(prinl
Line 894: Line 1,127:
(1 "Little endian")
(1 "Little endian")
(2 "Big endian")
(2 "Big endian")
(T "Bad EI_DATA") ) ) )</lang>
(T "Bad EI_DATA") ) ) )</syntaxhighlight>
{{out}}
{{out}}
<pre>64 bits
<pre>64 bits
Line 900: Line 1,133:


=={{header|PL/I}}==
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
details: procedure options (main); /* 6 July 2012 */
details: procedure options (main); /* 6 July 2012 */
declare x float, i fixed binary initial (1);
declare x float, i fixed binary initial (1);
Line 912: Line 1,145:
end details;
end details;
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 920: Line 1,153:


=={{header|PowerShell}}==
=={{header|PowerShell}}==
<lang powershell>Write-Host Word Size: ((Get-WMIObject Win32_Processor).DataWidth)
<syntaxhighlight lang="powershell">Write-Host Word Size: ((Get-WMIObject Win32_Processor).DataWidth)
Write-Host -NoNewLine "Endianness: "
Write-Host -NoNewLine "Endianness: "
if ([BitConverter]::IsLittleEndian) {
if ([BitConverter]::IsLittleEndian) {
Line 926: Line 1,159:
} else {
} else {
Write-Host Big-Endian
Write-Host Big-Endian
}</lang>
}</syntaxhighlight>
Note that endianness is essentially a moot point with PowerShell,
Note that endianness is essentially a moot point with PowerShell,
as there is only a Windows implementation currently
as there is only a Windows implementation currently
Line 933: Line 1,166:


=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang PureBasic>Enumeration
<syntaxhighlight lang="purebasic">Enumeration
#LittleEndian
#LittleEndian
#BigEndian
#BigEndian
Line 956: Line 1,189:
PrintN("and you use Big Endian.")
PrintN("and you use Big Endian.")
EndSelect
EndSelect
EndIf</lang>
EndIf</syntaxhighlight>


=={{header|Python}}==
=={{header|Python}}==
<lang python>>>> import platform, sys, socket
<syntaxhighlight lang="python">>>> import platform, sys, socket
>>> platform.architecture()
>>> platform.architecture()
('64bit', 'ELF')
('64bit', 'ELF')
Line 972: Line 1,205:
>>> socket.gethostname()
>>> socket.gethostname()
'yourhostname'
'yourhostname'
>>></lang>
>>></syntaxhighlight>


=={{header|R}}==
=={{header|R}}==
Word size
Word size
<lang R>8 * .Machine$sizeof.long # e.g. 32
<syntaxhighlight lang="r">8 * .Machine$sizeof.long # e.g. 32</syntaxhighlight>
Endianness
Endianness
<lang R>.Platform$endian # e.g. "little"</lang>
<syntaxhighlight lang="r">.Platform$endian # e.g. "little"</syntaxhighlight>


=={{header|Racket}}==
=={{header|Racket}}==
<syntaxhighlight lang="racket">
<lang Racket>
#lang racket/base
#lang racket/base


(printf "Word size: ~a\n" (system-type 'word))
(printf "Word size: ~a\n" (system-type 'word))
(printf "Endianness: ~a\n" (if (system-big-endian?) 'big 'little))
(printf "Endianness: ~a\n" (if (system-big-endian?) 'big 'little))
</syntaxhighlight>
</lang>

=={{header|Raku}}==
(formerly Perl 6)
Endian detection translated from C. {{works with|Rakudo|2018.03}}
<syntaxhighlight lang="raku" line>use NativeCall;
say $*VM.config<ptr_size>;
my $bytes = nativecast(CArray[uint8], CArray[uint16].new(1));
say $bytes[0] ?? "little-endian" !! "big-endian";</syntaxhighlight>
{{out}}
<pre>8
little-endian</pre>
Note: Rakudo 2018.12 is introducing the endian-sensitive<code>read-int16</code> method,
which makes endian detection a little easier:
<syntaxhighlight lang="raku" line>say blob8.new(1,0).read-int16(0) == 1 ?? "little-endian" !! "big-endian"</syntaxhighlight>

In Rakudo 2019.01 the dynamic KERNEL variable was fleshed out with a bunch of accessors, among them:
<syntaxhighlight lang="raku" line>say join ', ', $*KERNEL, $*KERNEL.bits, $*KERNEL.arch, $*KERNEL.endian</syntaxhighlight>
{{out}}
<pre>linux, 64, x86_64, LittleEndian</pre>


=={{header|Retro}}==
=={{header|Retro}}==
Line 993: Line 1,245:
Word Size
Word Size


<lang Retro>needs variations'
<syntaxhighlight lang="retro">needs variations'
^variations'size</lang>
^variations'size</syntaxhighlight>


Returns the number of bits per cell. This is normally 32, though may be smaller or larger on embedded systems and under special cases.
Returns the number of bits per cell. This is normally 32, though may be smaller or larger on embedded systems and under special cases.
Line 1,000: Line 1,252:
Endianness
Endianness


<lang Retro>needs variations'
<syntaxhighlight lang="retro">needs variations'
^variations'endian</lang>
^variations'endian</syntaxhighlight>


Returns 0 for little endian, and 1 for big endian.
Returns 0 for little endian, and 1 for big endian.
Line 1,011: Line 1,263:
<br>However, there is a STORAGE built-in function that allows a program to look at (local) storage, and if there is an
<br>However, there is a STORAGE built-in function that allows a program to look at (local) storage, and if there is an
<br>indicator stored anywhere in the virtual address space, it can be examined.
<br>indicator stored anywhere in the virtual address space, it can be examined.
<lang rexx>/*REXX program to examine which operating system that REXX is running under. */
<syntaxhighlight lang="rexx">/*REXX program to examine which operating system that REXX is running under. */


parse source opSys howInvoked pathName
parse source opSys howInvoked pathName


/*where opSys will indicate which operating system REXX is running under, and */
/*where opSys will indicate which operating system REXX is running under, and */
/*from that, one could make assumptions what the wordsize is, etc. */</lang>
/*from that, one could make assumptions what the wordsize is, etc. */</syntaxhighlight>


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby># We assume that a Fixnum occupies one machine word.
<syntaxhighlight lang="ruby"># We assume that a Fixnum occupies one machine word.
# Fixnum#size returns bytes (1 byte = 8 bits).
# Fixnum#size returns bytes (1 byte = 8 bits).
word_size = 42.size * 8
word_size = 42.size * 8
Line 1,028: Line 1,280:
bytes = [1].pack('S').unpack('C*')
bytes = [1].pack('S').unpack('C*')
byte_order = (bytes[0] == 0 ? 'big' : 'little') + ' endian'
byte_order = (bytes[0] == 0 ? 'big' : 'little') + ' endian'
puts "Byte order: #{byte_order}"</lang>
puts "Byte order: #{byte_order}"</syntaxhighlight>


With [[MRI]], <code>ri Fixnum</code> states, "A Fixnum holds Integer values that can be represented in a native machine word (minus 1 bit)." This bases our claim that a Fixnum occupies one machine word.
With [[MRI]], <code>ri Fixnum</code> states, "A Fixnum holds Integer values that can be represented in a native machine word (minus 1 bit)." This bases our claim that a Fixnum occupies one machine word.
Line 1,035: Line 1,287:


=={{header|Rust}}==
=={{header|Rust}}==
<lang Rust>#[derive(Copy, Clone, Debug)]
<syntaxhighlight lang="rust">#[derive(Copy, Clone, Debug)]
enum Endianness {
enum Endianness {
Big, Little,
Big, Little,
Line 1,056: Line 1,308:
println!("Word size: {} bytes", std::mem::size_of::<usize>());
println!("Word size: {} bytes", std::mem::size_of::<usize>());
println!("Endianness: {:?}", Endianness::target());
println!("Endianness: {:?}", Endianness::target());
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,063: Line 1,315:


=={{header|Scala}}==
=={{header|Scala}}==
{{libheader|Scala}}<lang Scala>import java.nio.ByteOrder
{{libheader|Scala}}<syntaxhighlight lang="scala">import java.nio.ByteOrder


object ShowByteOrder extends App {
object ShowByteOrder extends App {
Line 1,069: Line 1,321:
println(s"Word size: ${System.getProperty("sun.arch.data.model")}")
println(s"Word size: ${System.getProperty("sun.arch.data.model")}")
println(s"Endianness: ${System.getProperty("sun.cpu.endian")}")
println(s"Endianness: ${System.getProperty("sun.cpu.endian")}")
}</lang>
}</syntaxhighlight>


=={{header|Scheme}}==
=={{header|Scheme}}==
{{works with|Chicken Scheme}}<lang scheme>(define host-info
{{works with|Chicken Scheme}}<syntaxhighlight lang="scheme">(define host-info
(begin
(begin
(display "Endianness: ")
(display "Endianness: ")
Line 1,079: Line 1,331:
(display "Word Size: ")
(display "Word Size: ")
(display (if (fixnum? (expt 2 33)) 64 32))
(display (if (fixnum? (expt 2 33)) 64 32))
(newline)))</lang>
(newline)))</syntaxhighlight>
{{out}}
{{out}}
Endianness: little-endian
Endianness: little-endian
Line 1,088: Line 1,340:
The example below assumes that the word size is the size of a pointer.
The example below assumes that the word size is the size of a pointer.


<lang seed7>$ include "seed7_05.s7i";
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "cc_conf.s7i";
include "cc_conf.s7i";


Line 1,100: Line 1,352:
writeln("Big endian");
writeln("Big endian");
end if;
end if;
end func;</lang>
end func;</syntaxhighlight>


{{out}}
{{out}}
Line 1,109: Line 1,361:


=={{header|Slate}}==
=={{header|Slate}}==
<lang slate>inform: 'Endianness: ' ; Platform current endianness.
<syntaxhighlight lang="slate">inform: 'Endianness: ' ; Platform current endianness.
inform: 'Word Size: ' ; (Platform current bytesPerWord * 8) printString.</lang>
inform: 'Word Size: ' ; (Platform current bytesPerWord * 8) printString.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,119: Line 1,371:
=={{header|Tcl}}==
=={{header|Tcl}}==
This is very straightforward in Tcl. The global array <code>tcl_platform</code> contains these values. In an interactive <code>tclsh</code>:
This is very straightforward in Tcl. The global array <code>tcl_platform</code> contains these values. In an interactive <code>tclsh</code>:
<lang tcl>% parray tcl_platform
<syntaxhighlight lang="tcl">% parray tcl_platform
tcl_platform(byteOrder) = littleEndian
tcl_platform(byteOrder) = littleEndian
tcl_platform(machine) = intel
tcl_platform(machine) = intel
Line 1,128: Line 1,380:
tcl_platform(threaded) = 1
tcl_platform(threaded) = 1
tcl_platform(user) = glennj
tcl_platform(user) = glennj
tcl_platform(wordSize) = 4</lang>
tcl_platform(wordSize) = 4</syntaxhighlight>


=={{header|TI-89 BASIC}}==
=={{header|TI-89 BASIC}}==


<lang ti89b>Disp "32-bit big-endian"</lang>
<syntaxhighlight lang="ti89b">Disp "32-bit big-endian"</syntaxhighlight>


=={{header|TXR}}==
=={{header|TXR}}==
Line 1,162: Line 1,414:


No match, so big endian.
No match, so big endian.

=={{header|UNIX Shell}}==
The getconf command gets the word size, the piped command list gets the endianness , 1 means Little and 0 means Big :
<syntaxhighlight lang="bash">
Aamrun$ getconf WORD_BIT
32
Aamrun$ echo -n I | od -to2 | awk 'FNR==1{ print substr($2,6,1)}'
1
Aamrun$
</syntaxhighlight>

=={{header|Wren}}==
{{trans|C}}
As this information cannot be reliably obtained via Wren CLI, we instead embed a Wren script in a C application and ask the host program to get it for us.
<syntaxhighlight lang="wren">/* Host_introspection.wren */

class C {
foreign static wordSize
foreign static endianness
}

System.print("word size = %(C.wordSize) bits")
System.print("endianness = %(C.endianness)")</syntaxhighlight>
<br>
We now embed this Wren script in the following C program, compile and run it.
<syntaxhighlight lang="c">#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include "wren.h"

void C_wordSize(WrenVM* vm) {
/* size_t typically is exactly one word */
int ws = (int)(CHAR_BIT * sizeof(size_t));

/* return result to Wren */
wrenSetSlotDouble(vm, 0, (double)ws);
}

void C_endianness(WrenVM* vm) {
/* Check if the least significant bit is located in the lowest-address byte. */
int one = 1;
char *e = (*(char *)&one) ? "little" : "big";

/* return result to Wren */
wrenSetSlotString(vm, 0, e);
}

WrenForeignMethodFn bindForeignMethod(
WrenVM* vm,
const char* module,
const char* className,
bool isStatic,
const char* signature) {
if (strcmp(module, "main") == 0) {
if (strcmp(className, "C") == 0) {
if (isStatic && strcmp(signature, "wordSize") == 0) {
return C_wordSize;
} else if (isStatic && strcmp(signature, "endianness") == 0) {
return C_endianness;
}
}
}
return NULL;
}

static void writeFn(WrenVM* vm, const char* text) {
printf("%s", text);
}

void errorFn(WrenVM* vm, WrenErrorType errorType, const char* module, const int line, const char* msg) {
switch (errorType) {
case WREN_ERROR_COMPILE:
printf("[%s line %d] [Error] %s\n", module, line, msg);
break;
case WREN_ERROR_STACK_TRACE:
printf("[%s line %d] in %s\n", module, line, msg);
break;
case WREN_ERROR_RUNTIME:
printf("[Runtime Error] %s\n", msg);
break;
}
}

char *readFile(const char *fileName) {
FILE *f = fopen(fileName, "r");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
rewind(f);
char *script = malloc(fsize + 1);
fread(script, 1, fsize, f);
fclose(f);
script[fsize] = 0;
return script;
}

int main() {
WrenConfiguration config;
wrenInitConfiguration(&config);
config.writeFn = &writeFn;
config.errorFn = &errorFn;
config.bindForeignMethodFn = &bindForeignMethod;
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "Host_introspection.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
switch (result) {
case WREN_RESULT_COMPILE_ERROR:
printf("Compile Error!\n");
break;
case WREN_RESULT_RUNTIME_ERROR:
printf("Runtime Error!\n");
break;
case WREN_RESULT_SUCCESS:
break;
}
wrenFreeVM(vm);
free(script);
return 0;
}</syntaxhighlight>

{{out}}
The results, as expected, for my x64 Ubuntu 20.04 system are:
<pre>
word size = 64 bits
endianness = little
</pre>


=={{header|XPL0}}==
=={{header|XPL0}}==
Line 1,169: Line 1,549:
endian.
endian.


<lang XPL0>include c:\cxpl\codes; \intrinsic 'code' declarations
<syntaxhighlight lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
int A, B;
int A, B;
char C;
char C;
Line 1,178: Line 1,558:
Text(0, " endian
Text(0, " endian
");
");
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 1,185: Line 1,565:
Little endian
Little endian
</pre>
</pre>

=={{header|Z80 Assembly}}==
The Z80's word size is 16-bit, and you'd know this ahead of time simply because there aren't any commands that work with values any larger than that. It's also little-endian, but this can be proven without knowing it in advance using a simple store and load test.
<syntaxhighlight lang="z80">EndianTest:
ld hl,&8000
ld (&C000),hl ;store &8000 into memory.
ld a,(&C000) ;loads the byte at &C000 into A. If the Z80 were big-endian, A would equal &80. But it equals zero.
or a ;still, we need to pretend we don't already know the result and compare A to zero.
jr z,LittleEndian ;handle the case where Z80 is little-endian (which it is, so this branch is always taken.)

;else, do whatever you would do to show that the Z80 is big-endian (it isn't, so execution never reaches here.)</syntaxhighlight>

Latest revision as of 21:01, 9 December 2023

Task
Host introspection
You are encouraged to solve this task according to the task description, using any language you may know.

Print the word size and endianness of the host machine.

See also: Variable size/Get

68000 Assembly

It's not possible to get the word size without knowing it in advance. But the 68000's big-endian nature can easily be proven even if the programmer didn't know that it was big-endian already. Code is called as a subroutine, i.e. JSR TestEndianness. Hardware-specific print routines are unimplemented.

TestEndianness:
LEA UserRam,A0
MOVE.L #$0000FFFF,(A0)
MOVE.B (A0),D0	;read the 0th byte stored
BEQ isBigEndian	;if this was little endian, the bytes would be stored FF FF 00 00
         
;must have been little-endian. Spoiler alert: execution will never reach here
	LEA LittleEndianMessage,A3
	JSR PrintString
	rts
isBigEndian:
	LEA BigEndianMessage,A3
	JSR PrintString
	rts

BigEndianMessage:
	DC.B "BIG-ENDIAN",0
	EVEN
LittleEndianMessage:
	DC.B "LITTLE-ENDIAN",0
	EVEN

8086 Assembly

As with 68000 Assembly, there's no way to "prove" the word size without knowing it in advance. But endianness can still be tested for quite easily.

    .model small
    .stack 1024

    .data

UserRam BYTE 256 DUP (0)

    .code
start:

    mov ax,@data  ;assembler calculates this offset for us
    mov ds,ax     ;the 8086 can only load segment registers from other registers, not directly from immediate values.

    mov ax,@code
    mov es,ax     

    mov ax,3422h
    mov word ptr [ds:UserRam],ax
    mov bl, byte ptr [ds:UserRam]
    call doMonitor ;a routine that prints the contents of 
                   ;the 8086's registers to the screen

    mov ax,4C00h
    int 21h        ;return to MS-DOS

    end start

If the 8086 is little-endian, BX will equal 0022, since we loaded the low byte of UserRam into BL (the low half of BX). If it's big-endian, BX will equal 0034.

Output:
Monitor tools created by Keith of Chibiakumas
AX:3422 BX:0022 CX:00FF DX:0192
F :------I---------     IP:0018
SP:03FA BP:091C DI:0400 SI:0388
CS:01A2 DS:01EC ES:01A2 SS:0425

From this we conclude that the 8086 is indeed a little-endian CPU.

Action!

PROC Main()
  PrintE("All Atari 8-bit computers use little-endian word of 16-bits size.")
RETURN
Output:

Screenshot from Atari 8-bit computer

All Atari 8-bit computers use little-endian word of 16-bits size.

Ada

with Ada.Text_IO;  use Ada.Text_IO;
with System;       use System;

procedure Host_Introspection is
begin
   Put_Line ("Word size" & Integer'Image (Word_Size));
   Put_Line ("Endianness " & Bit_Order'Image (Default_Bit_Order));
end Host_Introspection;
Sample output on a Pentium machine:
Word size 32
Endianness LOW_ORDER_FIRST

ALGOL 68

Works with: ALGOL 68 version Revision 1 - no extensions to language used
Works with: ALGOL 68G version Any - tested with release 1.18.0-9h.tiny
INT max abs bit = ABS(BIN 1 SHL 1)-1;
INT bits per char = ENTIER (ln(max abs char+1)/ln(max abs bit+1));
INT bits per int = ENTIER (1+ln(max int+1.0)/ln(max abs bit+1));

printf(($"states per bit: "dl$,max abs bit+1));
printf(($"bits per char: "z-dl$,bits per char));
printf(($"bits per int:  "z-dl$,bits per int));
printf(($"chars per int: "z-dl$,bits per int OVER bits per char));

printf(($"bits width: "z-dl$, bits width));

STRING abcds = "ABCD";
FILE abcdf;
INT abcdi;

INT errno := open(abcdf, "abcd.dat",stand back channel);
put(abcdf,abcds); # output alphabetically #
reset(abcdf);
get bin(abcdf,abcdi); # input in word byte order #
STRING int byte order := "";
FOR shift FROM 0 BY bits per char TO bits per int - bits per char DO
  int byte order +:= REPR(abcdi OVER (max abs bit+1) ** shift MOD (max abs char+1))
OD;
printf(($"int byte order: "g,", Hex:",16r8dl$,int byte order, BIN abcdi))
Output:

(Intel i686)

states per bit:  2
bits per char:   8
bits per int:   32
chars per int:   4
bits width:  32
int byte order: ABCD, Hex:44434241

On older CPUs the results would vary:

ALGOL 68R ALGOL 68RS
~
bits per char:   6
bits per int:   24
chars per int:   4
ICL 2900
bits per char:   8
bits per int:   32
chars per int:   4
Multics
bits per char:   6
bits per int:   36
chars per int:   6

Applesoft BASIC

1  DATA248,169,153,24,105,1,48
2  DATA6,24,251,144,2,251,56
3  DATA216,105,0,133,251,96
4  FOR I = 768 TO 787
5  READ B: POKE I,B: NEXT 
6  CALL 768:M =  PEEK (251)
7  PRINT " WORD SIZE: ";
8  IF  NOT M THEN  PRINT 8
9 M$ = "HYBRID 8/16"
10  IF M THEN  PRINT M$
11  PRINT "ENDIANNESS: ";
12  PRINT "LITTLE-ENDIAN"

ARM Assembly

The word size of the ARM is 32-bit, which can't really be proven without knowing it ahead of time.

The ARM CPU's endianness can be set to either little-endian or big-endian. Not all ARM CPUs have this feature, but this test will work regardless of whether the endian switch features exist on any particular model or not. The easiest way to test endianness is to write a word to RAM, then read the 0th byte from that memory location and see what it is. (The example below uses VASM syntax.)

EndianTest:
mov r0,#0xFF
mov r1,#0x02000000   ;an arbitrary memory location on the Game Boy Advance. 
                     ;(The GBA is always little-endian but this test doesn't use that knowledge to prove it.)
str r0,[r1]          ;on a little-endian CPU a hexdump of 0x02000000 would be: FF 00 00 00
                     ;on a big-endian CPU it would be:                         00 00 00 FF
ldrB r0,[r1]         ;load just the byte at 0x02000000. If the machine is big-endian this will load 00; if little-endian, 0xFF.
cmp r0,#0
beq isBigEndian
;else, do whatever is needed to display "little-endian" to the screen. This part isn't implemented.

Babel

main : 
    { "Word size: " << msize 3 shl %d << " bits" cr << 
     "Endianness: " << { endian } { "little" } { "big" } ifte cr << }

BBC BASIC

      DIM P% 8
      !P% = -1
      I% = 0 : REPEAT I% += 1 : UNTIL P%?I%=0
      PRINT "Word size = " ; I% " bytes"
      !P% = 1
      IF P%?0 = 1 THEN PRINT "Little-endian"
      IF P%?(I%-1) = 1 THEN PRINT "Big-endian"

The 'word size' is reported as the number of bytes accessed by the ! indirection operator, which is 4 in all current versions of BBC BASIC.

C

#include <stdio.h>
#include <stddef.h> /* for size_t */
#include <limits.h> /* for CHAR_BIT */

int main() {
    int one = 1;

    /*
     * Best bet: size_t typically is exactly one word.
     */
    printf("word size = %d bits\n", (int)(CHAR_BIT * sizeof(size_t)));

    /*
     * Check if the least significant bit is located
     * in the lowest-address byte.
     */
    if (*(char *)&one)
        printf("little endian\n");
    else
        printf("big endian\n");
    return 0;
}

On POSIX-compatible systems, the following also tests the endianness (this makes use of the fact that network order is big endian):

#include <stdio.h>
#include <arpa/inet.h>

int main()
{
  if (htonl(1) == 1)
    printf("big endian\n");
  else
    printf("little endian\n");
}

C#

static void Main()
{
  Console.WriteLine("Word size = {0} bytes,",sizeof(int));

  if (BitConverter.IsLittleEndian)
    Console.WriteLine("Little-endian.");
  else
    Console.WriteLine("Big-endian.");
}

C++

#include <bit>
#include <iostream>

int main()
{
    std::cout << "int is " << sizeof(int) << " bytes\n";
    std::cout << "a pointer is " << sizeof(int*) << " bytes\n\n";

    if (std::endian::native == std::endian::big)
    {
        std::cout << "platform is big-endian\n";
    }
    else
    {
        std::cout << "host is little-endian\n";
    }
}
Output:
int is 4 bytes
a pointer is 8 bytes

host is little-endian

Caché ObjectScript

USER>Write "Word Size: "_$Case($System.Version.Is64Bits(), 1: 64, : 32)
Word Size: 32

USER>Write "Endianness: "_$Case($System.Version.IsBigEndian(), 1: "Big", : "Little")
Endianness: Little

Clojure

(println "word size: " (System/getProperty "sun.arch.data.model"))
(println "endianness: " (System/getProperty "sun.cpu.endian"))

Common Lisp

Common Lisp doesn't provide a native way to reliably determine this (though some unlike other languages, you rarely, if ever, need this information).

The Environment has some implementation-specific functions that might provide a good hint, e.g.,

(machine-type) ;; => "X86-64" on SBCL here

The *features* list also provides useful information, e.g., some compilers declare :LITTLE-ENDIAN there.

The cl-trivial-features library standardizes this, so you will always get either :LITTLE-ENDIAN or :BIG-ENDIAN. It also adds the CPU (:X86, :X86-64, :PPC, :PPC64, etc.), from which you can probably derive the word size, but it's not (yet) available as a separate flag.

D

void main() {
  import std.stdio, std.system;

  writeln("Word size = ", size_t.sizeof * 8, " bits.");
  writeln(endian == Endian.littleEndian ? "Little" : "Big", " endian.");
}
Output:
Word size = 64 bits.
Little endian.

Delphi

program HostIntrospection ;

{$APPTYPE CONSOLE}

uses SysUtils;

begin
  Writeln('word size: ', SizeOf(Integer));
  Writeln('endianness: little endian'); // Windows is always little endian
end.

Erlang

To find the word size:

1> erlang:system_info(wordsize).
4

In the case of endianness, Erlang's bit syntax by default has a 'native' option which lets you use what is supported natively. As such, there is no function to find endianness. However, one could write one by using bit syntax, setting endianness and then comparing to the native format:

1> <<1:4/native-unit:8>>.
<<1,0,0,0>>
2> <<1:4/big-unit:8>>
<<0,0,0,1>>
3> <<1:4/little-unit:8>>.
<<1,0,0,0>>

And so the following function would output endianness:

endianness() when <<1:4/native-unit:8>> =:= <<1:4/big-unit:8>> -> big;
endianness() -> little.

F#

A lot of research before I finally came up with an answer to this that isn't dependent on the machine it was compiled on. Works on Win32 machines only (obviously, due to the interop). I think that strictly speaking, I should be double checking the OS version before making the call to wow64Process, but I'm not worrying about it.

open System
open System.Runtime.InteropServices
open System.Diagnostics

[<DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)>]
extern bool IsWow64Process(nativeint hProcess, bool &wow64Process);

let answerHostInfo =
    let Is64Bit() =
        let mutable f64Bit = false;
        IsWow64Process(Process.GetCurrentProcess().Handle, &f64Bit) |> ignore
        f64Bit
    let IsLittleEndian() = BitConverter.IsLittleEndian
    (IsLittleEndian(), Is64Bit())

Factor

USING: alien.c-types alien.data io layouts ;
"Word size: " write cell 8 * .
"Endianness: " write little-endian? "little" "big" ? print

Forth

: endian
  cr 1 cells . ." address units per cell"
  s" ADDRESS-UNIT-BITS" environment? if cr . ." bits per address unit" then
  cr 1 here ! here c@ if ." little" else ." big" then ."  endian" ;

This relies on c@ being a byte fetch (4 chars = 1 cells). Although it is on most architectures, ANS Forth only guarantees that 1 chars <= 1 cells. Some Forths like OpenFirmware have explicitly sized fetches, like b@.

Fortran

Works with: Fortran version 90 and < 2018
   integer :: i
   character(len=1) :: c(20)
   equivalence (c, i)

   WRITE(*,*) bit_size(1)  ! number of bits in the default integer type
                           ! which may (or may not!) equal the word size
   i = 1

   IF (ichar(c(1)) == 0) THEN
      WRITE(*,*) "Big Endian"
   ELSE
     WRITE(*,*) "Little Endian"
   END IF
Works with: Fortran version 77 and later
      PROGRAM endianness
      IMPLICIT NONE
      INTEGER(KIND=4)  :: i = 1

      !ISHFT(INTEGER, SHIFT) : Left shift if SHIFT > 0
      !ISHFT(INTEGER, SHIFT) : Right shift if SHIFT < 0
      IF (ISHFT(i,1) .EQ. 0) THEN
        WRITE(*,FMT='(A)') 'Architechture is Big Endian'
      ELSE
        WRITE(*,FMT='(A)') 'Architecture is Little Endian'
      END IF

      RETURN

      STOP
      END PROGRAM endianness

FreeBASIC

' FB 1.05.0 Win64 (so little endian, 8 byte word size, expected)

' uses intrinsic defines, set by the compiler

#Ifdef __FB_64BIT__
  Print "Host has an 8 byte word size"
#Else
  Print "Host has a 4 byte word size"
#EndIf

#Ifdef __FB_BIGENDIAN__
  Print "Host is big endian"
#Else
  Print "Host is little endian"
#EndIf

Sleep
Output:
Host has an 8 byte word size
Host is little endian

Frink

println["Word size:  " + callJava["java.lang.System", "getProperty", "sun.arch.data.model"]]
println["Endianness: " + callJava["java.lang.System", "getProperty", "sun.cpu.endian"]]

Go

package main

import (
	"fmt"
	"io/ioutil"
	"runtime"
	"strconv"
	"strings"
	"unsafe"
)

func main() {
	fmt.Println(runtime.Version(), runtime.GOOS, runtime.GOARCH)

	// Inspect a uint32 variable to determine endianness.
	x := uint32(0x01020304)
	switch *(*byte)(unsafe.Pointer(&x)) {
	case 0x01:
		fmt.Println("big endian")
	case 0x04:
		fmt.Println("little endian")
	default:
		fmt.Println("mixed endian?")
	}

	// Usually one cares about the size the executible was compiled for
	// rather than the actual underlying host's size.

	// There are several ways of determining the size of an int/uint.
	fmt.Println("         strconv.IntSize =", strconv.IntSize)
	// That uses the following definition we can also be done by hand
	intSize := 32 << uint(^uint(0)>>63)
	fmt.Println("32 << uint(^uint(0)>>63) =", intSize)

	// With Go 1.0, 64-bit architectures had 32-bit int and 64-bit
	// uintptr. This was changed in Go 1.1. In general it would
	// still be possible that int and uintptr (the type large enough
	// to hold the bit pattern of any pointer) are of different sizes.
	const bitsPerByte = 8
	fmt.Println("  sizeof(int)     in bits:", unsafe.Sizeof(int(0))*bitsPerByte)
	fmt.Println("  sizeof(uintptr) in bits:", unsafe.Sizeof(uintptr(0))*bitsPerByte)
	// If we really want to know the architecture size the executable was
	// compiled for and not the size of int it safest to take the max of those.
	archSize := unsafe.Sizeof(int(0))
	if psize := unsafe.Sizeof(uintptr(0)); psize > archSize {
		archSize = psize
	}
	fmt.Println("  compiled with word size:", archSize*bitsPerByte)

	// There are some *very* unportable ways to attempt to get the actual
	// underlying hosts' word size.
	// Inspect cpuinfo to determine word size (some unix-like OS' only).
	c, err := ioutil.ReadFile("/proc/cpuinfo")
	if err != nil {
		fmt.Println(err)
		return
	}
	ls := strings.Split(string(c), "\n")
	for _, l := range ls {
		if strings.HasPrefix(l, "flags") {
			for _, f := range strings.Fields(l) {
				if f == "lm" { // "long mode"
					fmt.Println("64 bit word size")
					return
				}
			}
			fmt.Println("32 bit word size")
			return
		}
	}
}
Output:
go1.3.1 freebsd amd64
little endian
         strconv.IntSize = 64
32 << uint(^uint(0)>>63) = 64
  sizeof(int)     in bits: 64
  sizeof(uintptr) in bits: 64
  compiled with word size: 64
open /proc/cpuinfo: no such file or directory
go1.3.1 freebsd 386
little endian
         strconv.IntSize = 32
32 << uint(^uint(0)>>63) = 32
  sizeof(int)     in bits: 32
  sizeof(uintptr) in bits: 32
  compiled with word size: 32
open /proc/cpuinfo: no such file or directory
go1.3.1 nacl amd64p32
little endian
         strconv.IntSize = 32
32 << uint(^uint(0)>>63) = 32
  sizeof(int)     in bits: 32
  sizeof(uintptr) in bits: 32
  compiled with word size: 32
open /proc/cpuinfo: No such file or directory

Alternative technique:

package main

import (
    "debug/elf"
    "fmt"
    "os"
)

func main() {
    f, err := elf.Open(os.Args[0])
    if err != nil {
        fmt.Println("  ", err)
        return
    }
    fmt.Println(f.FileHeader.ByteOrder)
    f.Close()
}
Output:
LittleEndian

Groovy

Solution follows Java:

println "word size:  ${System.getProperty('sun.arch.data.model')}"
println "endianness: ${System.getProperty('sun.cpu.endian')}"
Output:
word size:  64
endianness: little

Haskell

import Data.Bits
import ADNS.Endian -- http://hackage.haskell.org/package/hsdns

main = do
  putStrLn $ "Word size: " ++ bitsize
  putStrLn $ "Endianness: " ++ show endian
      where
        bitsize = show $ bitSize (undefined :: Int)

Icon and Unicon

procedure main()
    write(if 0 = ishift(1,-1) then "little" else "big"," endian")
    if match("flags",line := !open("/proc/cpuinfo")) then    # Unix-like only
        write(if find(" lm ",line) then 64 else 32," bits per word")
    else write("Cannot determine word size.")
end

Sample run:

->hi
little endian
64 bits per word
->

J

   IF64 {32 64
64

This returns 32 in 32 bit J.

This value could also be calculated, exercising left shift of bits in a 2s complement fixed width integer:

   2+2^.>./1&(33 b.)^:a:1
64

Note that this mechanism is testing the interpreter, and not the OS or Hardware. (Though, of course, you cannot run a 64 bit interpreter on a machine that does not support it.)

That said, this does not deal with endianness. For the most part, J programs do not need to know their own endianness. When converting to and from binary format you can specify "native", "little endian" and "big endian", and it's rare that you have an interface which would need anything else. That said, you can inspect the binary representation of a simple constant:

   ":&> (|: 32 64  ;"0 big`little) {"_1~ 2 2 #: 16b_e0 + a. i. 0 { 3!:1  '' 
64    
little

Java

Java conceals the byte order of its integers, but reports the native byte order through java.nio.ByteOrder.nativeOrder().

Works with: Java version 1.4
import java.nio.ByteOrder;

public class ShowByteOrder {
    public static void main(String[] args) {
        // Print "BIG_ENDIAN" or "LITTLE_ENDIAN".
        System.out.println(ByteOrder.nativeOrder());
    }
}

Some JVMs also have system properties for the word size and byte order.

System.out.println("word size: "+System.getProperty("sun.arch.data.model"));
System.out.println("endianness: "+System.getProperty("sun.cpu.endian"));

Julia

Julia creates ENDIAN_BOM a 32 bit unsigned integer out of an array of 4 8 bit unsigned integers to serve as an endianness marker.

print("This host's word size is ", WORD_SIZE, ".")
if ENDIAN_BOM == 0x04030201
    println("And it is a little-endian machine.")
elseif ENDIAN_BOM == 0x01020304
    println("And it is a big-endian machine.")
else
    println("ENDIAN_BOM = ", ENDIAN_BOM, ", which is confusing")
end
Output:
This host's word size is 64.And it is a little-endian machine.

Kotlin

The following is not guaranteed to work on all JVMs but is working fine on my x64 Windows 10 machine:

// version 1.0.6

fun main(args: Array<String>) {
    println("Word size : ${System.getProperty("sun.arch.data.model")} bits")
    println("Endianness: ${System.getProperty("sun.cpu.endian")}-endian")
}
Output:
Word size : 64 bits
Endianness: little-endian

Lua

Pure/native Lua can't do this (and essentially doesn't care). However, Lua is often used in a scripting environment, where such issues may be important, and any needed support would be expected to be provided by the host or some other external library. Here using ffi:

ffi = require("ffi")
print("size of int (in bytes):  " .. ffi.sizeof(ffi.new("int")))
print("size of pointer (in bytes):  " .. ffi.sizeof(ffi.new("int*")))
print((ffi.abi("le") and "little" or "big") .. " endian")
Output:
size of int (in bytes):  4
size of pointer (in bytes):  8
little endian

M2000 Interpreter

Module CheckIt {
      \\ Always run in Little-endian, 32 bits (in Wow64 in 64 bit os)
      Module EndiannessAndSize {
            Buffer Check as Long
            Return Check, 0:=1
            if eval(Check, 0 as byte)=1 then {
                  Print "Little-endian"
            }
            \\ 4 bytes
            Print "Word size:"; Len(Check)*8;" bits"
      }
      EndiannessAndSize
      \\ Access to internal com object clsOsInfo
      Declare OsInfo Information
      Print Type$(OsInfo) ="clsOSInfo"
      \\ Build is a read only property
      With OsInfo, "Build" as Build, "OSName" as OSName$, "IsElevated" as IsElevated
      Print OsName$
      Print "Build=";Build
      \\ IsWow64 is a function
      Method OsInfo, "IsWow64" as IsWow64
      If  IsWow64 Then {
            Print "64 bit Os"
      } Else {
            Print  "32 bit OS"
      }
      Print "IsElevated:";IsElevated
}
Checkit

MACRO-10

title  Host Introspection
subttl PDP-10 assembly (MACRO-10 on TOPS-20). KJX 2022.
search monsym,macsym

comment \
        The wordsize is detected by putting 1 into a re-
        gister, counting the leading zeros (resulting in
        wordsize-1) and adding 1 to the result.

        Endianness doesn't really apply, as the PDP-10 is
        a 36bit word-adressable computer, and the handling
        of characters is peculiar enough that it would get
        out of hand if I'd dive into the details here.
        \

a=:1                            ;Define three accumulators.
b=:2
c=:3

start:: reset%                  ;Initialize process.

        movei a,1               ;Set A to 1.
        jffo a,.+1              ;B = leading zeros of A.
        aos b                   ;Add 1 to B. -> wordsize.

        movei a,.priou          ;Print B on standard output
        movei c,^d10            ;in base 10.
        nout%
          jfcl

        haltf%                  ;Halt program.
        jrst start              ;Allow continue-command.

end start

Mathematica / Wolfram Language

If[$ByteOrdering > 0, Print["Big endian"], Print["Little endian" ]] 
$SystemWordLength "bits"
Output:

x86

Little endian
32 bits

MATLAB / Octave

The concept of "word size" is not meaningful in Matlab and Octave, uint64 is also available on 32bit-platforms, and there are no pointers. Endianity can be tested with the function below:

  function [endian]=endian()
    fid=tmpfile();
    fwrite(fid,1:8,'uint8');

    fseek(fid,0,'bof');
    t=fread(fid,8,'int8');
    i8=sprintf('%02X',t);

    fseek(fid,0,'bof');
    t=fread(fid,4,'int16');
    i16=sprintf('%04X',t);

    fclose(fid);

    if strcmp(i8,i16) endian='big';
    else endian='little';
    end;
Output:
  octave:128> computer 
  x86_64-unknown-linux-gnu
  octave:129> endian
  endian = little

MIPS Assembly

This uses Keith S.'s tutorial at Chibialiens.com to print memory and show register contents. As I've come to find out, MIPS is a bi-endian architecture (meaning its endianness is implementation-defined rather than a constant trait of the CPU.) In particular, the PlayStation 1 is little-endian, and the Nintendo 64 is big-endian. This can be proven with the test below. (Hardware-specific routines MonitorA0A1RAPC and MemDump are omitted just to keep things brief.)

	jal Cls	 ;Zero Graphics cursor position
	nop      ;on the PlayStation, the instruction AFTER a branch gets executed BEFORE the branch actually occurs. 
                 ;The Nintendo 64 didn't have this "feature" but for compatibility's sake 
                 ;      it's staying in regardless of which version of the code I'm using.

	la a2,TestData		;Load address of TestData
	
	lw a0,(a2)		;Load Word into A0 from address in A2
	
	addiu a2,4              ;pointer arithmetic to load the next word.
	lw a1,(a2) 
        
        move t6,ra
        jal MonitorA0A1RAPC
        nop
        
	li t6,2		        ;Line Count - 2 lines = 16 bytes
	jal MemDump	        ;Dump Ram to screen
	nop
	
halt:	
	j halt                  ;loop forever
	nop




TestData:
	.byte 0xF3,0xF2,0xF1,0xF0   ;this will load as F0F1F2F3 on little-endian machines, and as-is on big-endian
	.word 0xF0F1F2F3            ;this will load as F0F1F2F3 regardless of endianness.
Output:

Register Dump of PlayStation 1:

a0:F0F1F2F3 a1:F0F1F2F3

Register Dump of Nintendo 64:

a0:F3F2F1F0 a1:F0F1F2F3

It also seems the registers are 32-bit even on the N64. I wouldn't have expected that to be honest...

Modula-3

MODULE Host EXPORTS Main;

IMPORT IO, Fmt, Word, Swap;

BEGIN
  IO.Put("Word Size: " & Fmt.Int(Word.Size) & "\n");
  IF Swap.endian = Swap.Endian.Big THEN
    IO.Put("Endianness: Big\n");
  ELSE
    IO.Put("Endianness: Little\n");
  END;
END Host.
Output:

(on an x86)

Word Size: 32
Endianness: Little

Neko

NekoVM can include shared library functions that adhere to an API of passing Neko values and library file naming. A small C helper is included here to get at the Host wordsize. NekoVM link library search path (.ndll files), includes looking in current directory. The endianess test is a BUILTIN (accessible with leading $ identifier).

C support file, host-introspection.c

/* Return wordsize to Neko */
/* From Rosetta Code, C entry, with Neko marshalling */

#include <stdio.h>
#include <stddef.h> /* for size_t */
#include <limits.h> /* for CHAR_BIT */
#include <neko.h>

value wordsize(void) {
    /*
     * Best bet: size_t typically is exactly one word.
     */
    return alloc_int((int)(CHAR_BIT * sizeof(size_t)));
}
/* Expose symbol to Neko loader */
DEFINE_PRIM(wordsize, 0);

Neko caller, host-introspection.neko

/**
 Host introspection, in Neko
*/

/* higher order byte first?  Intel being little ended. */
$print("isbigendian: ", $isbigendian(), "\n")

/*
  Getting at word size is a little more difficult in Neko source.
  Neko is a fixed bit-width VM, Int is 31 bits, 30 signed, etc.
  There is no builtin native sizeof, but a few lines of
  C data marshalling wrapper, a small change to tectonics, and...
*/

var wordsize = $loader.loadprim("native@wordsize", 0)
$print("wordsize: ", wordsize(), " bits\n")
Output:
prompt$ gcc -shared -fPIC host-introspection.c -o native.ndll
prompt$ nekoc host-introspection.neko                        
prompt$ neko host-introspection.n                            
isbigendian: false
wordsize: 64 bits

NetRexx

Translation of: Java

NetRexx can access this information from the Java virtual machine in the same way as the Java sample above.

/* NetRexx */
options replace format comments java crossref savelog symbols nobinary

wordSize = System.getProperty('sun.arch.data.model')
endian   = System.getProperty('sun.cpu.endian')

say ' word size:' wordSize
say 'endianness:' endian

Nim

In Nim, "int" type has the size of the word. So, to find the word size in bits, just multiply the "int" size in bytes by eight.

echo cpuEndian
echo sizeof(int) * 8

Objective-C

Endianness:

switch (NSHostByteOrder()) {
  case NS_BigEndian:
    NSLog(@"%@", @"Big Endian");
    break;
  case NS_LittleEndian:
    NSLog(@"%@", @"Little Endian");
    break;
  case NS_UnknownByteOrder:
    NSLog(@"%@", @"endianness unknown");
    break;
}

Architecture: (works on Mac OS X 10.6+)

switch ([NSRunningApplication currentApplication].executableArchitecture) {
  case NSBundleExecutableArchitectureI386:
    NSLog(@"%@", @"i386 32-bit");
    break;

  case NSBundleExecutableArchitectureX86_64:
    NSLog(@"%@", @"x86_64 64-bit");
    break;

  case NSBundleExecutableArchitecturePPC:
    NSLog(@"%@", @"PPC 32-bit");
    break;

  case NSBundleExecutableArchitecturePPC64:
    NSLog(@"%@", @"PPC64 64-bit");
    break;

  default:
    NSLog(@"%@", @"Unknown");
    break;
}

OCaml

Printf.printf "%d\n" Sys.word_size; (* Print word size *)
Printf.printf "%s\n" Sys.os_type;   (* Print operating system *)
Works with: OCaml version 4.00+
(* Print endianness *)
Printf.printf "%s\n" (if Sys.big_endian then "big endian" else "little endian");

On OCaml 3 and below, there are tricks to get endianness. For example in Linux or Unix variants, one may use the uname shell command :

let uname arg =
  let arg = if arg = "" then "-" else arg in
  let ic = Unix.open_process_in ("uname -" ^ arg) in
  (input_line ic)
;;

# uname "sm";;
- : string = "Linux i686"

In most cases, endianness can be infered from informations given by uname.

One may also read files in the /proc directory in order to get informations about the host, only under linux :

(* Reading all the lines from a file.
If the loop is implemented by a recursive auxiliary function, the try...with breaks
tail recursion if not written carefully *)
let lines name =
  let f = open_in name
  and r = ref []
  in
  (try
     while true do
       r := (input_line f)::!r
     done
   with End_of_file -> close_in f);
  (List.rev !r)
;;

# lines "/proc/meminfo";;
- : string list =
["MemTotal:      2075240 kB"; "MemFree:        469964 kB";
 "Buffers:         34512 kB"; "Cached:        1296380 kB";
 "SwapCached:         96 kB"; "Active:         317484 kB";
 "Inactive:      1233500 kB"; "HighTotal:     1178432 kB";
 "HighFree:        45508 kB"; "LowTotal:       896808 kB";
 "LowFree:        424456 kB"; "SwapTotal:     2650684 kB";
 "SwapFree:      2650588 kB"; "Dirty:             228 kB";
 "Writeback:           0 kB"; "AnonPages:      220036 kB";
 "Mapped:          67160 kB"; "Slab:            41540 kB";
 "SReclaimable:    34872 kB"; "SUnreclaim:       6668 kB";
 "PageTables:       1880 kB"; "NFS_Unstable:        0 kB";
 "Bounce:              0 kB"; "WritebackTmp:        0 kB";
 "CommitLimit:   3688304 kB"; "Committed_AS:   549912 kB";
 "VmallocTotal:   114680 kB"; "VmallocUsed:      5172 kB";
 "VmallocChunk:   109320 kB"; "HugePages_Total:     0";
 "HugePages_Free:      0"; "HugePages_Rsvd:      0";
 "HugePages_Surp:      0"; "Hugepagesize:     4096 kB"]

Same methods can be used to get the results of commands lshw, dmidecode...

Pascal

program HostIntrospection(output);
begin
  writeln('Pointer size: ', SizeOf(Pointer), ' byte, i.e. ', SizeOf(Pointer)*8, ' bit.');
{ NtoBE converts from native endianess to big endianess }
  if 23453 = NtoBE(23453) then
    writeln('This host is big endian.')
  else
    writeln('This host is little endian.');
end.
Output:
>: ./HostIntrospection
Pointer size: 4 byte, i.e. 32 bit.
This host is little endian.

Perl

Most basic example:

use Config;
print "UV size: $Config{uvsize}, byte order: $Config{byteorder}\n";
Output:
UV size: 4, byte order: 1234

More verbose example:

use 5.010;
use Config;
my ($size, $order, $end) = @Config{qw(uvsize byteorder)};
given ($order) {
    when (join '', sort split '') { $end = 'little' }
    when (join '', reverse sort split '') { $end = 'big' }
    default { $end = 'mixed' }
}
say "UV size: $size, byte order: $order ($end-endian)";
Output:
UV size: 4, byte order: 1234 (little-endian)
UV size: 4, byte order: 3412 (mixed-endian)
UV size: 8, byte order: 87654321 (big-endian)

Phix

Note that machine_word() and machine_bits() test the interpreter or compiled executable, rather than the OS or hardware.
Also, all known implementations of Phix are currently little-endian. See also platform(), which yields WINDOWS/LINUX/JS.

with javascript_semantics
function endianness()
    if platform()=JS then
        return "n/a (web browser)"
    end if
    atom m4 = allocate(4)
    poke4(m4,#01020304)
    integer b1 = peek1s(m4)
    free(m4)
    if b1=#01 then
        return "big-endian"
    elsif b1=#04 then
        return "little-endian"
    else
        return "???"
    end if
end function
 
printf(1,"Endianness: %s\n",{endianness()})
printf(1,"Word size: %d bytes/%d bits\n",{machine_word(),machine_bits()})
Output:
Endianness: little-endian
Word size: 4 bytes/32 bits

or

Endianness: little-endian
Word size: 8 bytes/64 bits

or

Endianness: n/a (web browser)
Word size: 4 bytes/32 bits

PicoLisp

We inspect the ELF header of the executable file (the 'cmd' function returns the path to the command that invoked the interpreter). Note that this (like most other contributions to this task) only tells how the binary was compiled/assembled/linked, not necessarily the nature of the underlying system.

(in (cmd)                              # Inspect ELF header
   (rd 4)                              # Skip "7F" and 'E', 'L' and 'F'
   (prinl
      (case (rd 1)                     # Get EI_CLASS byte
         (1 "32 bits")
         (2 "64 bits")
         (T "Bad EI_CLASS") ) )
   (prinl
      (case (rd 1)                     # Get EI_DATA byte
         (1 "Little endian")
         (2 "Big endian")
         (T "Bad EI_DATA") ) ) )
Output:
64 bits
Little endian

PL/I

details: procedure options (main); /* 6 July 2012 */
	declare x float, i fixed binary initial (1);

	put skip list ('word size=', length(unspec(x)));

	if unspec(i) = '0000000000000001'b then
		put skip list ('Big endian');
	else
		put skip list ('Little endian');
	
end details;
Output:
word size=                          32 
Little endian 

PowerShell

Write-Host Word Size: ((Get-WMIObject Win32_Processor).DataWidth)
Write-Host -NoNewLine "Endianness: "
if ([BitConverter]::IsLittleEndian) {
    Write-Host Little-Endian
} else {
    Write-Host Big-Endian
}

Note that endianness is essentially a moot point with PowerShell, as there is only a Windows implementation currently and current Windows versions don't run on big-endian systems. But in theory this check should work.

PureBasic

Enumeration 
  #LittleEndian
  #BigEndian
EndEnumeration

ProcedureDLL EndianTest()
  Protected Endian = #LittleEndian
  Protected dummy.l= 'ABCD'
  If "A"=Chr(PeekA(@dummy))
    Endian=#BigEndian
  EndIf
  ProcedureReturn Endian  
EndProcedure

;- *** Start of test code
If OpenConsole()  
  PrintN("Your word size is "+Str(SizeOf(Integer)) +" bytes,")
  Select EndianTest()
    Case #LittleEndian
      PrintN("and you use Little Endian.")
    Default
      PrintN("and you use Big Endian.")
  EndSelect
EndIf

Python

>>> import platform, sys, socket
>>> platform.architecture()
('64bit', 'ELF')
>>> platform.machine()
'x86_64'
>>> platform.node()
'yourhostname'
>>> platform.system()
'Linux'
>>> sys.byteorder
little
>>> socket.gethostname()
'yourhostname'
>>>

R

Word size

8 * .Machine$sizeof.long # e.g. 32

Endianness

.Platform$endian         # e.g. "little"

Racket

#lang racket/base

(printf "Word size: ~a\n" (system-type 'word))
(printf "Endianness: ~a\n" (if (system-big-endian?) 'big 'little))

Raku

(formerly Perl 6)

Endian detection translated from C.

Works with: Rakudo version 2018.03
use NativeCall;
say $*VM.config<ptr_size>;
my $bytes = nativecast(CArray[uint8], CArray[uint16].new(1));
say $bytes[0] ?? "little-endian" !! "big-endian";
Output:
8
little-endian

Note: Rakudo 2018.12 is introducing the endian-sensitiveread-int16 method, which makes endian detection a little easier:

say blob8.new(1,0).read-int16(0) == 1 ?? "little-endian" !! "big-endian"

In Rakudo 2019.01 the dynamic KERNEL variable was fleshed out with a bunch of accessors, among them:

say join ', ', $*KERNEL, $*KERNEL.bits, $*KERNEL.arch, $*KERNEL.endian
Output:
linux, 64, x86_64, LittleEndian

Retro

These introspections are possible through the standard variations library.

Word Size

needs variations'
^variations'size

Returns the number of bits per cell. This is normally 32, though may be smaller or larger on embedded systems and under special cases.

Endianness

needs variations'
^variations'endian

Returns 0 for little endian, and 1 for big endian.

REXX

Since all variables in the REXX language are stored as characters, the wordsize is immaterial (REXX supports variable precision for numbers).
This also applies to the "endianness" of words or how they are stored.
The REXX language was designed for scripting and interfacing with the operating system.
However, there is a STORAGE built-in function that allows a program to look at (local) storage, and if there is an
indicator stored anywhere in the virtual address space, it can be examined.

/*REXX program to examine which operating system that REXX is running under. */

parse source opSys howInvoked pathName

/*where  opSys  will indicate which operating system REXX is running under, and */
/*from that, one could make assumptions what the wordsize is, etc.              */

Ruby

# We assume that a Fixnum occupies one machine word.
# Fixnum#size returns bytes (1 byte = 8 bits).
word_size = 42.size * 8
puts "Word size: #{word_size} bits"

# Array#pack knows the native byte order. We pack 1 as a 16-bit integer,
# then unpack bytes: [0, 1] is big endian, [1, 0] is little endian.
bytes = [1].pack('S').unpack('C*')
byte_order = (bytes[0] == 0 ? 'big' : 'little') + ' endian'
puts "Byte order: #{byte_order}"

With MRI, ri Fixnum states, "A Fixnum holds Integer values that can be represented in a native machine word (minus 1 bit)." This bases our claim that a Fixnum occupies one machine word.

Some other implementations of Ruby are different. With JRuby, a Fixnum is always 64 bits, because it is a Java long (1). JRuby uses the correct native byte order by calling java.nio.ByteOrder.nativeOrder() (2).

Rust

#[derive(Copy, Clone, Debug)]
enum Endianness {
    Big, Little,
}

impl Endianness {
    fn target() -> Self {
        #[cfg(target_endian = "big")]
        {
            Endianness::Big
        }
        #[cfg(not(target_endian = "big"))]
        {
            Endianness::Little
        }
    }
}

fn main() {
    println!("Word size: {} bytes", std::mem::size_of::<usize>());
    println!("Endianness: {:?}", Endianness::target());
}
Output:
Word size: 8 bytes
Endianness: Little

Scala

Library: Scala
import java.nio.ByteOrder

object ShowByteOrder extends App {
  println(ByteOrder.nativeOrder())
  println(s"Word size: ${System.getProperty("sun.arch.data.model")}")
  println(s"Endianness: ${System.getProperty("sun.cpu.endian")}")
}

Scheme

Works with: Chicken Scheme
(define host-info
  (begin
    (display "Endianness: ")
    (display (machine-byte-order))
    (newline)
    (display "Word Size: ")
    (display (if (fixnum? (expt 2 33)) 64 32))
    (newline)))
Output:
Endianness: little-endian
Word Size: 32

Seed7

The library cc_conf.s7i provides values that describe C compiler and runtime library. The example below assumes that the word size is the size of a pointer.

$ include "seed7_05.s7i";
  include "cc_conf.s7i";

const proc: main is func
  begin
    writeln("Word size: " <& ccConf.POINTER_SIZE);
    write("Endianness: ");
    if ccConf.LITTLE_ENDIAN_INTTYPE then
      writeln("Little endian");
    else
      writeln("Big endian");
    end if;
  end func;
Output:
Word size: 64
Endianness: Little endian

Slate

inform: 'Endianness: ' ; Platform current endianness.
inform: 'Word Size: ' ; (Platform current bytesPerWord * 8) printString.
Output:
Endianness: LittleEndian
Word Size: 32

Tcl

This is very straightforward in Tcl. The global array tcl_platform contains these values. In an interactive tclsh:

% parray tcl_platform
tcl_platform(byteOrder)   = littleEndian
tcl_platform(machine)     = intel
tcl_platform(os)          = Windows NT
tcl_platform(osVersion)   = 5.1
tcl_platform(platform)    = windows
tcl_platform(pointerSize) = 4
tcl_platform(threaded)    = 1
tcl_platform(user)        = glennj
tcl_platform(wordSize)    = 4

TI-89 BASIC

Disp "32-bit big-endian"

TXR

Interactive session:

Which word? Pointer size or size of int? Let's get both:

This is the TXR Lisp interactive listener of TXR 177.
Use the :quit command or type Ctrl-D on empty line to exit.
1> (sizeof (ptr char))
8
2> (sizeof int)
4

Endianness: what we can do is put the integer 1 into a buffer as a uint32, the 32 bit unsigned integer type in the local representation. We then retrieve it as a le-uint32: little-endian uint32:

3> (ffi-put 1 (ffi uint32))
#b'01000000'
4> (ffi-get *3 (ffi le-uint32))
1

The extracted value 1 matches, so the machine must be little endian. Here is a transcript from a big-endian PPC64 machine:

1> (ffi-put 1 (ffi uint32))
#b'00000001'
2> (ffi-get *1 (ffi le-uint32))
16777216

No match, so big endian.

UNIX Shell

The getconf command gets the word size, the piped command list gets the endianness , 1 means Little and 0 means Big :

Aamrun$ getconf WORD_BIT
32
Aamrun$ echo -n I | od -to2 | awk 'FNR==1{ print substr($2,6,1)}'
1
Aamrun$

Wren

Translation of: C

As this information cannot be reliably obtained via Wren CLI, we instead embed a Wren script in a C application and ask the host program to get it for us.

/* Host_introspection.wren */

class C {
    foreign static wordSize
    foreign static endianness
}

System.print("word size  = %(C.wordSize) bits")
System.print("endianness = %(C.endianness)")


We now embed this Wren script in the following C program, compile and run it.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include "wren.h"

void C_wordSize(WrenVM* vm) {
    /* size_t typically is exactly one word */
    int ws = (int)(CHAR_BIT * sizeof(size_t));

    /* return result to Wren */
    wrenSetSlotDouble(vm, 0, (double)ws);
}

void C_endianness(WrenVM* vm) {
    /* Check if the least significant bit is located in the lowest-address byte. */
    int one = 1;
    char *e = (*(char *)&one) ? "little" : "big";

    /* return result to Wren */
    wrenSetSlotString(vm, 0, e);
}

WrenForeignMethodFn bindForeignMethod(
    WrenVM* vm,
    const char* module,
    const char* className,
    bool isStatic,
    const char* signature) {
    if (strcmp(module, "main") == 0) {
        if (strcmp(className, "C") == 0) {
            if (isStatic && strcmp(signature, "wordSize") == 0) {
                return C_wordSize;
            } else if (isStatic && strcmp(signature, "endianness") == 0) {
                return C_endianness;
            }
        }
    }
    return NULL;
}

static void writeFn(WrenVM* vm, const char* text) {
    printf("%s", text);
}

void errorFn(WrenVM* vm, WrenErrorType errorType, const char* module, const int line, const char* msg) {
    switch (errorType) {
        case WREN_ERROR_COMPILE:
            printf("[%s line %d] [Error] %s\n", module, line, msg);
            break;
        case WREN_ERROR_STACK_TRACE:
            printf("[%s line %d] in %s\n", module, line, msg);
            break;
        case WREN_ERROR_RUNTIME:
            printf("[Runtime Error] %s\n", msg);
            break;
    }
}

char *readFile(const char *fileName) {
    FILE *f = fopen(fileName, "r");
    fseek(f, 0, SEEK_END);
    long fsize = ftell(f);
    rewind(f);
    char *script = malloc(fsize + 1);
    fread(script, 1, fsize, f);
    fclose(f);
    script[fsize] = 0;
    return script;
}

int main() {
    WrenConfiguration config;
    wrenInitConfiguration(&config);
    config.writeFn = &writeFn;
    config.errorFn = &errorFn;
    config.bindForeignMethodFn = &bindForeignMethod;
    WrenVM* vm = wrenNewVM(&config);
    const char* module = "main";
    const char* fileName = "Host_introspection.wren";
    char *script = readFile(fileName);
    WrenInterpretResult result = wrenInterpret(vm, module, script);
    switch (result) {
        case WREN_RESULT_COMPILE_ERROR:
            printf("Compile Error!\n");
            break;
        case WREN_RESULT_RUNTIME_ERROR:
            printf("Runtime Error!\n");
            break;
        case WREN_RESULT_SUCCESS:
            break;
    }
    wrenFreeVM(vm);
    free(script);
    return 0;
}
Output:

The results, as expected, for my x64 Ubuntu 20.04 system are:

word size  = 64 bits
endianness = little

XPL0

This is the result when running the 32-bit version of the language on Intel 386 (and later) processors. Other versions give 2 bytes per word, and the Motorola 68000 version would give 4 bytes per word and Big endian.

include c:\cxpl\codes;          \intrinsic 'code' declarations
int  A, B;
char C;
[IntOut(0, @B-@A);  CrLf(0);    \word size = integer size
A:= $1234;
C:= @A;
Text(0, if C(0)=$34 then "Little" else "Big");
Text(0, " endian
");
]
Output:
4
Little endian

Z80 Assembly

The Z80's word size is 16-bit, and you'd know this ahead of time simply because there aren't any commands that work with values any larger than that. It's also little-endian, but this can be proven without knowing it in advance using a simple store and load test.

EndianTest:
ld hl,&8000
ld (&C000),hl     ;store &8000 into memory.
ld a,(&C000)      ;loads the byte at &C000 into A. If the Z80 were big-endian, A would equal &80. But it equals zero.
or a              ;still, we need to pretend we don't already know the result and compare A to zero.
jr z,LittleEndian ;handle the case where Z80 is little-endian (which it is, so this branch is always taken.)

;else, do whatever you would do to show that the Z80 is big-endian (it isn't, so execution never reaches here.)