Literals/Integer: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(15 intermediate revisions by 8 users not shown)
Line 623:
return 0;
}</syntaxhighlight>
 
=={{header|Cherrycake}}==
 
<syntaxhighlight lang="cherrycake">
515142 # Interpretted as an integer, 515142
0b10111011 # Interpretted as a binary integer, 10111011 (187)
0x0AB3 # Interpretted as a binary integer, 0AB3 (2739)
</syntaxhighlight>
 
=={{header|Clojure}}==
Line 769 ⟶ 777:
? 0123
# syntax error: Octal is no longer supported: 0123</syntaxhighlight>
 
=={{header|EasyLang}}==
EasyLang's ability to use hexadecimal literals is undocumented.
<syntaxhighlight lang="easylang">
decimal = 57
hexadecimal = 0x39
print decimal
print hexadecimal
</syntaxhighlight>
{{out}}
<pre>
57
57
</pre>
 
=={{header|Efene}}==
Line 821 ⟶ 843:
 
The digits and the radix character can both be any mixture of upper and lower case. See [http://www.gnu.org/software/emacs/manual/html_node/elisp/Integer-Basics.html GNU Elisp reference manual "Integer Basics"].
 
=={{header|EMal}}==
<syntaxhighlight lang="emal">
^|
| EMal internally uses 64 bit signed integers.
|^
int hex = 0xff # base16
int oct = 0o377 # base8
int bin = 0b11111111 # base2
int dec = 255 # base10
writeLine(hex)
writeLine(oct)
writeLine(bin)
writeLine(dec)
# here we check that they give the same value
writeLine(0b1011010111 == 0o1327 and
0o1327 == 0x2d7 and
0x2d7 == 727 and
727 == 0b1011010111)
</syntaxhighlight>
{{out}}
<pre>
255
255
255
255
</pre>
 
=={{header|Erlang}}==
Line 1,109 ⟶ 1,159:
every write(!L)
end</syntaxhighlight>
 
=={{Header|Insitux}}==
 
<syntaxhighlight lang="insitux">
[123 0x7F 0xFFF 0b0101001]
</syntaxhighlight>
 
{{out}}
 
<pre>
[123 127 4095 41]
</pre>
 
=={{header|J}}==
Line 1,180 ⟶ 1,242:
 
=={{header|Kotlin}}==
Kotlin supports 3 types of integer literal: (signeddecimal, 4hexadecimal byte),and namelybinary. :Hexadecimal decimal,literals hexadecimalare prefixed with <code>0x</code> or <code>0X</code>, and binary literals with <code>0b</code> or <code>0B</code>. Hexadecimal digits can be uppercase or lowercase, or a combination of the two.
 
A signed integer literal can be assigned to a variable of any signed integer type. If no type is specified, Int (4 bytes) is assumed. If the value cannot fit into an Int, Long (8 bytes) is assumed.
 
An unsigned integer literal is made by appending <code>u</code> or <code>U</code> to a signed integer literal. Unsigned literals can be assigned to any unsigned integer type, with UInt (4 bytes) being assumed if none is specified, or ULong (8 bytes) if the value cannot fit into a UInt.
 
Signed and unsigned integer literals can be forced to be interpreted as Long or ULong respectively by appending the suffix <code>L</code> to the literal (lower case 'l' is not allowed as it is easily confused with the digit '1').
 
Underscores can be used between digits of a literal for clarity.
 
<syntaxhighlight lang="kotlin">
These can be converted to long integer literals (signed 8 byte) by appending the suffix 'L' (lower case 'l' is not allowed as it is easily confused with the digit '1').
fun main() {
// signed integer literals
val d = 255 // decimal
val h = 0xff // hexadecimal (can use 0X instead of 0x)
val b = 0b11111111 // binary (can use 0B instead of 0b)
 
// signed long integer literals (cannot use l instead of L)
It is also possible to assign integer literals to variables of type Short (signed 2 byte) or Byte (signed 1 byte). They will be automatically converted by the compiler provided they are within the range of the variable concerned.
val ld = 255L // decimal
<syntaxhighlight lang="scala">// version 1.0.6
val lh = 0xffL // hexadecimal
val lb = 0b11111111L // binary
 
// unsigned integer literals (can use U instead of u)
fun main(args: Array<String>) {
val dud = 255 255u // decimal integer literal
val huh = 0xff 0xffu // hexadecimal integer literal
val bub = 0b11111111 0b11111111u // binary integer literal
 
val ld = 255L // decimalunsigned long integer literalliterals (can't use lU instead of Lu)
val lhuld = 0xffL 255uL // hexadecimal long integer literal (could use 0X rather than 0x)decimal
val lbulh = 0b11111111L0xffuL // binary long integer literal (could use 0B rather than// 0b)hexadecimal
val ulb = 0b11111111uL // binary
 
// implicit conversions
val sd : Short = 127 // decimal integer literal automatically converted to Short
val sh : Shortld2 = 0x7f2147483648 // hexadecimaldecimal signed integer literal automatically converted to ShortLong since it cannot fit into an Int
val bdush : Byte UShort = 0b011111110x7fu // binaryhexadecimal unsigned integer literal automatically converted to ByteUShort
val bd : Byte = 0b01111111 // binary signed integer literal automatically converted to Byte
 
println("$d $h $b $ud $uh $ub $ld $lh $lb $sduld $ulh $ulb $ld2 $shush $bd")
}</syntaxhighlight>
 
{{out}}
<pre>
255 255 255 255 255 255 127255 255 255 255 255 255 2147483648 127 127
</pre>
 
Line 1,740 ⟶ 1,819:
'ffff_ffff'xn /* a longer hexadecimal hexadecimal integer. */
1101b /* a binary integer, of value decimal 13. */
</syntaxhighlight>
 
=={{header|Plain English}}==
Plain English has two types of numerical literals. The first is the ordinary "number literal", which is expressed in base ten.
<syntaxhighlight lang="text">
12345
-12345 \ with a negative sign
+12345 \ with a positive sign
</syntaxhighlight>
 
The second is the "nibble literal", which is a dollar sign followed by a hexadecimal literal.
<syntaxhighlight lang="text">$12345DEADBEEF</syntaxhighlight>
 
Numerical literals can also be embedded into "ratio" or "mixed literals".
<syntaxhighlight lang="text">
123/456 \ ratio literal
1-2/3 \ mixed literal
</syntaxhighlight>
 
Line 1,967 ⟶ 2,063:
</pre>
 
Unsigned integers, which must begin with <code>#</code>, can be expressed in binary, octal, decimal or hexadecimal. A final lowercase letter defines the base.
#100111010b
#472o
#314d
#13Ah
 
=={{header|RPL}}==
#1011b <span style="color:grey">@ Base 2</span>
#1234o <span style="color:grey">@ Base 8</span>
#6789d <span style="color:grey">@ Base 10</span>
#ABCDh <span style="color:grey">@ Base 16</span>
=={{header|Ruby}}==
 
Line 2,181 ⟶ 2,288:
<syntaxhighlight lang="usala">t = 4534934521_</syntaxhighlight>
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.
 
=={{header|Uxntal}}==
Uxntal only allows hexadecimal literals, and they can be either one or two bytes. In order to push them to the stack, rather than writing them directly to the assembled binary, they must be prefixed with <code>#</code>.
<syntaxhighlight lang="Uxntal">#2a ( byte literal )
#c0de ( short literal )</syntaxhighlight>
And yes, they do have to be in lowercase hex.
 
=={{header|Verbexx}}==
Line 2,257 ⟶ 2,370:
End Sub</syntaxhighlight>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="Vlang">
fn main() {
w := 727
x := 0x2d7
y := 0o1327
z := 0b10110_10111
println([w, x, y, z])
}
</syntaxhighlight>
 
{{out}}
<pre>
[727, 727, 727, 727]
</pre>
 
=={{header|Wren}}==
Line 2,266 ⟶ 2,395:
 
As the only difference between integers and other numbers is that the former do not have a decimal part, it is also possible to represent integers using scientific notation.
<syntaxhighlight lang="ecmascriptwren">var a = 255
var b = 0xff
var c = 0255 // not an octal literal
9,485

edits