Binary Converter
Converts whole numbers between decimal and binary, including negative numbers stored as two's complement, the way computers hold signed integers. Choose the word size (4 to 64 bits) to see the exact bit pattern, the range of values that size can hold, and the same number in hexadecimal and octal. In the other direction, paste a bit pattern to read it both as an unsigned number and as a signed two's-complement number.
- Only whole numbers are converted; fractions and floating-point formats are not covered.
Formula
Binary to decimal: Σ bitₖ × 2ᵏ; two's complement of −x in n bits = 2ⁿ − x; signed range −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1
- To convert decimal to binary, divide by 2 repeatedly and read the remainders from last to first: 42 → 21 r0 → 10 r1 → 5 r0 → 2 r1 → 1 r0 → 0 r1, so 42 = 101010.
- In two's complement, a negative number −x is stored as 2ⁿ − x. Equivalently, write x in binary, invert all the bits and add 1. The leftmost bit is 1 for every negative number.
- A pattern of n bits holds unsigned values 0 to 2ⁿ − 1, or signed values −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1. All arithmetic is exact, even for 64-bit values.
−42 as an 8-bit signed integer
Inputs
- Convert: toBinary
- Word size: 8
- Decimal number: -42
42 is 0010 1010. Inverting the bits gives 1101 0101 and adding 1 gives 1101 0110, which is 256 − 42 = 214 read as unsigned (hex D6).
Sources & references
Frequently asked questions
How do I convert decimal to binary?
Divide the number by 2 and write down the remainder; repeat with the quotient until it is 0, then read the remainders from bottom to top. 13 → 6 r1 → 3 r0 → 1 r1 → 0 r1, so 13 = 1101.
How do I convert binary to decimal?
Multiply each bit by its place value (1, 2, 4, 8, … from the right) and add. 1101 = 8 + 4 + 0 + 1 = 13.
What is two's complement?
The standard way computers store signed integers. Positive numbers are stored as normal; a negative number is stored by inverting the bits of its magnitude and adding 1. It lets the same adder circuit handle both positive and negative numbers.
What is the largest number in 8, 16 or 32 bits?
Unsigned: 255, 65,535 and 4,294,967,295. Signed: 127, 32,767 and 2,147,483,647, with minimums of −128, −32,768 and −2,147,483,648.
Related calculators
Byte Converter Calculator Converts between bits and bytes and every decimal and binary multiple of them: kilobytes, megabytes, gigabytes and terabytes in powers of 1000, and kibibytes, mebibytes, gibibytes and tebibytes in powers of 1024, up to yottabytes and yobibytes. It explains why a 1 TB drive shows up as about 931 GB in some operating systems, and turns network speeds in megabits into file sizes in megabytes. Each result is also given in bytes, bits and the best-fitting decimal and binary unit. Roman Numerals Converter Converts numbers to Roman numerals and Roman numerals back to numbers, with a place-by-place breakdown so you can see how each part is built. Numbers from 1 to 3,999 use the standard letters I, V, X, L, C, D and M; larger numbers up to 3,999,999 use the vinculum, a bar over the letters that multiplies them by 1,000. Roman input is checked against the standard form, so non-standard spellings such as IIII or IC are flagged. Scientific Notation Calculator Converts a number to scientific notation (a × 10ⁿ with 1 ≤ |a| < 10), e notation (3.2e5) and engineering notation with its SI prefix, and back to an ordinary decimal. Enter a plain decimal such as 0.000452 or 93,000,000, or a number already in scientific form such as 6.022e23 or 3*10^4, and optionally round the coefficient to a number of significant figures. Exponent Calculator Raises a number to a power: enter the base b and the exponent x to get a = bˣ, including negative exponents (reciprocals), fractional exponents (roots) and very large or small results in scientific notation. You can also work backwards: find the exponent that turns a base into a given result (a logarithm), or the base that gives a result for a given exponent (a root). The working shows what the exponent means, such as repeated multiplication or a root.