Host introspection: Difference between revisions
(Added Java, I think I got the right property for word size...) |
m (→{{header|Java}}: Shorter version) |
||
Line 68: | Line 68: | ||
little |
little |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
<java>System.out.println("word size: "+System. |
<java>System.out.println("word size: "+System.getProperty("sun.arch.data.model")); |
||
System.out.println("endianness: "+System. |
System.out.println("endianness: "+System.getProperty("sun.cpu.endian"));</java> |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
<ocaml>Printf.printf "%d\n" Sys.word_size; (* Print word size *) |
<ocaml>Printf.printf "%d\n" Sys.word_size; (* Print word size *) |
Revision as of 18:04, 13 October 2008
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
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
<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; </ada> Sample output on a Pentium machine:
Word size 32 Endianness LOW_ORDER_FIRST
C
<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;
}</c>
On POSIX-compatible systems, the following also tests the endianness (this makes use of the fact that network order is big endian): <c>
- include <stdio.h>
- include <arpa/inet.h>
int main() {
if (htonl(1) == 1) printf("big endian\n"); else printf("little endian\n");
} </c>
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@.
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
<java>System.out.println("word size: "+System.getProperty("sun.arch.data.model")); System.out.println("endianness: "+System.getProperty("sun.cpu.endian"));</java>
OCaml
<ocaml>Printf.printf "%d\n" Sys.word_size; (* Print word size *) Printf.printf "%s\n" Sys.os_type; (* Print operating system *)</ocaml> Dunno about endianness
Python
<python>>>> import sys, math >>> int(round(math.log(sys.maxint,2)+1)) 32 >>> sys.byteorder little >>> import socket >>> socket.gethostname() 'PADDY3118-RESTING' >>> </python>