Integers
All integers are encoded using the LEB128 variable-length integer encoding, in either unsigned or signed variant.
Unsigned integers are encoded in unsigned LEB128 format.
As an additional constraint, the total number of bytes encoding a value of type \(\href{../syntax/values.html#syntax-int}{\mathit{u}N}\) must not exceed \(\mathrm{ceil}(N/7)\) bytes.
\[\begin{split}\begin{array}{llclll@{\qquad}l}
\def\mathdef950#1{{}}\mathdef950{unsigned integer} & \href{../binary/values.html#binary-int}{\def\mathdef951#1{{\mathtt{u}#1}}\mathdef951{N}} &::=&
n{:}\href{../binary/values.html#binary-byte}{\mathtt{byte}} &\Rightarrow& n & (\mathrel{\mbox{if}} n < 2^7 \wedge n < 2^N) \\ &&|&
n{:}\href{../binary/values.html#binary-byte}{\mathtt{byte}}~~m{:}\def\mathdef991#1{{\mathtt{u}#1}}\mathdef991{(N\mathtt{-7})} &\Rightarrow&
2^7\cdot m + (n-2^7) & (\mathrel{\mbox{if}} n \geq 2^7 \wedge N > 7) \\
\end{array}\end{split}\]
Signed integers are encoded in signed LEB128 format, which uses a two’s complement representation.
As an additional constraint, the total number of bytes encoding a value of type \(\href{../syntax/values.html#syntax-int}{\mathit{s}N}\) must not exceed \(\mathrm{ceil}(N/7)\) bytes.
\[\begin{split}\begin{array}{llclll@{\qquad}l}
\def\mathdef950#1{{}}\mathdef950{signed integer} & \href{../binary/values.html#binary-int}{\def\mathdef957#1{{\mathtt{s}#1}}\mathdef957{N}} &::=&
n{:}\href{../binary/values.html#binary-byte}{\mathtt{byte}} &\Rightarrow& n & (\mathrel{\mbox{if}} n < 2^6 \wedge n < 2^{N-1}) \\ &&|&
n{:}\href{../binary/values.html#binary-byte}{\mathtt{byte}} &\Rightarrow& n-2^7 & (\mathrel{\mbox{if}} 2^6 \leq n < 2^7 \wedge n \geq 2^7-2^{N-1}) \\ &&|&
n{:}\href{../binary/values.html#binary-byte}{\mathtt{byte}}~~m{:}\def\mathdef992#1{{\mathtt{s}#1}}\mathdef992{(N\mathtt{-7})} &\Rightarrow&
2^7\cdot m + (n-2^7) & (\mathrel{\mbox{if}} n \geq 2^7 \wedge N > 7) \\
\end{array}\end{split}\]
Uninterpreted integers are encoded as signed integers.
\[\begin{array}{llclll@{\qquad\qquad}l}
\def\mathdef950#1{{}}\mathdef950{uninterpreted integer} & \href{../binary/values.html#binary-int}{\def\mathdef962#1{{\mathtt{i}#1}}\mathdef962{N}} &::=&
n{:}\href{../binary/values.html#binary-int}{\def\mathdef957#1{{\mathtt{s}#1}}\mathdef957{N}} &\Rightarrow& i & (\mathrel{\mbox{if}} n = \href{../exec/numerics.html#aux-signed}{\mathrm{signed}}_{\href{../syntax/values.html#syntax-int}{\mathit{i}N}}(i))
\end{array}\]
Note
The side conditions \(N > 7\) in the productions for non-terminal bytes of the \(\def\mathdef993#1{{\mathit{u#1}}}\mathdef993{}\) and \(\def\mathdef994#1{{\mathit{s#1}}}\mathdef994{}\) encodings restrict the encoding’s length.
However, “trailing zeros” are still allowed within these bounds.
For example, \(\def\mathdef995#1{\mathtt{0x#1}}\mathdef995{03}\) and \(\def\mathdef996#1{\mathtt{0x#1}}\mathdef996{83}~\def\mathdef997#1{\mathtt{0x#1}}\mathdef997{00}\) are both well-formed encodings for the value \(3\) as a \(\href{../syntax/values.html#syntax-int}{\mathit{u8}}\).
Similarly, either of \(\def\mathdef998#1{\mathtt{0x#1}}\mathdef998{7e}\) and \(\def\mathdef999#1{\mathtt{0x#1}}\mathdef999{FE}~\def\mathdef1000#1{\mathtt{0x#1}}\mathdef1000{7F}\) and \(\def\mathdef1001#1{\mathtt{0x#1}}\mathdef1001{FE}~\def\mathdef1002#1{\mathtt{0x#1}}\mathdef1002{FF}~\def\mathdef1003#1{\mathtt{0x#1}}\mathdef1003{7F}\) are well-formed encodings of the value \(-2\) as a \(\href{../syntax/values.html#syntax-int}{\mathit{s16}}\).
The side conditions on the value \(n\) of terminal bytes further enforce that
any unused bits in these bytes must be \(0\) for positive values and \(1\) for negative ones.
For example, \(\def\mathdef1004#1{\mathtt{0x#1}}\mathdef1004{83}~\def\mathdef1005#1{\mathtt{0x#1}}\mathdef1005{10}\) is malformed as a \(\href{../syntax/values.html#syntax-int}{\mathit{u8}}\) encoding.
Similarly, both \(\def\mathdef1006#1{\mathtt{0x#1}}\mathdef1006{83}~\def\mathdef1007#1{\mathtt{0x#1}}\mathdef1007{3E}\) and \(\def\mathdef1008#1{\mathtt{0x#1}}\mathdef1008{FF}~\def\mathdef1009#1{\mathtt{0x#1}}\mathdef1009{7B}\) are malformed as \(\href{../syntax/values.html#syntax-int}{\mathit{s8}}\) encodings.
Floating-Point
Floating-point values are encoded directly by their IEEE 754-2019 (Section 3.4) bit pattern in little endian byte order:
\[\begin{split}\begin{array}{llclll@{\qquad\qquad}l}
\def\mathdef950#1{{}}\mathdef950{floating-point value} & \href{../binary/values.html#binary-float}{\def\mathdef965#1{{\mathtt{f}#1}}\mathdef965{N}} &::=&
b^\ast{:\,}\href{../binary/values.html#binary-byte}{\mathtt{byte}}^{N/8} &\Rightarrow& \href{../exec/numerics.html#aux-bytes}{\mathrm{bytes}}_{\href{../syntax/values.html#syntax-float}{\mathit{f}N}}^{-1}(b^\ast) \\
\end{array}\end{split}\]
Names
Names are encoded as a vector of bytes containing the Unicode (Section 3.9) UTF-8 encoding of the name’s character sequence.
\[\begin{split}\begin{array}{llclllll}
\def\mathdef950#1{{}}\mathdef950{name} & \href{../binary/values.html#binary-name}{\mathtt{name}} &::=&
b^\ast{:}\href{../binary/conventions.html#binary-vec}{\mathtt{vec}}(\href{../binary/values.html#binary-byte}{\mathtt{byte}}) &\Rightarrow& \href{../syntax/values.html#syntax-name}{\mathit{name}}
&& (\mathrel{\mbox{if}} \href{../binary/values.html#binary-utf8}{\mathrm{utf8}}(\href{../syntax/values.html#syntax-name}{\mathit{name}}) = b^\ast) \\
\end{array}\end{split}\]
The auxiliary \(\href{../binary/values.html#binary-utf8}{\mathrm{utf8}}\) function expressing this encoding is defined as follows:
\[\begin{split}\begin{array}{@{}l@{}}
\begin{array}{@{}lcl@{\qquad}l@{}}
\href{../binary/values.html#binary-utf8}{\mathrm{utf8}}(c^\ast) &=& (\href{../binary/values.html#binary-utf8}{\mathrm{utf8}}(c))^\ast \\[1ex]
\href{../binary/values.html#binary-utf8}{\mathrm{utf8}}(c) &=& b &
(\begin{array}[t]{@{}c@{~}l@{}}
\mathrel{\mbox{if}} & c < \def\mathdef1010#1{\mathrm{U{+}#1}}\mathdef1010{80} \\
\wedge & c = b) \\
\end{array} \\
\href{../binary/values.html#binary-utf8}{\mathrm{utf8}}(c) &=& b_1~b_2 &
(\begin{array}[t]{@{}c@{~}l@{}}
\mathrel{\mbox{if}} & \def\mathdef1011#1{\mathrm{U{+}#1}}\mathdef1011{80} \leq c < \def\mathdef1012#1{\mathrm{U{+}#1}}\mathdef1012{800} \\
\wedge & c = 2^6(b_1-\def\mathdef1013#1{\mathtt{0x#1}}\mathdef1013{C0})+(b_2-\def\mathdef1014#1{\mathtt{0x#1}}\mathdef1014{80})) \\
\end{array} \\
\href{../binary/values.html#binary-utf8}{\mathrm{utf8}}(c) &=& b_1~b_2~b_3 &
(\begin{array}[t]{@{}c@{~}l@{}}
\mathrel{\mbox{if}} & \def\mathdef1015#1{\mathrm{U{+}#1}}\mathdef1015{800} \leq c < \def\mathdef1016#1{\mathrm{U{+}#1}}\mathdef1016{D800} \vee \def\mathdef1017#1{\mathrm{U{+}#1}}\mathdef1017{E000} \leq c < \def\mathdef1018#1{\mathrm{U{+}#1}}\mathdef1018{10000} \\
\wedge & c = 2^{12}(b_1-\def\mathdef1019#1{\mathtt{0x#1}}\mathdef1019{E0})+2^6(b_2-\def\mathdef1020#1{\mathtt{0x#1}}\mathdef1020{80})+(b_3-\def\mathdef1021#1{\mathtt{0x#1}}\mathdef1021{80})) \\
\end{array} \\
\href{../binary/values.html#binary-utf8}{\mathrm{utf8}}(c) &=& b_1~b_2~b_3~b_4 &
(\begin{array}[t]{@{}c@{~}l@{}}
\mathrel{\mbox{if}} & \def\mathdef1022#1{\mathrm{U{+}#1}}\mathdef1022{10000} \leq c < \def\mathdef1023#1{\mathrm{U{+}#1}}\mathdef1023{110000} \\
\wedge & c = 2^{18}(b_1-\def\mathdef1024#1{\mathtt{0x#1}}\mathdef1024{F0})+2^{12}(b_2-\def\mathdef1025#1{\mathtt{0x#1}}\mathdef1025{80})+2^6(b_3-\def\mathdef1026#1{\mathtt{0x#1}}\mathdef1026{80})+(b_4-\def\mathdef1027#1{\mathtt{0x#1}}\mathdef1027{80})) \\
\end{array} \\
\end{array} \\
\mathrel{\mbox{where}} b_2, b_3, b_4 < \def\mathdef1028#1{\mathtt{0x#1}}\mathdef1028{C0} \\
\end{array}\end{split}\]
Note
Unlike in some other formats, name strings are not 0-terminated.