📖 Scenario: You are working on a simple calculator app that needs to handle different types of numbers. To do this, you must understand how to write numbers in Swift using different numeric literal formats.
🎯 Goal: Learn how to write and use different numeric literal formats in Swift, including decimal, binary, octal, and hexadecimal numbers.
📋 What You'll Learn
Create variables using decimal, binary, octal, and hexadecimal numeric literals
Use underscores to improve readability in large numbers
Print the values of these variables to see their decimal equivalents
💡 Why This Matters
🌍 Real World
Understanding numeric literals helps when working with low-level data, colors in hex, or binary flags in real apps.
💼 Career
Many programming jobs require reading and writing numbers in different formats, especially in systems programming, embedded devices, or graphics.
Progress0 / 4 steps
1
Create decimal and binary numeric literals
Create a variable called decimalNumber and set it to the decimal number 42. Then create a variable called binaryNumber and set it to the binary number 0b101010.
Swift
Hint
Use var to create variables. Binary numbers start with 0b.
2
Add octal and hexadecimal numeric literals
Add a variable called octalNumber and set it to the octal number 0o52. Also add a variable called hexNumber and set it to the hexadecimal number 0x2A.
Swift
Hint
Octal numbers start with 0o and hexadecimal numbers start with 0x.
3
Use underscores for readability
Create a variable called largeNumber and set it to the decimal number 1_000_000 using underscores to separate the thousands.
Swift
Hint
Use underscores _ inside numbers to make them easier to read.
4
Print all numeric variables
Print the values of decimalNumber, binaryNumber, octalNumber, hexNumber, and largeNumber each on a new line.
Swift
Hint
Use print() to display each variable's value on its own line.
Practice
(1/5)
1. Which of the following is a valid binary numeric literal in Swift?
easy
A. 0b1010
B. 0x1010
C. 1010b
D. 0o1010
Solution
Step 1: Understand binary literal format in Swift
Binary literals start with 0b followed by 0s and 1s.
Step 2: Check each option
0b1010 uses 0b prefix correctly. 0x1010 is hexadecimal, C is invalid, A is octal.
Final Answer:
0b1010 -> Option A
Quick Check:
Binary literals start with 0b = D [OK]
Hint: Binary literals always start with 0b prefix [OK]
Common Mistakes:
Confusing binary (0b) with hexadecimal (0x)
Placing the b suffix after the number
Using octal prefix 0o for binary
2. Which of the following is the correct way to write the number one million using underscores in Swift?
easy
A. 1_00_0000
B. 1000_000
C. 10_00_000
D. 1_000_000
Solution
Step 1: Understand underscore usage in numeric literals
Underscores can be placed anywhere between digits to improve readability.
Step 2: Check each option for correct grouping
1_000_000 groups digits in thousands correctly. Others have irregular grouping which is allowed but uncommon and confusing.
Final Answer:
1_000_000 -> Option D
Quick Check:
Use underscores to separate thousands = A [OK]
Hint: Use underscores every three digits for clarity [OK]
Common Mistakes:
Placing underscores at the start or end of number
Using inconsistent grouping that confuses readers
Thinking underscores change the value
3. What is the output of the following Swift code?
let number = 1.5e2
print(number)
medium
A. 150.0
B. 1.5e2
C. 15.0
D. SyntaxError
Solution
Step 1: Understand scientific notation in Swift
1.5e2 means 1.5 x 10², which equals 150.0.
Step 2: Evaluate the print output
Printing the number shows the decimal value 150.0, not the literal string.
Final Answer:
150.0 -> Option A
Quick Check:
1.5e2 = 150.0 [OK]
Hint: e2 means multiply by 10 squared [OK]
Common Mistakes:
Thinking print shows the literal string '1.5e2'
Confusing e2 with e-2 (which divides)
Assuming integer output instead of floating-point
4. Identify the error in this Swift numeric literal:
let value = 0b1021
medium
A. Should use 0x prefix for binary
B. Binary literals cannot contain digits other than 0 or 1
C. Missing underscore for readability
D. Floating-point literals cannot use binary format
Solution
Step 1: Check digits allowed in binary literals
Binary literals only allow digits 0 and 1 after the 0b prefix.
Step 2: Analyze the given literal
The literal contains digit 2, which is invalid for binary (only 0s and 1s allowed).
Final Answer:
Binary literals cannot contain digits other than 0 or 1 -> Option B
Quick Check:
Binary digits only 0 or 1 = C [OK]
Hint: Binary digits must be only 0 or 1 [OK]
Common Mistakes:
Thinking underscores are mandatory
Confusing binary prefix 0b with hexadecimal 0x
Assuming floating-point literals can be binary
5. You want to represent the decimal number 255 in Swift using hexadecimal and octal literals. Which pair of literals correctly represents 255?
hard
A. 0xFF and 0o255
B. 0x255 and 0o255
C. 0xFF and 0o377
D. 0x377 and 0oFF
Solution
Step 1: Convert decimal 255 to hexadecimal
255 decimal equals FF in hexadecimal, so 0xFF is correct.
Step 2: Convert decimal 255 to octal
255 decimal equals 377 in octal, so 0o377 is correct.