Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Numbers and Bases

An integer literal may use a radix prefix. Dewy supports integer numerals through base 16:

BasePrefixDigits
20b01
30t02
40q03
60s05
80o07
100d09
120z09, x, e
160x09, af

Alphabetic digits are case-insensitive in these integer forms. Decimal is the default, so 42 and 0d42 denote the same value.

0b10101010      # 170
0t121010        # 435
0q123           # 27
0s1432          # 380
0o1234567       # 342391
0xdeadbeef      # 3735928559

Underscores may group integer digits without changing the value:

let population = 1_000_000
let mask = 0b1111_0000

Packed Based Strings

A radix prefix followed by a quoted digit sequence produces exact packed data rather than an integer:

const program:array<uint8> = 0q"000000010002"
const header:array<uint8> = 0x"deadbeef"

Power-of-two bases have a compositional bit width and can therefore be packed directly:

BasePrefixBits per digit
20b1
40q2
80o3
160x4
320u5
640g6

Digits contribute bits from left to right, most-significant bit first. A final partial byte is padded with zero bits on the right. Whitespace and comments may separate digits.

Base 64 uses + or - for digit 62 and / or _ for digit 63. Trailing = characters are accepted as explicit padding and do not contribute bits. Unlike numeric underscores, _ inside a base-64 string is a digit.

Based strings for non-power-of-two bases are reserved until their sequence-width and composition rules are settled. The Reference gives the exact literal and packing rules.