Literals/Integer: Difference between revisions
m Put in lang learning cat |
Forth, BASE and prefixes |
||
Line 65: | Line 65: | ||
T |
T |
||
</pre> |
</pre> |
||
=={{header|Forth}}== |
|||
The standard method for entering numbers of a particular base is to set the user variable BASE to the desired radix from 2 to 36. There are also convenience words for setting the base to DECIMAL and HEX. |
|||
<lang forth> |
|||
HEX |
|||
FEEDFACE |
|||
2 BASE ! |
|||
1011001 |
|||
DECIMAL |
|||
1234 |
|||
: mask var @ [ base @ hex ] 3fff and [ base ! ] var ! ; |
|||
</lang> |
|||
The Forth numeric parser will look for symbols embedded within the stream of digits to determine whether to interpret it as a single cell, double cell, or floating point literal ('e'). |
|||
<lang forth> |
|||
1234 ( n ) |
|||
123.4 ( l h ) |
|||
123e4 ( F: n ) |
|||
</lang> |
|||
===Base prefixes=== |
|||
{{works with|GNU Forth}} |
|||
In addition, many Forths have extensions for using a prefix to temporarily override BASE when entering an integer literal. These are the prefixes supported by GNU Forth. |
|||
<lang forth> |
|||
$feedface \ hexadecimal |
|||
&1234 \ decimal |
|||
%1001101 \ binary |
|||
'a \ base 256 (ASCII literal) |
|||
</lang> |
|||
Some Forths also support "0xABCD" hex literals for compatibility with C-like languages. |
|||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
Revision as of 17:03, 2 February 2009
You are encouraged to solve this task according to the task description, using any language you may know.
Some programming languages have ways of expressing integer literals in bases other than the normal base ten.
Show how integer literals can be expressed in as many bases as your language allows.
Note: this should not involve the calling of any functions/methods but should be interpreted by the compiler or interpreter as an integer written to a given base.
Ada
In Ada integer literals may have the form <base>#<numeral>#. Here <base> can be from the range 2..16. For example: <lang ada> with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
procedure Test_Literals is begin
Put (16#2D7#); Put (10#727#); Put (8#1_327#); Put (2#10_1101_0111#);
end Test_Literals; </lang> Sample output:
727 727 727 727
AWK
<lang>BEGIN {
if ( (0x2d7 == 727) && (01327 == 727) ) { print "true" }
}</lang>
C
Leading 0 means octal, 0x or 0X means hexadecimal. Otherwise, it is just decimal.
<lang c>#include <stdio.h>
int main() {
printf("%s\n", ( (727 == 0x2d7) && (727 == 01327) ) ? "true" : "false");
return 0;
}</lang>
C has no way of specifying integers in binary (if there's something like 0b..., it is not standard)
Common Lisp
(This is an interactive common lisp session)
binary: #b, octal: #o, hexadecimal: #x, any base from 2 to 36: #Nr
>(= 727 #b1011010111) T >(= 727 #o1327) T >(= 727 #x2d7) T >(= 727 #20r1g7) T
Forth
The standard method for entering numbers of a particular base is to set the user variable BASE to the desired radix from 2 to 36. There are also convenience words for setting the base to DECIMAL and HEX. <lang forth>
HEX FEEDFACE 2 BASE ! 1011001 DECIMAL 1234 : mask var @ [ base @ hex ] 3fff and [ base ! ] var ! ;
</lang> The Forth numeric parser will look for symbols embedded within the stream of digits to determine whether to interpret it as a single cell, double cell, or floating point literal ('e'). <lang forth>
1234 ( n ) 123.4 ( l h ) 123e4 ( F: n )
</lang>
Base prefixes
In addition, many Forths have extensions for using a prefix to temporarily override BASE when entering an integer literal. These are the prefixes supported by GNU Forth. <lang forth>
$feedface \ hexadecimal &1234 \ decimal %1001101 \ binary 'a \ base 256 (ASCII literal)
</lang> Some Forths also support "0xABCD" hex literals for compatibility with C-like languages.
Fortran
<lang fortran>program IntegerLiteral
implicit none integer, parameter :: dec = 727 integer, parameter :: hex = Z'2d7' integer, parameter :: oct = O'1327' integer, parameter :: bin = B'1011010111'
print *, dec, hex, oct, bin
end program IntegerLiteral</lang>
Outputs:
727 727 727 727
Haskell
(This is an interactive ghci session)
Oct(leading 0o or 0O), Hex(leading 0x or 0X) <lang haskell> Prelude> 727 == 0o1327 True Prelude> 727 == 0x2d7 True </lang>
Java
Leading 0 means octal, 0x or 0X means hexadecimal. Otherwise, it is just decimal.
<lang java5> public class IntegerLiterals {
public static void main(String[] args) { System.out.println( 727 == 0x2d7 && 727 == 01327 ); }
}</lang>
Java has no way of specifying integers in binary.
You may also specify a long literal by adding an l or L to the end. Ex: long a = 727L.
JavaScript
<lang javascript> if ( 727 == 0x2d7 &&
727 == 01327 ) window.alert("true");
</lang>
OCaml
(This is an interactive ocaml session)
Bin(leading 0b or 0B), Oct(leading 0o or 0O), Hex(leading 0x or 0X) <lang ocaml>
- 727 = 0b1011010111;;
- : bool = true
- 727 = 0o1327;;
- : bool = true
- 727 = 0x2d7;;
- : bool = true </lang>
Perl
<lang perl> print "true\n" if ( 727 == 0x2d7 &&
727 == 01327 && 727 == 0b1011010111 );
</lang>
PHP
<lang php><?php if ( 727 == 0x2d7 &&
727 == 01327 ) echo "true\n";
?></lang>
Python
Python 3.0 brought in the binary literal and uses 0o or 0O exclusively for octal. <lang python>>>> # Bin(leading 0b or 0B), Oct(leading 0o or 0O), Dec, Hex(leading 0x or 0X), in order: >>> 0b1011010111 == 0o1327 == 727 == 0x2d7 True >>> </lang>
Python 2.6 has the binary and new octal formats of 3.0, as well as keeping the earlier leading 0 octal format of previous 2.X versions for compatability. <lang python>>>> # Bin(leading 0b or 0B), Oct(leading 0o or 0O, or just 0), Dec, Hex(leading 0x or 0X), in order: >>> 0b1011010111 == 0o1327 == 01327 == 727 == 0x2d7 True >>> </lang>
<lang python>>>> # Oct(leading 0), Dec, Hex(leading 0x or 0X), in order: >>> 01327 == 727 == 0x2d7 True >>> </lang>
Ruby
(This is an interactive ruby session)
irb(main):001:0> 727 == 0b1011010111 => true irb(main):002:0> 727 == 0x2d7 => true irb(main):003:0> 727 == 01327 => true
Scheme
(This is an interactive scheme session)
binary: #b, octal: #o, decimal: #d (optional obviously), hex: #x
> (= 727 #b1011010111) #t > (= 727 #o1327) #t > (= 727 #d727) #t > (= 727 #x2d7) #t
UNIX Shell
As manual states, 0x or 0X is the prefix for hexadecimal numbers, while 0 is the one for octal, and nothing means the number is decimal. But the sintax BASE#NUMBER can be used, with BASE going from 2 to 64, and the symbols used are digits, lowercase letters, uppercase letters, @ and _ in that order; if the BASE is less than or equal to 36, lowercase and uppercase letters can be used interchangeably to represent number from 10 and 35. (From the info manual of the Bash). This syntax works only in some circumstances, i.e. in the shell expansion (e.g. inside $(( ))) or using let.
dec=727 oct=$(( 01327 )) bin=$(( 2#1011010111 )) hex=$(( 0x2d7 )) # or e.g. let bin=2#1011010111 let "baseXX = 20#1g7"