Number literal formats (underscore, hex, binary) in Kotlin - Time & Space Complexity
We want to understand how the time it takes to read and use number literals changes as the numbers get bigger or more complex.
How does the format of a number affect the work the program does?
Analyze the time complexity of the following code snippet.
val decimal = 1_000_000
val hex = 0xFF_EC_DE_5E
val binary = 0b11010010_01101001_10010100_10010010
fun printNumbers() {
println(decimal)
println(hex)
println(binary)
}
This code defines numbers using decimal with underscores, hexadecimal, and binary formats, then prints them.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading and converting the number literals once when the program starts.
- How many times: Each number literal is processed only once during initialization.
As the number literals get longer (more digits or bits), the time to read and convert them grows a little.
| Input Size (digits/bits) | Approx. Operations |
|---|---|
| 10 | About 10 steps to read and convert |
| 100 | About 100 steps to read and convert |
| 1000 | About 1000 steps to read and convert |
Pattern observation: The work grows directly with the length of the number literal.
Time Complexity: O(n)
This means the time to process number literals grows in a straight line with the number of digits or bits.
[X] Wrong: "Using underscores or hex/binary formats makes the program slower in a big way."
[OK] Correct: The program only reads and converts these numbers once, so the difference is very small and grows only with the length of the number.
Understanding how number formats affect processing helps you explain how programs handle data efficiently, a useful skill in many coding situations.
"What if we used very large number literals repeatedly inside a loop? How would the time complexity change?"
Practice
Solution
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.Step 2: Check each option for correct underscore placement
1_000_000uses single underscores separating groups of three digits correctly. A has consecutive underscores (invalid). B has trailing underscore (invalid). C has consecutive underscores (invalid).Final Answer:
1_000_000 -> Option DQuick Check:
Underscores separate digits properly = 1_000_000 [OK]
- Using double underscores
- Placing underscores at start or end
- Trailing underscores
Solution
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.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.Final Answer:
123_456_ -> Option AQuick Check:
Trailing underscore invalid = 123_456_ [OK]
- Trailing underscores in numbers
- Confusing hex prefix 0x with decimal
- Misplacing underscores in binary literals
val x = 0xF_A
val y = 0b1010_1010
println(x + y)
Solution
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.Step 2: Add the decimal values
250 + 170 = 420.Final Answer:
420 -> Option CQuick Check:
(0xFA + 0b10101010) = 420 [OK]
- Misreading hex digits
- Incorrect binary to decimal conversion
- Ignoring underscores in literals
val num = 0b1021_0010
Solution
Step 1: Understand valid digits in binary literals
Binary literals can only contain digits 0 and 1, optionally separated by underscores.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.Final Answer:
Binary literals cannot contain digits other than 0 or 1 -> Option BQuick Check:
Binary digits must be 0 or 1 only [OK]
- Using digits other than 0 or 1 in binary
- Confusing hex prefix 0x with binary 0b
- Thinking underscores are disallowed in binary
Solution
Step 1: Convert decimal 255 to binary
255 in binary is 11111111 (eight ones).Step 2: Add underscores for readability
Grouping bits in fours is common, so 1111_1111 is clearer and valid.Step 3: Check options for correctness
0b1111_1111correctly uses underscores and matches 255.0b11111111is correct but less readable.0b1111_1110is 254, and0b1111_11112has invalid digit '2'.Final Answer:
0b1111_1111 -> Option AQuick Check:
Binary 1111_1111 = 255 decimal [OK]
- Using wrong binary digits
- Adding invalid digits like 2
- Not grouping bits for readability
Ready to prove it?
Pass the test and this pattern moves unknown
