About this calculator
The same whole number can be written in different bases. People use base 10 (decimal); computers store numbers in base 2 (binary); programmers often write them in base 16 (hexadecimal), where each digit stands for exactly four bits, and sometimes in base 8 (octal), where each digit stands for three bits. This converter changes a whole number from any of these bases to all the others exactly — with no rounding, even for very long numbers.
The results are given in two separate groups, so they are not confused:
- Ordinary conversions (binary, octal, hexadecimal) — the number itself written in each base, with a minus sign when it is negative. For example −128 is −1000 0000 in binary and −80 in hexadecimal.
- Two’s-complement bit pattern — how a computer stores the number in a fixed number of bits (8, 16, 32 or 64). For example −128 in 8 bits is stored as 1000 0000, written 0x80 in hexadecimal. This pattern has no minus sign; the leftmost bit 1 marks a negative number. Read as an ordinary (unsigned) number, the same bits would mean 128, which is why the two must not be mixed up. The results show both readings of the pattern: for example 1010 1010 means −86 when read as signed and 170 when read as unsigned.
Formula
value = Σ di × bi
Two’s complement of a negative number in n bits: write the magnitude in n bits → invert every bit → add 1
(the same as 2n − magnitude) Signed range in n bits: −2n−1 to 2n−1 − 1
di = digit in position i (counting from 0 on the right), b = base. To convert to another base, divide repeatedly by the new base and read the remainders from last to first. One hexadecimal digit = 4 bits; one octal digit = 3 bits.
Worked example
- 214 (decimal) = 128 + 64 + 16 + 4 + 2 = 1101 0110 (binary) = 326 (octal) = D6 (hex).
- FF (hex) = 15 × 16 + 15 = 255.
- −1 in 8 bits: positive 1 = 0000 0001 → invert = 1111 1110 → add 1 = 1111 1111 = 0xFF.
- −42 in 16 bits: 42 = 0000 0000 0010 1010 → invert = 1111 1111 1101 0101 → add 1 = 1111 1111 1101 0110 = 0xFFD6 (check: 216 − 42 = 65,494).
- Signed ranges: 8 bits −128 to 127; 16 bits −32,768 to 32,767; 32 bits −2,147,483,648 to 2,147,483,647. A value outside the range is rejected, not cut off.
Assumptions and limitations
- Whole numbers (integers) only; fractions are not converted.
- Negative numbers are shown with a minus sign. When a number of bits is chosen, the value must be inside the signed range for that width (for 8 bits, −128 to 127); values outside it are rejected rather than truncated.
- Up to 512 digits are accepted; all arithmetic is exact.
References
- Positional number systems and two’s complement representation (e.g. Patterson and Hennessy, Computer Organization and Design).
