Literals/Integer
You are encouraged to solve this task according to the task description, using any language you may know.
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.
Also show any other ways of expressing literals, e.g. for different types of integers.
See also Literals/Floating point.
[edit] Ada
In Ada integer literals may have the form <base>#<numeral>#. Here <base> can be from the range 2..16. For example:
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;
Sample output:
727 727 727 727
[edit] Aime
if ((727 == 0x2d7) && (727 == 01327)) {
o_text("true\n");
} else {
o_text("false\n");
}
[edit] ALGOL 68
Binary literals are of type BITS, and need to be converted to INT using the operator ABS.
main:(
SHORT SHORT INT ssdec = SHORT SHORT 727,
sshex = ABS SHORT SHORT 16r2d7,
ssoct = ABS SHORT SHORT 8r1327,
ssbin = ABS SHORT SHORT 2r1011010111;
SHORT INT sdec = SHORT 727,
shex = ABS SHORT 16r2d7,
soct = ABS SHORT 8r1327,
sbin = ABS SHORT 2r1011010111;
INT dec = 727,
hex = ABS 16r2d7,
oct = ABS 8r1327,
bin = ABS 2r1011010111;
LONG INT ldec = LONG 727,
lhex = ABS LONG 16r2d7,
loct = ABS LONG 8r1327,
lbin = ABS LONG 2r1011010111;
CO
LONG LONG INT lldec = LONG LONG 727,
llhex = ABS LONG LONG 16r2d7,
lloct = ABS LONG LONG 8r1327,
llbin = ABS LONG LONG 2r1011010111
# etc ... #
END CO
print(("SHORT SHORT INT:", ssdec, sshex, ssoct, ssbin, new line));
print((" SHORT INT:", sdec, shex, soct, sbin, new line));
print((" INT:", dec, hex, oct, bin, new line));
print((" LONG INT:", ldec, lhex, loct, lbin, new line))
CO LONG LONG INT not supported by ELLA ALGOL 68RS
print(("LONG LONG INT:", new line, lldec, new line, llhex, new line, lloct, new line, llbin, new line))
# etc ... #
END CO
)
algol68g output:
SHORT SHORT INT: +727 +727 +727 +727
SHORT INT: +727 +727 +727 +727
INT: +727 +727 +727 +727
LONG INT: +727 +727 +727 +727
algol68toc output:
SHORT SHORT INT: -41 -41 -41 -41
SHORT INT: +727 +727 +727 +727
INT: +727 +727 +727 +727
LONG INT: +727 +727 +727 +727
[edit] AmigaE
PROC main()
IF ($2d7 = 727) AND (%001011010111 = 727) THEN WriteF('true\n')
ENDPROC
[edit] AutoHotkey
If (727 == 0x2d7)
MsgBox true
[edit] AWK
Awk has decimal literals, using the digits from 0 to 9. Literals/Floating point#AWK describes the format of these literals.
As an extension to the language, some Awk implementations also have octal or hexadecimal literals. GNU awk (gawk) has both octal and hexadecimal literals, like C. The One True Awk (nawk) only has decimal literals.
BEGIN {
if ( (0x2d7 == 727) &&
(01327 == 727) ) {
print "true with GNU awk"
}
}
nawk parses 01327 as 1327, and parses 0x2d7 as 0 x2d7 (which is the string concatentation of "0" and variable x2d7).
BEGIN {
x2d7 = "Goodbye, world!"
print 0x2d7 # gawk prints "727", nawk prints "0Goodbye, world!"
print 01327 # gawk prints "727", nawk prints "1327"
}
[edit] BASIC
&O = octal; &H = hexadecimal. Some flavors of BASIC also support &B = binary, but they're somewhat rare.
PRINT 17
PRINT &O21
PRINT &H11
Output:
17 17 17
[edit] BBC BASIC
PRINT 1234 : REM Decimal
PRINT &4D2 : REM Hexadecimal
PRINT %10011010010 : REM Binary
Output:
1234
1234
1234
[edit] bc
Numeric literals use the digits 0-9 and A-F (only the uppercase letters). The minus sign '-' and radix point '.' are optional. When the program encounters a numeric literal, it uses the current value of ibase.
This example shows the literal -727 in all bases from 2 to 16. (It never prints "Impossible!")
ibase = 2
b[10] = -1011010111
ibase = 11 /* 3 */
b[10] = -222221
ibase = 11 /* 4 */
b[10] = -23113
ibase = 11 /* 5 */
b[10] = -10402
ibase = 11 /* 6 */
b[10] = -3211
ibase = 11 /* 7 */
b[10] = -2056
ibase = 11 /* 8 */
b[10] = -1327
ibase = 11 /* 9 */
b[10] = -887
ibase = 11 /* 10 */
b[10] = -727
ibase = 11 /* 11 */
b[10] = -601
ibase = 11 /* 12 */
b[10] = -507
ibase = 11 /* 13 */
b[10] = -43C
ibase = 11 /* 14 */
b[10] = -39D
ibase = 11 /* 15 */
b[10] = -337
ibase = 11 /* 16 */
b[10] = -2D7
ibase = A
for (i = 2; i <= 16; i++) if (b[i] != -727) "Impossible!
"
quit
The digits 0-9 and A-F are valid with all input bases. For example, FF from base 2 is 45 (because 15 * 2 + 15 is 45), and FF from base 10 is 165 (because 15 * 10 + 15 is 45). Most importantly, ibase = A always switches to base ten.
[edit] Befunge
While Befunge doesn't directly support numbers aside from 0-9 (base 10), characters in strings are essentially treated as base-256 numbers.
" ~"..@
Output:
126 32
[edit] C
Leading 0 means octal, 0x or 0X means hexadecimal. Otherwise, it is just decimal.
#include <stdio.h>
int main(void)
{
printf("%s\n",
( (727 == 0x2d7) &&
(727 == 01327) ) ? "true" : "false");
return 0;
}
GCC supports specifying integers in binary using the 0b prefix syntax, but it's not standard. Standard C has no way of specifying integers in binary.
To specify a literal of an unsigned integer, you add the suffix "u" or "U". To specify a literal of a "long" integer, you add the suffix "l" or "L". In C99, to specify a literal of a "long long" integer, you add the suffix "ll" or "LL". (The "l" and "ll" forms are discouraged as "l" looks like the digit "1"). The "u" suffixes can be combined with "l" or "ll" suffixes for unsigned long or unsigned long long integers.
[edit] C#
C# has decimal and hexadecimal integer literals, the latter of which are prefixed with 0x:
int a = 42;
int b = 0x2a;
Literals of either form can be suffixed with U and/or L. U will cause the literal to be interpreted as an unsigned type (necessary for numbers exceeding 231 or hex literals that have a first digit larger than 7) and L signifies the use of a long type – using UL or LU as suffix will then use ulong. C# has no syntactic notion of expressing integer literals of smaller types than Int32; it is a compile-time error to have an assignment such as
byte x = 500;
[edit] C++
The same comments apply as to the C example.
#include <iostream>
int main()
{
std::cout << ( (727 == 0x2d7) &&
(727 == 01327) ? "true" : "false")
<< std::endl;
return 0;
}
[edit] Clojure
Clojure uses the Java octal (0...) and hexadecimal (0x...) notation; for any other base, nR... is used, 2 <= n <= 36.
user=> 2r1001
9
user=> 8r64
52
user=> 064
52
user=> 16r4b
75
user=> 0x4b
75
user=>
[edit] 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
[edit] D
D besides hexadecimal, has also binary base. Additionally you can use _ to separate digits in integer (and FP) literals. Octal number literals are library-based to avoid bugs caused by the leading zero.
import std.stdio, std.conv;
void main() {
writeln("oct: ", octal!777);
writeln("bin: ", 0b01011010);
writeln("hex: ", 0xBADF00D);
writeln("dec: ", 1000000000);
writeln("dec: ", 1_000_000_000);
writeln();
writeln(typeid(typeof(0)));
writeln(typeid(typeof(0u)));
// writeln(typeid(typeof(0l))); // 'l' suffix is deprecated
writeln(typeid(typeof(0L)));
writeln(typeid(typeof(0uL)));
writeln(typeid(typeof(0LU)));
writeln();
writefln("%x", 0xFEE1_BAD_CAFE_BABEuL);
}
- Output:
oct: 511 bin: 90 hex: 195948557 dec: 1000000000 dec: 1000000000 int uint long ulong ulong fee1badcafebabe
[edit] Delphi
const
INT_VALUE = 256;
HEX_VALUE = $100;
[edit] DWScript
DWScript has decimal and hexadecimal integer literals, the latter of which are prefixed with $:
var a : Integer := 42;
var b : Integer := $2a;
Both notations can also be used for character codes (when prefixed by #).
[edit] E
? 256
# value: 256
? 0x100
# value: 256
? 0123
# syntax error: Octal is no longer supported: 0123
[edit] Efene
@public
run = fn () {
io.format("0xff : ~B~n", [0xff])
io.format("0xFF : ~B~n", [0xFF])
io.format("0o777 : ~B~n", [0o777])
io.format("0b1011: ~B~n", [0b1011])
}
[edit] Erlang
Erlang allows integer literals in bases 2 through 36. The format is Base#Number. For bases greater than 10, the values 10-35 are represented by A-Z or a-z.
> 2#101.
5
> 101.
101
> 16#F.
15
> 36#3z.
143
[edit] 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.
HEX
FEEDFACE
2 BASE !
1011001
DECIMAL
1234
: mask var @ [ base @ hex ] 3fff and [ base ! ] var ! ;
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').
1234 ( n )
123.4 ( l h )
123e4 ( F: n )
[edit] 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.
$feedface \ hexadecimal
&1234 \ decimal
%1001101 \ binary
'a \ base 256 (ASCII literal)
Some Forths also support "0xABCD" hex literals for compatibility with C-like languages.
[edit] 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
Outputs:
727 727 727 727
[edit] Frink
Bases from 2 to 36 are allowed in Frink. All literals can be arbitrarily large. Frink does not subscribe to the insanity that a leading 0 implies octal.
123456789123456789 // (a number in base 10)
123_456_789_123_456_789 // (the same number in base 10 with underscores for readability)
1 quadrillion // (named numbers are fine in Frink.)
1ee39 // (exact exponent, an integer with exact value 10^39)
100001000101111111101101\\2 // (a number in base 2)
1000_0100_0101_1111_1110_1101\\2 // (a number in base 2 with underscores for readability)
845FED\\16 // (a number in base 16... bases from 2 to 36 are allowed)
845fed\\16 // (The same number in base 16... upper or lowercase are allowed.)
845_fed\\16 // (a number in base 16 with underscores for readability)
FrinkRulesYou\\36 // (a number in base 36)
0x845fed // (Common hexadecimal notation)
0x845FED // (Common hexadecimal notation)
0xFEED_FACE // (Hexadecimal with underscores for readability)
0b100001000101111111101101 // (Common binary notation)
0b1000_0100_0101_1111_1110_1101 // (Binary with underscores for readability)
[edit] Go
For integer literals, leading 0 means octal, 0x or 0X means hexadecimal. Otherwise, it is just decimal. There is no binary.
Character literals though, also specify integer values. Go source is specified to be UTF-8 encoded. The value of a character literal is the Unicode value of the UTF-8 encoded character.
package main
import "fmt"
func main() {
fmt.Println(727 == 0x2d7) // prints true
fmt.Println(727 == 01327) // prints true
fmt.Println(727 == '˗') // prints true
}
[edit] Groovy
Solution:
println 025 // octal
println 25 // decimal integer
println 25l // decimal long
println 25g // decimal BigInteger
println 0x25 // hexadecimal
Output:
21 25 25 25 37
[edit] Haskell
(This is an interactive ghci session)
Oct(leading 0o or 0O), Hex(leading 0x or 0X)
Prelude> 727 == 0o1327
True
Prelude> 727 == 0x2d7
True
[edit] HicEst
HicEst only supports decimal integer literals.
[edit] Icon and Unicon
Icon/Unicon supports digit literals of the form <base>r<value> with base being from 2-36 and the digits being from 0..9 and a..z.
procedure main()
L := [1, 2r10, 3r10, 4r10, 5r10, 6r10, 7r10, 8r10, 9r10, 10r10, 11r10, 12r10, 13r10, 14r10,
15r10, 16r10, 17r10, 18r10,19r10, 20r10, 21r10, 22r10, 23r10, 24r10, 25r10, 26r10, 27r10,
28r10, 29r10, 30r10, 31r10, 32r10, 33r10, 34r10, 35r10, 36r10]
every write(!L)
end
[edit] J
J's numeric mini-language allows spaces, underlines, dots and lower case alphabetic characters in its numeric literals.
Arbitrary base numbers begin with a base ten literal (which represents the base of this number), and then the letter 'b' and then an arbitrary sequence of digits and letters which represents the number in that base. Letters a..z represent digits in the range 10..35. Each numeric item in a numeric constant must have its base specified independently.
10b123 16b123 8b123 20b123 2b123 1b123 0b123 100b123 99 0
123 291 83 443 11 6 3 10203 99 0
This may be used to enter hexadecimal or octal or binary numbers. However, note also that J's primitives support a variety of binary operations on numbers represented as sequences of 0s and 1s, like this:
0 1 0 0 0 1 0 0 0 1 1 1 1
J also supports extended precision integers, if one member of a list ends with an 'x' when they are parsed. Extended precision literals can not be combined, in the same constant, with arbitrary base literals because of ambiguities regarding the meaning of the letters.
123456789123456789123456789 100000000000x
123456789123456789123456789 100000000000
16b100 10x
|ill-formed number
J also allows integers to be entered using other notations, such as scientific or rational.
1e2 100r5
100 20
Internally, J freely converts fixed precision integers to floating point numbers when they overflow, and numbers (including integers) of any type may be combined using any operation where they would individually be valid arguments.
Internally, J represents numeric constants in their simplest type, regardless of how they were specified. In other words 9r1, although it is "specified as a rational" is represented as an extended precision integer. Similarly, 2.0, although it is "specified as a floating point value" is represented as an integer, and 1.0 is represented as a boolean.
That said, note that "type" is a property of the array, and not a property of the value. And, code that modifies the structure of an array leaves its type alone. So, if you need an array of a type different than that specified by J's "simplest type for constants" rule, you can extract the constant you need from an array which contains it and has the type you need. For example {.1 2 would give you an integer 1 instead of a boolean 1.
[edit] Java
A leading 0 means octal, 0x or 0X means hexadecimal. Otherwise, it is just decimal.
public class IntegerLiterals {
public static void main(String[] args) {
System.out.println( 727 == 0x2d7 &&
727 == 01327 );
}
}
You may also specify a long literal by adding an l or L (uppercase is preferred as the lowercase looks like a "1" in some fonts) to the end (ex: long a = 574298540721727L). This is required for numbers that are too large to be expressed as an int.
Java 7 has added binary literals to the language. A leading 0b means binary. You may also use underscores as separators in all bases.
public class BinaryLiteral {
public static void main(String[] args) {
System.out.println( 727 == 0b10_1101_0111 );
}
}
[edit] JavaScript
if ( 727 == 0x2d7 &&
727 == 01327 )
window.alert("true");
[edit] Logo
Logo only supports decimal integer literals.
[edit] Lua
Lua supports either base ten or hex
45, 0x45
[edit] M4
m4 has decimal, octal and hexadecimal literals like C.
eval(10) # base 10Output:
eval(010) # base 8
eval(0x10) # base 16
10 # base 10 8 # base 8 16 # base 16
As an extension, GNU m4 provides "0b" and "0r" literals.
eval(0b10) # base 2Output:
eval(`0r2:10') # base 2
...
eval(`0r36:10') # base 36
2 # base 2 2 # base 2 ... 36 # base 36
[edit] Mathematica
b^^nnnn is a valid number in base b (with b ranging from 2 to 36) :
2^^1011
-> 11
36^^1011
-> 46693
[edit] Maxima
/* Maxima has integers of arbitrary length */
170141183460469231731687303715884105727
[edit] Metafont
num1 := oct"100";
num2 := hex"100";
Metafont numbers can't be greater than 4096, so that the maximum octal and hexadecimal legal values are 7777 and FFF respectively. To be honest, "100" is a string, and oct is an "internal" "macro"; but this is the way Metafont specifies numbers in base 8 and 16.
[edit] Modula-3
All numbers 2 to 16 are allowed to be bases.
MODULE Literals EXPORTS Main;
IMPORT IO;
BEGIN
IO.PutInt(16_2D7);
IO.Put(" ");
IO.PutInt(10_727);
IO.Put(" ");
IO.PutInt(8_1327);
IO.Put(" ");
IO.PutInt(2_1011010111);
IO.Put("\n");
END Literals.
[edit] NetRexx
Along with decimal notation NetRexx accepts numeric literals in hexadecimal and binary formats.
The NetRexx documentation describes hexadecimal and binary literal symbol notation in more detail; a summary follows:
A hexadecimal numeric symbol describes a whole number, and is of the form nXstring where, n is a simple number which describes the effective length of the hexadecimal string and string is a string of one or more hexadecimal characters.
A binary numeric symbol describes a whole number using the same rules, except that the identifying character is B or b, and the digits of string must be either 0 or 1, each representing a single bit.
/* NetRexx */
options replace format comments java crossref symbols
iv = 8; say '8'.right(20) '==' iv.right(8) -- 8
iv = -8; say '-8'.right(20) '==' iv.right(8) -- -8
iv = 1x8; say '1x8'.right(20) '==' iv.right(8) -- -8
iv = 2x8; say '2x8'.right(20) '==' iv.right(8) -- 8
iv = 2x08; say '2x08'.right(20) '==' iv.right(8) -- 8
iv = 0x08; say '0x08'.right(20) '==' iv.right(8) -- 8
iv = 0x10; say '0x10'.right(20) '==' iv.right(8) -- 16
iv = 0x81; say '0x81'.right(20) '==' iv.right(8) -- 129
iv = 2x81; say '2x81'.right(20) '==' iv.right(8) -- -127
iv = 3x81; say '3x81'.right(20) '==' iv.right(8) -- 129
iv = 4x81; say '4x81'.right(20) '==' iv.right(8) -- 129
iv = 04x81; say '04x81'.right(20) '==' iv.right(8) -- 129
iv = 16x81; say '16x81'.right(20) '==' iv.right(8) -- 129
iv = 4xF081; say '4xF081'.right(20) '==' iv.right(8) -- -3967
iv = 8xF081; say '8xF081'.right(20) '==' iv.right(8) -- 61569
iv = 0Xf081; say '0Xf081'.right(20) '==' iv.right(8) -- 61569
iv = 0xffff; say '0xffff'.right(20) '==' iv.right(8) -- 65535
iv = 4xffff; say '4xffff'.right(20) '==' iv.right(8) -- -1
iv = 8xffff; say '8xffff'.right(20) '==' iv.right(8) -- 65535
iv = 1b0; say '1b0'.right(20) '==' iv.right(8) -- 0
iv = 1b1; say '1b1'.right(20) '==' iv.right(8) -- -1
iv = 2b1; say '2b1'.right(20) '==' iv.right(8) -- 1
iv = 0b10; say '0b10'.right(20) '==' iv.right(8) -- 2
iv = 2b10; say '2b10'.right(20) '==' iv.right(8) -- -2
iv = 3b10; say '3b10'.right(20) '==' iv.right(8) -- 2
iv = 0b100; say '0b100'.right(20) '==' iv.right(8) -- 4
iv = 3b100; say '3b100'.right(20) '==' iv.right(8) -- -4
iv = 4b100; say '4b100'.right(20) '==' iv.right(8) -- 4
iv = 4b1000; say '4b1000'.right(20) '==' iv.right(8) -- -8
iv = 8B1000; say '8B1000'.right(20) '==' iv.right(8) -- 8
iv = 00B1111111111111111; say '00B1111111111111111'.right(20) '==' iv.right(8) -- 65535
iv = 16B1111111111111111; say '16B1111111111111111'.right(20) '==' iv.right(8) -- -1
iv = 32B1111111111111111; say '32B1111111111111111'.right(20) '==' iv.right(8) -- 65535
return
Output:
8 == 8
-8 == -8
1x8 == -8
2x8 == 8
2x08 == 8
0x08 == 8
0x10 == 16
0x81 == 129
2x81 == -127
3x81 == 129
4x81 == 129
04x81 == 129
16x81 == 129
4xF081 == -3967
8xF081 == 61569
0Xf081 == 61569
0xffff == 65535
4xffff == -1
8xffff == 65535
1b0 == 0
1b1 == -1
2b1 == 1
0b10 == 2
2b10 == -2
3b10 == 2
0b100 == 4
3b100 == -4
4b100 == 4
4b1000 == -8
8B1000 == 8
00B1111111111111111 == 65535
16B1111111111111111 == -1
32B1111111111111111 == 65535
[edit] Objeck
As of v1.1, Objeck only supports hexadecimal and decimal literals.
bundle Default {
class Literal {
function : Main(args : String[]) ~ Nil {
(727 = 0x2d7)->PrintLine();
}
}
}
[edit] OCaml
(This is an interactive ocaml session)
Bin(leading 0b or 0B), Oct(leading 0o or 0O), Hex(leading 0x or 0X)
# 727 = 0b1011010111;;
- : bool = true
# 727 = 0o1327;;
- : bool = true
# 727 = 0x2d7;;
- : bool = true
# 12345 = 12_345 (* underscores are ignored; useful for keeping track of places *);;
- : bool = true
Literals for the other built-in integer types:
- 727l - int32
- 727L - int64
- 727n - nativeint
[edit] Oz
To demonstrate the different numerical bases, we unify the identical values:
try
%% binary octal dec. hexadecimal
0b1011010111 = 01327 = 727 = 0x2d7
{Show success}
catch _ then
{Show unexpectedError}
end
Negative integers start with "~":
X = ~42
[edit] PARI/GP
GP doesn't support input in other bases, though see the FAQ for an approach with a script. Pari of course supports precisely those bases supported by C.
[edit] Pascal
See Delphi
[edit] Perl
print "true\n" if ( 727 == 0x2d7 &&
727 == 01327 &&
727 == 0b1011010111 &&
12345 == 12_345 # underscores are ignored; useful for keeping track of places
);
[edit] Perl 6
These all print 255.
say 255;
say 0d255;
say 0xff;
say 0o377;
say 0b1111_1111;
say :10<255>;
say :16<ff>;
say :8<377>;
say :2<1111_1111>;
say :3<100110>;
say :4<3333>;
say :12<193>;
say :36<73>;
There is a specced form for bases above 36, but rakudo does not yet implement it.
[edit] PHP
<?php
if ( 727 == 0x2d7 &&
727 == 01327 )
echo "true\n";
?>
[edit] PicoLisp
In the strict sense of this task, PicoLisp reads only integers at bases which are a power of ten (scaled fixpoint numbers). This is controlled via the global variable '*Scl':
: (setq *Scl 4)
-> 4
: 123.456789
-> 1234568
However, the reader is normally augmented by read macros, which can read any base or any desired format. Read macros are not executed at runtime, but intially when the sources are read.
: '(a `(hex "7F") b `(oct "377") c)
-> (a 127 b 255 c)
In addition to standard formats like 'hex' (hexadecimal) and 'oct' (octal), there are also more esoteric formats like 'fmt64' (base 64) and 'hax' (hexadecimal numbers coded with alphabetic characters).
[edit] PL/I
12345
'b4'xn /* a hexadecimal literal integer. */
'ffff_ffff'xn /* a longer hexadecimal hexadecimal integer. */
1101b /* a binary integer, of value decimal 13. */
[edit] PostScript
Integer literals in PostScript can be either standard decimal literals or in the form base#number. base can be any decimal integer between 2 and 36, number can then use digits from 0 to base − 1. Digits above 9 are replaced by A through Z and case does not matter.
123 % 123
8#1777 % 1023
16#FFFE % 65534
2#11011 % 27
5#44 % 24
[edit] PowerShell
PowerShell only supports base 10 and 16 directly:
727 # base 10
0x2d7 # base 16
Furthermore there are special suffices which treat the integer as a multiple of a specific power of two, intended to simplify file size operations:
3KB # 3072
3MB # 3145728
3GB # 3221225472
3TB # 3298534883328
A number can be suffixed with d to make it a decimal. This doesn't work in conjunction with above suffixes, though:
PS> 4d.GetType().ToString() System.Decimal
[edit] PureBasic
PureBasic allows integer literals to be specified in base 10, base 2 by using the prefix '%', or base 16 by using the prefix '$'.
x = 15 ;15 in base 10
x = %1111 ;15 in base 2
x = $f ;15 in base 16
An integer literal representing a character code can also be expressed by surrounding the character with single quotes. More than one character can be included in the single quotes (i.e. 'abc'). Depending on whether code is compiled in Ascii or Unicode mode this will result in the integer value being specified in base 256 or base 65536 respectively.
x = 'a' ;129
[edit] Python
Python 3.0 brought in the binary literal and uses 0o or 0O exclusively for octal.
>>> # Bin(leading 0b or 0B), Oct(leading 0o or 0O), Dec, Hex(leading 0x or 0X), in order:
>>> 0b1011010111 == 0o1327 == 727 == 0x2d7
True
>>>
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.
>>> # 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
>>>
>>> # Oct(leading 0), Dec, Hex(leading 0x or 0X), in order:
>>> 01327 == 727 == 0x2d7
True
>>>
In Python 2.x you may also specify a long literal by adding an l or L (the latter form is preferred as the former looks like a "1") to the end (ex: 574298540721727L), but this is optional, as integer literals that are too large for an int will be interpreted as a long.
[edit] R
0x or 0X followed by digits or the letters a-f denotes a hexadecimal number. The suffix L means that the number should be stored as an integer rather than numeric (floating point).
0x2d7==727 # TRUE
identical(0x2d7, 727) # TRUE
is.numeric(727) # TRUE
is.integer(727) # FALSE
is.integer(727L) # TRUE
is.numeric(0x2d7) # TRUE
is.integer(0x2d7) # FALSE
is.integer(0x2d7L) # TRUE
For more information, see Section 10.3.1 of the R Language definition (PDF).
[edit] Racket
#lang racket
#b1011010111
#o1327
#d727
#x2d7
Output:
727 727 727 727
[edit] REBOL
1
[edit] Retro
#100 ( decimal )
%100 ( binary )
$100 ( hex )
'c ( ascii character )
100 ( number in current base )
Numbers without a prefix are interpreted using the current base, which is a variable Valid characters are stored in a string called numbers, which can also be altered to allow for larger bases.
[edit] REXX
thing=37
thing='37' /*this is exactly the same as above. */
say 'base 10=' thing
say 'base 2=' x2b(d2x(thing))
say 'base 16=' d2x(thing)
say 'base 256=' d2c(thing) /*the output shown is ASCII (or maybe EBCDIC).*/
output
base 10= 37 base 2= 00100101 base 16= 25 base 256= %
On TSO d2c(37) does not result in a displayable character. With thing=c2d('A') I see:
base 10= 193 base 2= 11000001 base 16= C1 base 256= A
The first three lines are platform-independent.
[edit] Ruby
(This is an interactive irb session)
irb(main):001:0> 727 == 0b1011010111
=> true
irb(main):002:0> 727 == 0x2d7
=> true
irb(main):003:0> 727 == 01327
=> true
irb(main):001:0> 12345 == 12_345 # underscores are ignored; useful for keeping track of places
=> true
[edit] Scala
Scala has signed integers of 8, 16, 32 and 64 bits. They can be represented in decimal, octal by prefixing
0, or hexadecimal by prefixing 0x or 0X. Without any other type hint,
it defaults to 32 bits integers, or an Int. An l or L suffix will
indicate a 64 bits integer, or a Long. The other two types, Byte and Short,
can be represented using type ascription, as shown below.
scala> 16 res10: Int = 16 scala> 020L res11: Long = 16 scala> 0x10 : Byte res12: Byte = 16 scala> 16 : Short res13: Short = 16 scala> 020 : Int res14: Int = 16 scala> 0x10 : Long res15: Long = 16
[edit] 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
[edit] Seed7
In Seed7 integer literals may have the form <base>#<numeral>. Here <base> can be from the range 2..36. For example:
$ include "seed7_05.s7i";
const proc: main is func
begin
writeln(727);
writeln(32#MN);
writeln(16#2D7);
writeln(10#727);
writeln(8#1327);
writeln(2#1011010111);
end func;
Sample output:
727 727 727 727 727 727
[edit] Slate
2r1011010111 + 8r1327 + 10r727 + 16r2d7 / 4
[edit] Smalltalk
2r1011010111 + 5r100 + 8r1327 + 10r727 + 16r2d7 / 4
binary, base-5, octal, decimal, binary, decimal (default). The radix is any between 2 and 32 (although only 2, 8, 10 and 16 are typically needed).
There is no size limit (except memory constraints), the runtime chooses an appropriate representation automatically:
16r1B30964EC395DC24069528D54BBDA40D16E966EF9A70EB21B5B2943A321CDF10391745570CCA9420C6ECB3B72ED2EE8B02EA2735C61A000000000000000000000000 = 100 factorial
"evaluates to true"
[edit] Standard ML
(This is an interactive SML/NJ session)
Hex(leading 0x), Word (unsigned ints, leading 0w), Word Hex (leading 0wx)
- 727 = 0x2d7;
val it = true : bool
- 727 = Word.toInt 0w727;
val it = true : bool
- 0w727 = 0wx2d7;
val it = true : bool
- ~727; (* negative number;
* ~ is the unary negation operator for all numbers, including reals and ints;
* worth mentioning because it's unusual
*)
val it = ~727 : int
[edit] Tcl
(This is an interactive tclsh session; expr is only called to evaluate the equality test.)
% expr 727 == 0x2d7
1
% expr 727 == 0o1327
1
% expr 727 == 01327
1
% expr 727 == 0b1011010111
1
[edit] TI-89 BASIC
Binary, decimal, and hexadecimal are supported. The system base mode sets the default output base, but does not affect input; unmarked digits are always decimal.
0b10000001 = 129 = 0h81
[edit] UNIX Shell
The expr command accepts only decimal literals.
$ expr 700 - 1
699
$ expr 0700 - 01
699
Some shells have arithmetic expansion. These shells may accept literals in other bases. This syntax only works in places that do arithmetic expansion, such as in $(( )), or in Bash's let command.
Quoting the manual page of pdksh:
Integer constants may be specified with arbitrary bases using the notation base#number, where base is a decimal integer specifying the base, and number is a number in the specified base. Additionally, integers may be prefixed with `0X' or `0x' (specifying base 16) or `0' (base 8) in all forms of arithmetic expressions, except as numeric arguments to the test command.
pdksh allows bases from 2 to 36. The letters a-z or A-Z represent numbers 10 to 35.
Bash allows the same syntax as pdksh. In addition, Bash can handle bases as high as 64: 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).
dec=727
oct=$(( 01327 ))
bin=$(( 2#1011010111 ))
hex=$(( 0x2d7 ))
# or e.g.
let bin=2#1011010111
let "baseXX = 20#1g7"
dec=727
oct=$(( 01327 ))
bin=$(( 2#1011010111 ))
hex=$(( 0x2d7 ))
# or e.g.
(( bin = 2#1011010111 ))
(( baseXX = 20#1g7 ))
[edit] Ursala
Natural numbers (i.e., unsigned integers) of any size are supported. Only decimal integer literals are recognized by the compiler, as in a declaration such as the following.
n = 724
Signed integers are also recognized and are considered a separate type from natural numbers, but non-negative integers and natural numbers have compatible binary representations.
z = -35
Signed rational numbers of unlimited precision are yet another primitive type and can be expressed in conventional decimal form.
m = -2/3
The forward slash in a rational literal is part of the syntax and not a division operator. Finally, a signed or unsigned integer with a trailing underscore, like this
t = 4534934521_
is used for numbers stored in binary converted decimal format, also with unlimited precision, which may perform better in applications involving very large decimal numbers.
[edit] XPL0
code CrLf=9, IntOut=11;
def A=123, B=$123, C=%11_0011, D=^A;
[IntOut(0, A); CrLf(0); \decimal
IntOut(0, B); CrLf(0); \hex
IntOut(0, C); CrLf(0); \binary
IntOut(0, D); CrLf(0); \ASCII
]
Output:
123 291 51 65
- Programming Tasks
- Basic language learning
- Ada
- Aime
- ALGOL 68
- AmigaE
- AutoHotkey
- AWK
- BASIC
- BBC BASIC
- Bc
- Befunge
- C
- C sharp
- C++
- Clojure
- Common Lisp
- D
- Delphi
- DWScript
- E
- Efene
- Erlang
- Forth
- Fortran
- Frink
- Go
- Groovy
- Haskell
- HicEst
- Icon
- Unicon
- J
- Java
- JavaScript
- Logo
- Lua
- M4
- Mathematica
- Maxima
- Metafont
- Modula-3
- NetRexx
- Objeck
- OCaml
- Oz
- PARI/GP
- Pascal
- Perl
- Perl 6
- PHP
- PicoLisp
- PL/I
- PostScript
- PowerShell
- PureBasic
- Python
- R
- Racket
- REBOL
- Retro
- REXX
- Ruby
- Scala
- Scheme
- Seed7
- Slate
- Smalltalk
- Standard ML
- Tcl
- TI-89 BASIC
- UNIX Shell
- Ursala
- XPL0
- ML/I/Omit