Bird
Raised Fist0
Kotlinprogramming~10 mins

Number literal formats (underscore, hex, binary) in Kotlin - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Number literal formats (underscore, hex, binary)
Start
↓
Read number literal
↓
Check for underscores
| Yes↓
Ignore underscores
↓
Check for prefix
↓
Parse as hex
↓
Parse as decimal
↓
Store value
↓
End
The program reads a number literal, ignores underscores, detects if it's hex or binary by prefix, parses accordingly, and stores the value.
Execution Sample
Kotlin
val a = 1_000
val b = 0x1F
val c = 0b1010
Defines three numbers: decimal with underscores, hexadecimal, and binary.
Execution Table
StepLiteralUnderscores RemovedPrefix DetectedParsed ValueAction
11_0001000None1000Parsed as decimal
20x1F0x1F0x (hex)31Parsed as hexadecimal
30b10100b10100b (binary)10Parsed as binary
4----End of literals
💡 All literals processed and parsed to decimal values.
Variable Tracker
VariableInitialAfter Step 1After Step 2After Step 3Final
aundefined1000100010001000
bundefinedundefined313131
cundefinedundefinedundefined1010
Key Moments - 3 Insights
Why do underscores not affect the number's value?
Underscores are ignored during parsing as shown in step 1 of the execution_table where '1_000' becomes '1000'. They are just for readability.
How does the program know to parse '0x1F' as hexadecimal?
The prefix '0x' signals hexadecimal parsing, as shown in step 2 of the execution_table.
What happens if a number starts with '0b'?
It is parsed as binary, like in step 3 where '0b1010' becomes decimal 10.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the parsed value of '0x1F' at step 2?
A31
B1F
C0x1F
D15
💡 Hint
Check the 'Parsed Value' column in row for step 2.
At which step does the program remove underscores from the literal?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Underscores Removed' column in the execution_table.
If the literal was '0b1101_0010', how would the parsed value change compared to '0b1010'?
AIt would be 210
BIt would be 162
CIt would be 10
DIt would be 11010010
💡 Hint
Binary '0b11010010' equals decimal 210, but underscores are ignored, so check binary to decimal conversion.
Concept Snapshot
Number literals can include underscores (_) for readability.
Hexadecimal numbers start with '0x' and use digits 0-9 and letters A-F.
Binary numbers start with '0b' and use digits 0 and 1.
Underscores are ignored when parsing.
All literals convert to decimal values internally.
Full Transcript
This example shows how Kotlin reads number literals with underscores, hexadecimal, and binary formats. First, underscores are removed to simplify the number. Then, the program checks if the number starts with '0x' for hex or '0b' for binary. It parses accordingly: hex numbers convert from base 16, binary from base 2, and others as decimal. The parsed values are stored as normal decimal numbers. This helps write numbers clearly without changing their value.

Practice

(1/5)
1. Which of the following is a valid way to write the number one million in Kotlin using underscores for readability?
easy
A. 1__000000
B. 1000_000_
C. 10__00_000
D. 1_000_000

Solution

  1. Step 1: Understand underscore usage in Kotlin numbers

    Kotlin allows underscores between digits to improve readability, but they must not be consecutive, leading, or trailing.
  2. Step 2: Check each option for correct underscore placement

    1_000_000 uses single underscores separating groups of three digits correctly. A has consecutive underscores (invalid). B has trailing underscore (invalid). C has consecutive underscores (invalid).
  3. Final Answer:

    1_000_000 -> Option D
  4. Quick Check:

    Underscores separate digits properly = 1_000_000 [OK]
Hint: Use single underscores between digit groups only [OK]
Common Mistakes:
  • Using double underscores
  • Placing underscores at start or end
  • Trailing underscores
2. Which of the following Kotlin number literals is NOT valid syntax?
easy
A. 123_456_
B. 0b1010_1101
C. 0x1A3F
D. 987_654

Solution

  1. Step 1: Review Kotlin rules for underscores in number literals

    Underscores can be used between digits but not at the end or start of a number.
  2. Step 2: Check each option for trailing underscores

    123_456_ ends with an underscore, which is invalid syntax. Options A, B, and D are valid hexadecimal, binary, and decimal literals respectively.
  3. Final Answer:

    123_456_ -> Option A
  4. Quick Check:

    Trailing underscore invalid = 123_456_ [OK]
Hint: Never put underscore at the end of a number [OK]
Common Mistakes:
  • Trailing underscores in numbers
  • Confusing hex prefix 0x with decimal
  • Misplacing underscores in binary literals
3. What is the output of this Kotlin code?
val x = 0xF_A
val y = 0b1010_1010
println(x + y)
medium
A. 270
B. 250
C. 420
D. 280

Solution

  1. Step 1: Convert hexadecimal and binary literals to decimal

    0xF_A means hex FA: F=15, A=10, so 15*16 + 10 = 250. 0b1010_1010 is binary 10101010 which equals 128+32+8+2=170.
  2. Step 2: Add the decimal values

    250 + 170 = 420.
  3. Final Answer:

    420 -> Option C
  4. Quick Check:

    (0xFA + 0b10101010) = 420 [OK]
Hint: Convert hex and binary to decimal before adding [OK]
Common Mistakes:
  • Misreading hex digits
  • Incorrect binary to decimal conversion
  • Ignoring underscores in literals
4. Identify the error in this Kotlin code snippet:
val num = 0b1021_0010
medium
A. Underscores are not allowed in binary literals
B. Binary literals cannot contain digits other than 0 or 1
C. Binary literals must start with 0x
D. The number is too large for binary format

Solution

  1. Step 1: Understand valid digits in binary literals

    Binary literals can only contain digits 0 and 1, optionally separated by underscores.
  2. Step 2: Check the given literal for invalid digits

    The literal contains '2', which is invalid in binary. Underscores are allowed, and binary literals start with 0b, not 0x.
  3. Final Answer:

    Binary literals cannot contain digits other than 0 or 1 -> Option B
  4. Quick Check:

    Binary digits must be 0 or 1 only [OK]
Hint: Binary digits only 0 or 1, no other digits allowed [OK]
Common Mistakes:
  • Using digits other than 0 or 1 in binary
  • Confusing hex prefix 0x with binary 0b
  • Thinking underscores are disallowed in binary
5. You want to represent the decimal number 255 in Kotlin using a binary literal with underscores for readability. Which of the following is the best way to write it?
hard
A. 0b1111_1111
B. 0b11111111
C. 0b1111_1110
D. 0b1111_11112

Solution

  1. Step 1: Convert decimal 255 to binary

    255 in binary is 11111111 (eight ones).
  2. Step 2: Add underscores for readability

    Grouping bits in fours is common, so 1111_1111 is clearer and valid.
  3. Step 3: Check options for correctness

    0b1111_1111 correctly uses underscores and matches 255. 0b11111111 is correct but less readable. 0b1111_1110 is 254, and 0b1111_11112 has invalid digit '2'.
  4. Final Answer:

    0b1111_1111 -> Option A
  5. Quick Check:

    Binary 1111_1111 = 255 decimal [OK]
Hint: Group binary digits in fours with underscores [OK]
Common Mistakes:
  • Using wrong binary digits
  • Adding invalid digits like 2
  • Not grouping bits for readability
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard