This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,9 @@
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.
Also show any other ways of expressing literals, e.g. for different types of integers.
See also [[Literals/Floating point]].

View file

@ -0,0 +1,2 @@
---
note: Basic language learning

View file

@ -0,0 +1,40 @@
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
)

View file

@ -0,0 +1,6 @@
BEGIN {
if ( (0x2d7 == 727) &&
(01327 == 727) ) {
print "true with GNU awk"
}
}

View file

@ -0,0 +1,5 @@
BEGIN {
x2d7 = "Goodbye, world!"
print 0x2d7 # gawk prints "727", nawk prints "0Goodbye, world!"
print 01327 # gawk prints "727", nawk prints "1327"
}

View file

@ -0,0 +1,9 @@
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;

View file

@ -0,0 +1,5 @@
if ((727 == 0x2d7) && (727 == 01327)) {
o_text("true\n");
} else {
o_text("false\n");
}

View file

@ -0,0 +1,3 @@
PROC main()
IF ($2d7 = 727) AND (%001011010111 = 727) THEN WriteF('true\n')
ENDPROC

View file

@ -0,0 +1,2 @@
If (727 == 0x2d7)
MsgBox true

View file

@ -0,0 +1,3 @@
PRINT 17
PRINT &O21
PRINT &H11

View file

@ -0,0 +1,3 @@
PRINT 1234 : REM Decimal
PRINT &4D2 : REM Hexadecimal
PRINT %10011010010 : REM Binary

View file

@ -0,0 +1 @@
" ~"..@

View file

@ -0,0 +1,10 @@
#include <iostream>
int main()
{
std::cout << ( (727 == 0x2d7) &&
(727 == 01327) ? "true" : "false")
<< std::endl;
return 0;
}

View file

@ -0,0 +1,10 @@
#include <stdio.h>
int main(void)
{
printf("%s\n",
( (727 == 0x2d7) &&
(727 == 01327) ) ? "true" : "false");
return 0;
}

View file

@ -0,0 +1,11 @@
user=> 2r1001
9
user=> 8r64
52
user=> 064
52
user=> 16r4b
75
user=> 0x4b
75
user=>

View file

@ -0,0 +1,8 @@
>(= 727 #b1011010111)
T
>(= 727 #o1327)
T
>(= 727 #x2d7)
T
>(= 727 #20r1g7)
T

View file

@ -0,0 +1,20 @@
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);
}

View file

@ -0,0 +1,2 @@
var a : Integer := 42;
var b : Integer := $2a;

View file

@ -0,0 +1,3 @@
const
INT_VALUE = 256;
HEX_VALUE = $100;

View file

@ -0,0 +1,8 @@
? 256
# value: 256
? 0x100
# value: 256
? 0123
# syntax error: Octal is no longer supported: 0123

View file

@ -0,0 +1,7 @@
@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])
}

View file

@ -0,0 +1,8 @@
> 2#101.
5
> 101.
101
> 16#F.
15
> 36#3z.
143

View file

@ -0,0 +1,7 @@
HEX
FEEDFACE
2 BASE !
1011001
DECIMAL
1234
: mask var @ [ base @ hex ] 3fff and [ base ! ] var ! ;

View file

@ -0,0 +1,3 @@
1234 ( n )
123.4 ( l h )
123e4 ( F: n )

View file

@ -0,0 +1,4 @@
$feedface \ hexadecimal
&1234 \ decimal
%1001101 \ binary
'a \ base 256 (ASCII literal)

View file

@ -0,0 +1,11 @@
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

View file

@ -0,0 +1,15 @@
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)

View file

@ -0,0 +1,9 @@
package main
import "fmt"
func main() {
fmt.Println(727 == 0x2d7) // prints true
fmt.Println(727 == 01327) // prints true
fmt.Println(727 == '˗') // prints true
}

View file

@ -0,0 +1,5 @@
println 025 // octal
println 25 // decimal integer
println 25l // decimal long
println 25g // decimal BigInteger
println 0x25 // hexadecimal

View file

@ -0,0 +1,4 @@
Prelude> 727 == 0o1327
True
Prelude> 727 == 0x2d7
True

View file

@ -0,0 +1,7 @@
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

View file

@ -0,0 +1,2 @@
10b123 16b123 8b123 20b123 2b123 1b123 0b123 100b123 99 0
123 291 83 443 11 6 3 10203 99 0

View file

@ -0,0 +1 @@
0 1 0 0 0 1 0 0 0 1 1 1 1

View file

@ -0,0 +1,5 @@
123456789123456789123456789 100000000000x
123456789123456789123456789 100000000000
16b100 10x
|ill-formed number

View file

@ -0,0 +1,2 @@
1e2 100r5
100 20

View file

@ -0,0 +1,6 @@
public class IntegerLiterals {
public static void main(String[] args) {
System.out.println( 727 == 0x2d7 &&
727 == 01327 );
}
}

View file

@ -0,0 +1,5 @@
public class BinaryLiteral {
public static void main(String[] args) {
System.out.println( 727 == 0b10_1101_0111 );
}
}

View file

@ -0,0 +1,3 @@
if ( 727 == 0x2d7 &&
727 == 01327 )
window.alert("true");

View file

@ -0,0 +1 @@
45, 0x45

View file

@ -0,0 +1,3 @@
eval(10) # base 10
eval(010) # base 8
eval(0x10) # base 16

View file

@ -0,0 +1,4 @@
eval(0b10) # base 2
eval(`0r2:10') # base 2
...
eval(`0r36:10') # base 36

View file

@ -0,0 +1,6 @@
b^^nnnn is a valid number in base b (with b ranging from 2 to 36) :
2^^1011
-> 11
36^^1011
-> 46693

View file

@ -0,0 +1,2 @@
/* Maxima has integers of arbitrary length */
170141183460469231731687303715884105727

View file

@ -0,0 +1,2 @@
num1 := oct"100";
num2 := hex"100";

View file

@ -0,0 +1,14 @@
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.

View file

@ -0,0 +1,5 @@
<?php
if ( 727 == 0x2d7 &&
727 == 01327 )
echo "true\n";
?>

View file

@ -0,0 +1,5 @@
print "true\n" if ( 727 == 0x2d7 &&
727 == 01327 &&
727 == 0b1011010111 &&
12345 == 12_345 # underscores are ignored; useful for keeping track of places
);

View file

@ -0,0 +1,5 @@
: (setq *Scl 4)
-> 4
: 123.456789
-> 1234568

View file

@ -0,0 +1,2 @@
: '(a `(hex "7F") b `(oct "377") c)
-> (a 127 b 255 c)

View file

@ -0,0 +1,4 @@
>>> # Bin(leading 0b or 0B), Oct(leading 0o or 0O), Dec, Hex(leading 0x or 0X), in order:
>>> 0b1011010111 == 0o1327 == 727 == 0x2d7
True
>>>

View file

@ -0,0 +1,4 @@
>>> # 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
>>>

View file

@ -0,0 +1,4 @@
>>> # Oct(leading 0), Dec, Hex(leading 0x or 0X), in order:
>>> 01327 == 727 == 0x2d7
True
>>>

View file

@ -0,0 +1,8 @@
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

View file

@ -0,0 +1,7 @@
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).*/

View file

@ -0,0 +1,5 @@
#lang racket
#b1011010111
#o1327
#d727
#x2d7

View file

@ -0,0 +1,8 @@
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

View file

@ -0,0 +1,8 @@
> (= 727 #b1011010111)
#t
> (= 727 #o1327)
#t
> (= 727 #d727)
#t
> (= 727 #x2d7)
#t

View file

@ -0,0 +1 @@
2r1011010111 + 5r100 + 8r1327 + 10r727 + 16r2d7 / 4

View file

@ -0,0 +1,2 @@
16r1B30964EC395DC24069528D54BBDA40D16E966EF9A70EB21B5B2943A321CDF10391745570CCA9420C6ECB3B72ED2EE8B02EA2735C61A000000000000000000000000 = 100 factorial
"evaluates to true"

View file

@ -0,0 +1,8 @@
% expr 727 == 0x2d7
1
% expr 727 == 0o1327
1
% expr 727 == 01327
1
% expr 727 == 0b1011010111
1