Host introspection: Difference between revisions
No edit summary |
(Undo revision 21528 by GugaTagFixer (Talk)) |
||
Line 1: | Line 1: | ||
{{task}} |
|||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
|||
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr"> |
|||
<head> |
|||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
|||
<meta name="generator" content="MediaWiki 1.13.3" /> |
|||
<meta name="robots" content="noindex,nofollow" /> |
|||
<meta name="keywords" content="Host" /> |
|||
<link rel="alternate" type="application/rss+xml" title="Rosetta Code: New tasks - RSS 2.0" href="/w/index.php?title=Special:NewsChannel&format=rss20" /> |
|||
<link rel="alternate" type="application/atom+xml" title="Rosetta Code: New tasks - Atom 1.0" href="/w/index.php?title=Special:NewsChannel&format=atom10" /> |
|||
<link rel="shortcut icon" href="/favicon.ico" /> |
|||
<link rel="search" type="application/opensearchdescription+xml" href="/w/opensearch_desc.php" title="Rosetta Code (en)" /> |
|||
<link title="Creative Commons" type="application/rdf+xml" href="/w/index.php?title=Host&action=creativecommons" rel="meta" /> |
|||
<link rel="copyright" href="http://www.gnu.org/copyleft/fdl.html" /> |
|||
<link rel="alternate" type="application/rss+xml" title="Rosetta Code RSS Feed" href="http://www.rosettacode.org/w/index.php?title=Special:RecentChanges&feed=rss" /> |
|||
<link rel="alternate" type="application/atom+xml" title="Rosetta Code Atom Feed" href="http://www.rosettacode.org/w/index.php?title=Special:RecentChanges&feed=atom" /> |
|||
<title>Host - Rosetta Code</title> |
|||
<style type="text/css" media="screen, projection">/*<![CDATA[*/ |
|||
@import "/w/skins/common/shared.css?164"; |
|||
@import "/w/skins/monobook/main.css?164"; |
|||
/*]]>*/</style> |
|||
<link rel="stylesheet" type="text/css" media="print" href="/w/skins/common/commonPrint.css?164" /> |
|||
<!--[if lt IE 5.5000]><style type="text/css">@import "/w/skins/monobook/IE50Fixes.css?164";</style><![endif]--> |
|||
<!--[if IE 5.5000]><style type="text/css">@import "/w/skins/monobook/IE55Fixes.css?164";</style><![endif]--> |
|||
<!--[if IE 6]><style type="text/css">@import "/w/skins/monobook/IE60Fixes.css?164";</style><![endif]--> |
|||
<!--[if IE 7]><style type="text/css">@import "/w/skins/monobook/IE70Fixes.css?164";</style><![endif]--> |
|||
<!--[if lt IE 7]><script type="text/javascript" src="/w/skins/common/IEFixes.js?164"></script> |
|||
<meta http-equiv="imagetoolbar" content="no" /><![endif]--> |
|||
<script type= "text/javascript">/*<![CDATA[*/ |
|||
var skin = "monobook"; |
|||
var stylepath = "/w/skins"; |
|||
var wgArticlePath = "/wiki/$1"; |
|||
var wgScriptPath = "/w"; |
|||
var wgScript = "/w/index.php"; |
|||
var wgVariantArticlePath = false; |
|||
var wgActionPaths = []; |
|||
var wgServer = "http://www.rosettacode.org"; |
|||
var wgCanonicalNamespace = ""; |
|||
var wgCanonicalSpecialPageName = false; |
|||
var wgNamespaceNumber = 0; |
|||
var wgPageName = "Host"; |
|||
var wgTitle = "Host"; |
|||
var wgAction = "view"; |
|||
var wgArticleId = 0; |
|||
var wgIsArticle = true; |
|||
var wgUserName = null; |
|||
var wgUserGroups = null; |
|||
var wgUserLanguage = "en"; |
|||
var wgContentLanguage = "en"; |
|||
var wgBreakFrames = false; |
|||
var wgCurRevisionId = false; |
|||
var wgVersion = "1.13.3"; |
|||
var wgEnableAPI = true; |
|||
var wgEnableWriteAPI = true; |
|||
var wgRestrictionEdit = []; |
|||
var wgRestrictionMove = []; |
|||
/*]]>*/</script> |
|||
<script type="text/javascript" src="/w/skins/common/wikibits.js?164"><!-- wikibits js --></script> |
|||
<script type="text/javascript" src="/w/index.php?title=-&action=raw&gen=js&useskin=monobook"><!-- site js --></script> |
|||
<style type="text/css">/*<![CDATA[*/ |
|||
@import "/w/index.php?title=MediaWiki:Common.css&usemsgcache=yes&action=raw&ctype=text/css&smaxage=18000"; |
|||
@import "/w/index.php?title=MediaWiki:Monobook.css&usemsgcache=yes&action=raw&ctype=text/css&smaxage=18000"; |
|||
@import "/w/index.php?title=-&action=raw&gen=css&maxage=18000&useskin=monobook"; |
|||
/*]]>*/</style> |
|||
<!-- Head Scripts --> |
|||
<script type="text/javascript" src="/w/skins/common/ajax.js?164"></script> |
|||
</head> |
|||
<body class="mediawiki ns-0 ltr page-Host"> |
|||
<div id="globalWrapper"> |
|||
<div id="column-content"> |
|||
<div id="content"> |
|||
<a name="top" id="top"></a> |
|||
<h1 class="firstHeading">Host</h1> |
|||
<div id="bodyContent"> |
|||
<h3 id="siteSub">From Rosetta Code</h3> |
|||
<div id="contentSub"></div> |
|||
<div id="jump-to-nav">Jump to: <a href="#column-one">navigation</a>, <a href="#searchInput">search</a></div> <!-- start content --> |
|||
<div class="noarticletext"> |
|||
<p>There is currently no text in this page, you can <a href="/wiki/Special:Search/Host" title="Special:Search/Host">search for this page title</a> in other pages or <a href="http://www.rosettacode.org/w/index.php?title=Host&action=edit" class="external text" title="http://www.rosettacode.org/w/index.php?title=Host&action=edit" rel="nofollow">edit this page</a>. |
|||
</p> |
|||
</div> |
|||
Print the [[wp:Word_size#Word_size_choice|word size]] and [[wp:Endianness|endianness]] of the host machine. |
|||
<!-- |
|||
NewPP limit report |
|||
Preprocessor node count: 6/1000000 |
|||
Post-expand include size: 69/2097152 bytes |
|||
Template argument size: 0/2097152 bytes |
|||
Expensive parser function count: 0/100 |
|||
--> |
|||
<div class="printfooter"> |
|||
Retrieved from "<a href="http://www.rosettacode.org/wiki/Host">http://www.rosettacode.org/wiki/Host</a>"</div> |
|||
<!-- end content --> |
|||
<div class="visualClear"></div> |
|||
</div> |
|||
</div> |
|||
</div> |
|||
<div id="column-one"> |
|||
<div id="p-cactions" class="portlet"> |
|||
<h5>Views</h5> |
|||
<div class="pBody"> |
|||
<ul> |
|||
<li id="ca-nstab-main" class="selected new"><a href="/w/index.php?title=Host&action=edit" title="View the content page [c]" accesskey="c">Page</a></li> |
|||
<li id="ca-talk" class="new"><a href="/w/index.php?title=Talk:Host&action=edit" title="Discussion about the content page [t]" accesskey="t">Discussion</a></li> |
|||
<li id="ca-edit"><a href="/w/index.php?title=Host&action=edit" title="You can edit this page. Please use the preview button before saving. [e]" accesskey="e">Create</a></li> |
|||
</ul> |
|||
</div> |
|||
</div> |
|||
<div class="portlet" id="p-personal"> |
|||
<h5>Personal tools</h5> |
|||
<div class="pBody"> |
|||
<ul> |
|||
<li id="pt-login"><a href="/w/index.php?title=Special:UserLogin&returnto=Host" title="You are encouraged to log in, it is not mandatory however. [o]" accesskey="o">Log in / create account</a></li> |
|||
</ul> |
|||
</div> |
|||
</div> |
|||
<div class="portlet" id="p-logo"> |
|||
<a style="background-image: url(/w/title.png);" href="/wiki/Main_Page" title="Visit the Main Page [z]" accesskey="z"></a> |
|||
</div> |
|||
<script type="text/javascript"> if (window.isMSIE55) fixalpha(); </script> |
|||
<div class='portlet' id='p-Browse'> |
|||
<h5>Browse</h5> |
|||
<div class='pBody'> |
|||
<ul> |
|||
<li id="n-mainpage"><a href="/wiki/Main_Page" title="Visit the Main Page [z]" accesskey="z">Main Page</a></li> |
|||
<li id="n-Solutions-by-Language"><a href="/wiki/Category:Solutions_by_Programming_Language">Solutions by Language</a></li> |
|||
<li id="n-Solutions-by-Task"><a href="/wiki/Category:Solutions_by_Programming_Task">Solutions by Task</a></li> |
|||
<li id="n-Encyclopedia"><a href="/wiki/Category:Encyclopedia">Encyclopedia</a></li> |
|||
<li id="n-recentchanges"><a href="/wiki/Special:RecentChanges" title="The list of recent changes in the wiki. [r]" accesskey="r">Recent changes</a></li> |
|||
<li id="n-randompage"><a href="/wiki/Special:Random" title="Load a random page [x]" accesskey="x">Random page</a></li> |
|||
</ul> |
|||
</div> |
|||
</div> |
|||
<div class='portlet' id='p-Contribute'> |
|||
<h5>Contribute</h5> |
|||
<div class='pBody'> |
|||
<ul> |
|||
<li id="n-Contribute-Content"><a href="/wiki/Help:Contribute_Content">Contribute Content</a></li> |
|||
<li id="n-Pages-needing-attention"><a href="/wiki/Category:Maintenance">Pages needing attention</a></li> |
|||
<li id="n-Request-a-task"><a href="/wiki/Help:Request_a_new_programming_task">Request a task</a></li> |
|||
<li id="n-Request-a-language"><a href="/wiki/Help:Request_a_language_focus">Request a language</a></li> |
|||
</ul> |
|||
</div> |
|||
</div> |
|||
<div class='portlet' id='p-Community'> |
|||
<h5>Community</h5> |
|||
<div class='pBody'> |
|||
<ul> |
|||
<li id="n-Village-Pump"><a href="/wiki/Village_Pump:Home">Village Pump</a></li> |
|||
<li id="n-Blog"><a href="http://blog.rosettacode.org">Blog</a></li> |
|||
<li id="n-Similar-sites"><a href="/wiki/Help:Similar_Sites">Similar sites</a></li> |
|||
<li id="n-Internet-Relay-Chat"><a href="/wiki/Help:IRC">Internet Relay Chat</a></li> |
|||
</ul> |
|||
</div> |
|||
</div> |
|||
<div id="p-search" class="portlet"> |
|||
<h5><label for="searchInput">Search</label></h5> |
|||
<div id="searchBody" class="pBody"> |
|||
<form action="/wiki/Special:Search" id="searchform"><div> |
|||
<input id="searchInput" name="search" type="text" title="Search Rosetta Code [f]" accesskey="f" value="" /> |
|||
<input type='submit' name="go" class="searchButton" id="searchGoButton" value="Go" /> |
|||
<input type='submit' name="fulltext" class="searchButton" id="mw-searchButton" value="Search" /> |
|||
</div></form> |
|||
</div> |
|||
</div> |
|||
<div class="portlet" id="p-tb"> |
|||
<h5>Toolbox</h5> |
|||
<div class="pBody"> |
|||
<ul> |
|||
<li id="t-whatlinkshere"><a href="/wiki/Special:WhatLinksHere/Host" title="List of all wiki pages that link here [j]" accesskey="j">What links here</a></li> |
|||
<li id="t-upload"><a href="/wiki/Special:Upload" title="Upload files [u]" accesskey="u">Upload file</a></li> |
|||
<li id="t-specialpages"><a href="/wiki/Special:SpecialPages" title="List of all special pages [q]" accesskey="q">Special pages</a></li> |
|||
<li id="t-print"><a href="/w/index.php?title=Host&printable=yes" title="Printable version of this page [p]" accesskey="p">Printable version</a></li> </ul> |
|||
</div> |
|||
</div> |
|||
</div><!-- end of the left (by default at least) column --> |
|||
<div class="visualClear"></div> |
|||
<div id="footer"> |
|||
<div id="f-poweredbyico"><a href="http://www.mediawiki.org/"><img src="/w/skins/common/images/poweredby_mediawiki_88x31.png" alt="Powered by MediaWiki" /></a></div> |
|||
<div id="f-copyrightico"><a href="http://www.gnu.org/copyleft/fdl.html"><img src="/w/skins/common/images/gnu-fdl.png" alt='GNU Free Documentation License 1.2' /></a></div> |
|||
<ul id="f-list"> |
|||
<li id="privacy"><a href="/wiki/Rosetta_Code:Privacy_policy" title="Rosetta Code:Privacy policy">Privacy policy</a></li> |
|||
<li id="about"><a href="/wiki/Rosetta_Code:About" title="Rosetta Code:About">About Rosetta Code</a></li> |
|||
<li id="disclaimer"><a href="/wiki/Rosetta_Code:General_disclaimer" title="Rosetta Code:General disclaimer">Disclaimers</a></li> |
|||
</ul> |
|||
</div> |
|||
<script type="text/javascript">if (window.runOnloadHook) runOnloadHook();</script> |
|||
=={{header|Ada}}== |
|||
<!-- Set $wgGoogleAnalyticsAccount to your account # provided by Google Analytics. --></div> |
|||
<lang ada> |
|||
<!-- Served in 0.324 secs. --><!--http://www.rosettacode.org--> |
|||
with Ada.Text_IO; use Ada.Text_IO; |
|||
</body></html> |
|||
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; |
|||
</lang> |
|||
Sample output on a Pentium machine: |
|||
<pre> |
|||
Word size 32 |
|||
Endianness LOW_ORDER_FIRST |
|||
</pre> |
|||
=={{header|ALGOL 68}}== |
|||
<pre> |
|||
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)) |
|||
</pre> |
|||
Output (Intel i686): |
|||
<pre> |
|||
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 |
|||
</pre> |
|||
On older CPUs the results would vary: |
|||
{|border="1" align="center" |
|||
|| ALGOL 68R |
|||
|| ALGOL 68RS |
|||
|- |
|||
|| ~ <pre> |
|||
bits per char: 6 |
|||
bits per int: 24 |
|||
chars per int: 4</pre> |
|||
|| ICL 2900 <pre> |
|||
bits per char: 8 |
|||
bits per int: 32 |
|||
chars per int: 4</pre> |
|||
|| Multics <pre> |
|||
bits per char: 6 |
|||
bits per int: 36 |
|||
chars per int: 6</pre> |
|||
|} |
|||
=={{header|C}}== |
|||
<lang c>#include <stdio.h> |
|||
#include <stddef.h> /* for size_t */ |
|||
#include <limits.h> /* for CHAR_BIT */ |
|||
int main() { |
|||
int one = 1; |
|||
printf("word size = %d\n", CHAR_BIT * sizeof(size_t)); /* best bet: size_t typically is exactly one word */ |
|||
if (*(char *)&one) /* if the least significant bit is located in the lowest-address byte */ |
|||
printf("little endian\n"); |
|||
else |
|||
printf("big endian\n"); |
|||
return 0; |
|||
}</lang> |
|||
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> |
|||
#include <arpa/inet.h> |
|||
int main() |
|||
{ |
|||
if (htonl(1) == 1) |
|||
printf("big endian\n"); |
|||
else |
|||
printf("little endian\n"); |
|||
} |
|||
</lang> |
|||
=={{header|D}}== |
|||
<lang d> |
|||
import std.stdio, std.system; |
|||
void main() { |
|||
writefln("word size = ", size_t.sizeof * 8); |
|||
writefln(endian == Endian.LittleEndian ? "little" : "big", " endian"); |
|||
} |
|||
</lang> |
|||
=={{header|Factor}}== |
|||
USING: alien.c-types io layouts ; |
|||
"Word size: " write cell 8 * . |
|||
"Endianness: " write little-endian? "little" "big" ? print |
|||
=={{header|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@. |
|||
=={{header|Fortran}}== |
|||
{{works with|Fortran|90 and later}} |
|||
INTEGER, PARAMETER :: i8 = SELECTED_INT_KIND(2) |
|||
INTEGER, PARAMETER :: i16 = SELECTED_INT_KIND(4) |
|||
INTEGER(i8) :: a(2) |
|||
INTEGER(i16) :: b |
|||
WRITE(*,*) bit_size(1) ! number of bits in the default integer type |
|||
! which may (or may not!) equal the word size |
|||
b = Z'1234' ! Hexadecimal assignment |
|||
a = (TRANSFER(b, a)) ! Split a 16 bit number into two 8 bit numbers |
|||
IF (a(1) == Z'12') THEN ! where did the most significant 8 bits end up |
|||
WRITE(*,*) "Big Endian" |
|||
ELSE |
|||
WRITE(*,*) "Little Endian" |
|||
END IF |
|||
=={{header|Haskell}}== |
|||
<pre> |
|||
import Data.Bits |
|||
main = print $ bitSize (undefined :: Int) -- print word size |
|||
</pre> |
|||
Dunno about endianness |
|||
=={{header|J}}== |
|||
'''Method A''': |
|||
":&> (|: 32 64 ;"0 big`little) {"_1~ 2 2 #: 16b_e0 + a. i. 0 { 3!:1 <nowiki>''</nowiki> |
|||
32 |
|||
little |
|||
'''Method B''': |
|||
((4*#) ,:&": little`big {::~ '7'={.) {: 3!:3 ] 33 b.~_1 |
|||
32 |
|||
little |
|||
=={{header|Java}}== |
|||
<lang java>System.out.println("word size: "+System.getProperty("sun.arch.data.model")); |
|||
System.out.println("endianness: "+System.getProperty("sun.cpu.endian"));</lang> |
|||
=={{header|Modula-3}}== |
|||
<pre> |
|||
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. |
|||
</pre> |
|||
Output (on an x86): |
|||
<pre> |
|||
Word Size: 32 |
|||
Endianness: Little |
|||
</pre> |
|||
=={{header|OCaml}}== |
|||
<lang ocaml>Printf.printf "%d\n" Sys.word_size; (* Print word size *) |
|||
Printf.printf "%s\n" Sys.os_type; (* Print operating system *)</lang> |
|||
Dunno about endianness |
|||
=={{header|Python}}== |
|||
<lang python>>>> import sys, math |
|||
>>> int(round(math.log(sys.maxint,2)+1)) # this only works in Python 2.x |
|||
32 |
|||
>>> import struct |
|||
>>> struct.calcsize('i') * 8 |
|||
32 |
|||
>>> sys.byteorder |
|||
little |
|||
>>> import socket |
|||
>>> socket.gethostname() |
|||
'PADDY3118-RESTING' |
|||
>>> </lang> |
|||
=={{header|Ruby}}== |
|||
irb(main):001:0> 42.size * 8 |
|||
=> 32 |
|||
irb(main):002:0> if [1].pack('i')[0] != 0 |
|||
irb(main):003:1> 'little endian' |
|||
irb(main):004:1> else |
|||
irb(main):005:1* 'big endian' |
|||
irb(main):006:1> end |
|||
=> "little endian" |
Revision as of 15:16, 3 February 2009
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.
Ada
<lang 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; </lang> Sample output on a Pentium machine:
Word size 32 Endianness LOW_ORDER_FIRST
ALGOL 68
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 |
C
<lang c>#include <stdio.h>
- include <stddef.h> /* for size_t */
- include <limits.h> /* for CHAR_BIT */
int main() {
int one = 1; printf("word size = %d\n", CHAR_BIT * sizeof(size_t)); /* best bet: size_t typically is exactly one word */ if (*(char *)&one) /* if the least significant bit is located in the lowest-address byte */ printf("little endian\n"); else printf("big endian\n"); return 0;
}</lang>
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>
- include <arpa/inet.h>
int main() {
if (htonl(1) == 1) printf("big endian\n"); else printf("little endian\n");
} </lang>
D
<lang d> import std.stdio, std.system;
void main() {
writefln("word size = ", size_t.sizeof * 8); writefln(endian == Endian.LittleEndian ? "little" : "big", " endian");
} </lang>
Factor
USING: alien.c-types 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
INTEGER, PARAMETER :: i8 = SELECTED_INT_KIND(2) INTEGER, PARAMETER :: i16 = SELECTED_INT_KIND(4) INTEGER(i8) :: a(2) INTEGER(i16) :: b WRITE(*,*) bit_size(1) ! number of bits in the default integer type ! which may (or may not!) equal the word size b = Z'1234' ! Hexadecimal assignment a = (TRANSFER(b, a)) ! Split a 16 bit number into two 8 bit numbers IF (a(1) == Z'12') THEN ! where did the most significant 8 bits end up WRITE(*,*) "Big Endian" ELSE WRITE(*,*) "Little Endian" END IF
Haskell
import Data.Bits main = print $ bitSize (undefined :: Int) -- print word size
Dunno about endianness
J
Method A:
":&> (|: 32 64 ;"0 big`little) {"_1~ 2 2 #: 16b_e0 + a. i. 0 { 3!:1 '' 32 little
Method B:
((4*#) ,:&": little`big {::~ '7'={.) {: 3!:3 ] 33 b.~_1 32 little
Java
<lang java>System.out.println("word size: "+System.getProperty("sun.arch.data.model")); System.out.println("endianness: "+System.getProperty("sun.cpu.endian"));</lang>
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
OCaml
<lang ocaml>Printf.printf "%d\n" Sys.word_size; (* Print word size *) Printf.printf "%s\n" Sys.os_type; (* Print operating system *)</lang> Dunno about endianness
Python
<lang python>>>> import sys, math >>> int(round(math.log(sys.maxint,2)+1)) # this only works in Python 2.x 32 >>> import struct >>> struct.calcsize('i') * 8 32 >>> sys.byteorder little >>> import socket >>> socket.gethostname() 'PADDY3118-RESTING' >>> </lang>
Ruby
irb(main):001:0> 42.size * 8 => 32 irb(main):002:0> if [1].pack('i')[0] != 0 irb(main):003:1> 'little endian' irb(main):004:1> else irb(main):005:1* 'big endian' irb(main):006:1> end => "little endian"