0Pricing
Swift Academy · Lesson

Numeric Literals and Underscores

Write binary, hex, and grouped literals for readability.

Numeric Literals and Underscores is a free Swift Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Numeric Literals

A literal is a value written directly in code, like 42 or 3.14. Swift supports decimal, binary, octal, and hexadecimal integer literals.

let dec = 42
let pi = 3.14
print(dec, pi)

Binary Literals

Prefix a literal with 0b to write it in binary. Each digit is a single bit.

let flags = 0b1010
print(flags)
print(0b1111)

Octal Literals

Prefix with 0o for octal (base 8). Each digit ranges 0 through 7.

let perm = 0o755
print(perm)
print(0o17)

Hexadecimal Literals

Prefix with 0x for hexadecimal (base 16). Digits go 0 through 9 then A through F.

let color = 0xFF
print(color)
print(0x10)

Same Value Many Bases

The same number can be written in different bases. They all store the identical integer value.

print(0b1111 == 15)
print(0o17 == 15)
print(0xF == 15)

Underscores for Grouping

You may insert _ anywhere inside a numeric literal to group digits for readability. Swift ignores the underscores.

let million = 1_000_000
print(million)

Underscores Are Cosmetic

Underscores have no effect on the value; 1_000 equals 1000. They simply make large numbers easier to read.

print(1_000 == 1000)
let bytes = 1_048_576
print(bytes)

Grouping in Other Bases

You can use underscores in binary, octal, and hex literals too, often to group bits or bytes.

let mask = 0b1010_0101
print(mask)
let addr = 0xFF_FF
print(addr)

Floating-Point Literals

Decimal floating-point literals always have a fractional part written out, and may use underscores in the whole or fractional portion.

let pay = 12_500.75
print(pay)

Scientific Notation

Use e for decimal exponent notation. 1.5e3 means 1.5 times ten to the power 3, which is 1500.

let big = 1.5e3
let small = 2.5e-2
print(big)
print(small)

Hexadecimal Floating-Point

Swift also supports hex floats using p for the power-of-two exponent, mainly for low-level precision needs.

let hexFloat = 0x1p4
print(hexFloat)

Quick Check

Recall the meaning of scientific notation in literals.

Recap: Numeric Literals and Underscores

You learned the literal prefixes 0b (binary), 0o (octal), and 0x (hex), that underscores like 1_000_000 group digits without changing the value, and that e notation such as 1.5e3 writes scaled decimals.

let total = 1_000_000
let hex = 0xFF
let bin = 0b1010
let sci = 1.5e3
print(total, hex, bin, sci)

Frequently asked questions

Is the “Numeric Literals and Underscores” lesson free?

Yes — the full text of “Numeric Literals and Underscores” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

What will I learn in “Numeric Literals and Underscores”?

Write binary, hex, and grouped literals for readability. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Numeric Literals and Underscores” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Integer Types and Their Ranges
  2. Floating-Point: Double vs Float
  3. Explicit Numeric Conversions
  4. Numeric Literals and Underscores
← Back to Swift Academy