Jump to content

Literals/Integer: Difference between revisions

→‎{{header|Kotlin}}: Updated the Kotlin example to include unsigned types
(→‎{{header|Plain English}}: Added imformation)
(→‎{{header|Kotlin}}: Updated the Kotlin example to include unsigned types)
Line 1,194:
 
=={{header|Kotlin}}==
Kotlin supports 3 types of integer literal (signed 4 byte), namely : decimal, hexadecimal and binary.
 
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.
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').
 
An unsigned integer literal is made by appending 'u' or 'U' 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.
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.
<syntaxhighlight lang="scala">// version 1.0.6
 
TheseSigned and unsigned integer literals can be convertedforced to longbe integerinterpreted literalsas (signedLong 8or byte)ULong respectively by appending the suffix 'L' to the literal (lower case 'l' is not allowed as it is easily confused with the digit '1').
fun main(args: Array<String>) {
val d = 255 // decimal integer literal
val h = 0xff // hexadecimal integer literal
val b = 0b11111111 // binary integer literal
 
<syntaxhighlight lang="scalakotlin">// version 1.0.6
val ld = 255L // decimal long integer literal (can't use l instead of L)
fun main() {
val lh = 0xffL // hexadecimal long integer literal (could use 0X rather than 0x)
// signed integer literals
val lb = 0b11111111L // binary long integer literal (could use 0B rather than 0b)
val d = 255 // decimal integer literal
val h = 0xff // hexadecimal integer(can use 0X instead of literal0x)
val b = 0b11111111 // binary integer(can use 0B instead of literal0b)
 
// signed long integer literals (cannot use l instead of L)
val sd : Short = 127 // decimal integer literal automatically converted to Short
val sh : Shortld = 0x7f255L // hexadecimal integer literal automatically converted to// Shortdecimal
val bdlh := Byte0xffL = 0b01111111 // binary integer literal automatically converted to Byte // hexadecimal
val lb = 0b11111111L // binary long integer literal (could use 0B rather than 0b)
 
// unsigned integer literals (can use U instead of u)
println("$d $h $b $ld $lh $lb $sd $sh $bd")
val ud = 255u // decimal
val uh = 0xffu // hexadecimal
val ub = 0b11111111u // binary
 
val ld = 255L // decimalunsigned long integer literalliterals (can't use lU instead of Lu)
val uld = 255uL // decimal
val ulh = 0xffuL // hexadecimal
val ulb = 0b11111111uL // binary
 
// implicit conversions
val ld2 = 2147483648 // decimal signed integer literal automatically converted to Long since it cannot fit into an Int
val lh = 0xffL ush : UShort = 0x7fu // hexadecimal longunsigned integer literal (could use 0Xautomatically ratherconverted thanto 0x)UShort
val sdbd : ShortByte = 127 0b01111111 // decimalbinary signed integer literal automatically converted to ShortByte
 
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>
 
32

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.