Fixed-point converter

Encode a decimal value as a fixed-point word, or decode an existing bit pattern. All stored integers and conversions use exact integer arithmetic, including words wider than JavaScript’s native number precision.

Calculator

Representation and method

The represented value is the stored integer multiplied by 2−F. W includes every bit, including the sign bit for signed words. F may range from zero to W. Resolution is 2−F.

FormatMinimumMaximum
Signed−2W−F−12W−F−1 − 2−F
Unsigned02W−F − 2−F

For encoding, multiply the exact input decimal by 2F, apply the selected rounding rule, then apply the overflow policy. Saturation clamps to the signed or unsigned range. Wrapping keeps the least significant W bits. Decoding accepts an unsigned decimal integer, 0x hexadecimal, or 0b binary.

Worked example

With W = 16 and F = 12, −1.375 × 4096 = −5632. The two’s-complement word is 0xEA00. The resolution is exactly 0.000244140625.

Using the result in AMD Vitis HLS

For this convention, the corresponding integer width in ap_fixed<W,I> is I = W − F. Nearest with ties to even corresponds to convergent rounding. “Toward zero” and “toward −∞” are distinct for negative inputs. Explicitly select the HLS quantization and overflow modes to match this calculator. Scientific decimal input is accepted with an exponent within ±300.

Fixed-point conversion of -1.375 to the 16-bit hexadecimal word EA00
Encoding -1.375 in a signed 16-bit word with 12 fractional bits gives the stored integer -5632 and the bit pattern 0xEA00.

References