Number format converter: two's complement, IEEE 754 and Q-format
Integers (two's complement, 8–64 bit), IEEE 754 floats and Q-format fixed point
This number format converter gives exact conversions for firmware work on one page: integers in two's complement from 8 to 64 bits, IEEE 754 single and double precision floating point, and Q-format fixed point. Type a value, choose the format, and read the hexadecimal, binary and decimal forms, the bit fields, the value actually stored and the rounding error. Results are copyable text with plain ASCII signs, so they paste straight into code.
Two's complement integers, 8 to 64 bits
Choose the width and whether the number is signed. A decimal must fit the range, and hexadecimal, binary and octal input is a bit pattern with no sign. A 0x, 0b or 0o prefix overrides the chosen base, and spaces and underscores are ignored. Conversions are exact up to 64 bits.
unsigned: 0 … 2ⁿ − 1
signed: −2ⁿ⁻¹ … 2ⁿ⁻¹ − 1
xsigned = xunsigned − 2ⁿ if the top bit is 1
IEEE 754 floating point
A float32 has 1 sign bit, 8 exponent bits with bias 127 and 23 mantissa bits; a float64 has 1, 11 with bias 1023, and 52. The converter shows the hexadecimal pattern, the three bit fields, the stored value to 17 significant digits, the exact error between what was stored and what you typed, and whether the number is normal, subnormal, zero, infinity or NaN. You can also enter a bit pattern with 0x and read the value back.
x = (−1)ˢ × 1.m × 2^(e − bias) (normal)
x = (−1)ˢ × 0.m × 2^(1 − bias) (subnormal)
Q-format fixed point
Choose the total bits, the fractional bits and the signedness. The code is the value times two to the number of fractional bits, rounded to nearest with ties away from zero. A value outside the range saturates, and the check fails with the saturated code and what would fit, such as more total bits or an unsigned format.
code = round(x × 2ⁿ)
resolution = 2⁻ⁿ
Worked examples
- −100 as 16-bit two's complement is 0xFF9C, which is 65436 read as unsigned; in binary 1111 1111 1001 1100, with the bytes FF 9C big-endian and 9C FF little-endian.
- 3.14 as float32 is 0x4048F5C3: sign 0 (positive), exponent 10000000 = 128, unbiased +1. The stored value is 3.1400001049041748, an error of +1.049e-7.
- 0.3 as Q15 (16 bits, 15 fractional, signed) has the code 9830 (0x2666), represents 0.29998779296875, with a quantization error of -1.2207e-5 and a resolution of 3.0518e-5.
- 300 does not fit 8-bit unsigned (0 to 255): it needs 9 bits unsigned, 10 bits signed.
Limits
Input is limited to 80 characters, and a decimal exponent beyond ±1000 is refused: a float64 only reaches about 1e308. Where a value does not fit the chosen format the page says so and offers the settings that would fit, instead of showing a wrapped number that would look real.