Bird
Raised Fist0
Swiftprogramming~10 mins

Numeric literal formats in Swift - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Numeric literal formats
Start
↓
Read Numeric Literal
↓
Check Format
↓
Decimal
↓
Convert to Number Value
↓
Use in Code
↓
End
The program reads a numeric literal, checks its format (decimal, binary, octal, or hexadecimal), converts it to a number, and then uses it in the code.
Execution Sample
Swift
let decimal = 1234
let binary = 0b1001
let octal = 0o17
let hex = 0x1F
This code defines four numbers using different numeric literal formats in Swift.
Execution Table
StepLiteralFormat DetectedConversion ResultVariable Assigned
11234Decimal1234decimal = 1234
20b1001Binary9binary = 9
30o17Octal15octal = 15
40x1FHexadecimal31hex = 31
5---Execution ends after all literals processed
💡 All numeric literals converted and assigned to variables.
Variable Tracker
VariableStartAfter AssignmentFinal
decimalundefined12341234
binaryundefined99
octalundefined1515
hexundefined3131
Key Moments - 3 Insights
Why does 0b1001 become 9 and not 1001?
Because 0b prefix means binary format, so 1001 in binary equals 9 in decimal (see execution_table step 2).
What does 0o17 represent?
0o means octal format; 17 in octal equals 15 in decimal (see execution_table step 3).
How is 0x1F interpreted?
0x means hexadecimal; 1F in hex equals 31 in decimal (see execution_table step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the decimal value of the binary literal 0b1001?
A9
B1001
C11
D8
💡 Hint
Check execution_table row 2 under Conversion Result.
At which step does the octal literal get converted?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look for 0o prefix in execution_table.
If the hex literal 0x1F was changed to 0x20, what would be the new decimal value?
A31
B30
C32
D16
💡 Hint
0x20 in hex equals 32 decimal; compare with execution_table step 4.
Concept Snapshot
Numeric literals in Swift can be decimal (e.g., 1234), binary (prefix 0b), octal (prefix 0o), or hexadecimal (prefix 0x).
Each format converts to a decimal number internally.
Use prefixes to specify the format.
Swift automatically converts these literals to their decimal values when assigned.
Full Transcript
This visual execution shows how Swift reads numeric literals in different formats: decimal, binary, octal, and hexadecimal. Each literal is detected by its prefix or lack thereof. The program converts each literal to its decimal equivalent and assigns it to a variable. For example, 0b1001 is binary and converts to 9 decimal. The octal literal 0o17 converts to 15 decimal. The hexadecimal 0x1F converts to 31 decimal. The variable tracker shows each variable's value after assignment. Key moments clarify why prefixes matter and how conversion works. The quiz tests understanding of these conversions and steps.

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

  1. Step 1: Understand binary literal format in Swift

    Binary literals start with 0b followed by 0s and 1s.
  2. Step 2: Check each option

    0b1010 uses 0b prefix correctly. 0x1010 is hexadecimal, C is invalid, A is octal.
  3. Final Answer:

    0b1010 -> Option A
  4. 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

  1. Step 1: Understand underscore usage in numeric literals

    Underscores can be placed anywhere between digits to improve readability.
  2. 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.
  3. Final Answer:

    1_000_000 -> Option D
  4. 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

  1. Step 1: Understand scientific notation in Swift

    1.5e2 means 1.5 x 10², which equals 150.0.
  2. Step 2: Evaluate the print output

    Printing the number shows the decimal value 150.0, not the literal string.
  3. Final Answer:

    150.0 -> Option A
  4. 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

  1. Step 1: Check digits allowed in binary literals

    Binary literals only allow digits 0 and 1 after the 0b prefix.
  2. Step 2: Analyze the given literal

    The literal contains digit 2, which is invalid for binary (only 0s and 1s allowed).
  3. Final Answer:

    Binary literals cannot contain digits other than 0 or 1 -> Option B
  4. 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

  1. Step 1: Convert decimal 255 to hexadecimal

    255 decimal equals FF in hexadecimal, so 0xFF is correct.
  2. Step 2: Convert decimal 255 to octal

    255 decimal equals 377 in octal, so 0o377 is correct.
  3. Final Answer:

    0xFF and 0o377 -> Option C
  4. Quick Check:

    255 decimal = 0xFF hex & 0o377 oct [OK]
Hint: Hex FF = 255 decimal; Oct 377 = 255 decimal [OK]
Common Mistakes:
  • Confusing octal digits with decimal digits
  • Using decimal digits after 0x prefix
  • Mixing up hexadecimal and octal prefixes
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