-42 in decimal is -2A in hex. A 16-bit register stores it as FFD6 in hex, which reads as 65494 if the same bits are treated as unsigned.
16-bit pattern
16-bit two's complement
1
1
1
1
1
1
1
1
1
1
0
1
0
1
1
0
How it's calculated S
Read the decimal digits
4210=4×101+2×100=42
The minus sign is carried through unchanged.
Integer part: divide by 16 repeatedly
422=2×16+10(A)=0×16+2(2)
Reading the remainders from last to first gives the digits.
Store in 16 bits (two's complement)
−42≡65494=11111111110101102(mod216)
Read as unsigned the bits are 65494; read as signed the top bit counts −2¹⁵, giving -42.
About the binary, hex and decimal converter
A number written in base b is a sum of digits times powers of b, so 2A in hex is 2 × 16 + 10 = 42. To write a whole number in base b, the converter divides by b repeatedly and reads the remainders from last to first; for a fraction it multiplies by b repeatedly and takes each whole part as the next digit. It works with exact fractions, so it can tell whether the digits after the point end or repeat.
Programmers use it for memory addresses, color codes, file permissions and bit masks. The default, −42, is −2A in hex and 1111 1111 1101 0110 in 16-bit two's complement, which reads as 65,494 when the same bits are treated as unsigned. Decimal 0.1 never ends in binary: it is 0.0(0011), with the block 0011 repeating.
Two's-complement bits are shown for whole numbers at widths from 8 to 128 bits; a value outside the chosen width wraps to its low bits, with a warning.
How to convert between binary, decimal and hex
Each digit is worth its value times a power of the base, counting powers from 0 at the right. Binary 101010 is 32 + 8 + 2 = 42, and hex 2A is 2 × 16 + 10 = 42. To go the other way, divide by the base repeatedly and read the remainders from last to first.
Hex uses the digits 0–9 and then A–F for 10 to 15. One hex digit stands for exactly four bits, which is why a byte is written as two hex digits: 255 is FF, or 1111 1111.
Binary to decimal by place value
Write the powers of 2 above the bits, from 1 at the right: 128, 64, 32, 16, 8, 4, 2, 1. Then add the powers that sit over a 1.
For 1101 0110 that is 128 + 64 + 16 + 4 + 2 = 214. A full byte, 1111 1111, adds every power and gives 255.
Hex works the same way with powers of 16: 1, 16, 256, 4,096. Hex 3E8 is 3 × 256 + 14 × 16 + 8 = 1,000.
Decimal to binary or hex by repeated division
To convert 42 to binary, divide by 2 until the quotient reaches 0:
42 ÷ 2 = 21, remainder 0
21 ÷ 2 = 10, remainder 1
10 ÷ 2 = 5, remainder 0
5 ÷ 2 = 2, remainder 1
2 ÷ 2 = 1, remainder 0
1 ÷ 2 = 0, remainder 1
Reading the remainders from the last one up gives 101010. Dividing by 16 instead turns 1,000 into hex: 1,000 ÷ 16 = 62 r 8, 62 ÷ 16 = 3 r 14 (E), 3 ÷ 16 = 0 r 3, so 3E8.
Binary to hex and octal by grouping bits
Split the bits into groups of four from the right and convert each group: 1010 1111 is A and F, so AF. Pad the leftmost group with zeros, so 10 1010 becomes 0010 1010, which is 2A.
Octal uses groups of three: 101 010 is 5 and 2, so 42 is 52 in octal. Unix file permissions are written in octal because each digit holds three bits, for read, write and execute: 755 is 111 101 101, or rwxr-xr-x.
Binary, octal and hex conversion chart
Decimal
Binary
Octal
Hex
0
0
0
0
1
1
1
1
2
10
2
2
7
111
7
7
8
1000
10
8
10
1010
12
A
15
1111
17
F
16
10000
20
10
31
11111
37
1F
42
101010
52
2A
64
1000000
100
40
127
1111111
177
7F
128
10000000
200
80
255
11111111
377
FF
256
100000000
400
100
1024
10000000000
2000
400
Leading zeros don't change a value: 0010 1010 and 101010 are both 42, padded when a fixed width is needed.
Prefixes and digits above 9
0b, 0o and 0x mark binary, octal and hex, as in 0b101010, 0o52 and 0x2A.
No prefix means decimal unless you choose the source base. An unprefixed 101010 is read as one hundred one thousand and ten, which is 18A92 in hex.
Spaces and underscores are ignored, so 1111 1111 and 1111_1111 both work.
Bases up to 36 use letters after 9, through Z for 35. ZZ in base 36 is 35 × 36 + 35 = 1,295.
Two's complement is shown for whole numbers at widths of 8, 16, 32, 64 or 128 bits.
Fractions in other bases
Digits after the point use negative powers of the base. Binary 0.11 is 1/2 + 1/4 = 0.75, and hex 0x1.8 is 1 + 8/16 = 1.5.
To convert a decimal fraction, multiply by the base and peel off the whole part each time. For 0.75: 0.75 × 2 = 1.5 gives 1, then 0.5 × 2 = 1 gives 1, so 0.75 is 0.11 in binary.
A fraction ends in base b only if its denominator, in lowest terms, has no prime factors other than those of b. Decimal 0.1 is 1/10, and 5 divides no power of 2, so in binary 0.1 repeats forever: 0.0(0011).
That is why 0.1 + 0.2 gives 0.30000000000000004 in JavaScript or Python floats.
Negative numbers and two's complement
Computers store a negative whole number −x in w bits as 2ʷ − x. C has required this two's-complement representation since C23 (C23, §6.2.6.2 (ISO/IEC 9899:2024 working draft N3096)). In 16 bits, −42 becomes 65,536 − 42 = 65,494, which is FFD6 in hex and 1111 1111 1101 0110 in binary.
By hand, invert every bit of +42 and add 1: 0000 0000 0010 1010 inverts to 1111 1111 1101 0101, and adding 1 gives 1111 1111 1101 0110.
The same bits mean different numbers depending on how they are read. An 8-bit pattern runs from 0 to 255 unsigned but −128 to 127 signed, so 1000 0000 is 128 unsigned and −128 signed.
A value too big for the width keeps only its low bits, which is how overflow wraps around: 101010 needs 17 bits, so in 16 bits it is stored as 8A92.
Common mistakes
Reading remainders top down. The first remainder is the lowest digit. Reading 42's remainders downward gives 010101, which is 21.
Grouping bits from the left. 101010 must be grouped as 10 1010 to give 2A; grouping from the left gives A2.
Treating stored bits as the number. FFD6 is −42 only when read as a signed 16-bit value. Read unsigned, it is 65,494.
Expecting decimal fractions to end in binary. 0.1, 0.2 and 0.3 all repeat. Only fractions whose denominators are powers of 2, such as 0.5, 0.25 and 0.375, end.
Guessing the base from the digits. 101010 is a valid binary, decimal and hex number, with three different values. Write the prefix.
Checked against: Format = '000110011001'; a separate long-division script finds the remainder cycle of length 4 starting after the first digit
Hex fraction 0x1.8
Number
0x1.8
From base
Auto (prefix, else decimal)
To base
Decimal (10)
Two's-complement width
16-bit
Digit grouping
Groups of 4
Result
1.5
Fraction in the target base
Terminating
Binary
1.1
Checked against: Independent calculation
Questions
How do you convert binary to decimal?
Multiply each binary digit by 2 raised to its position, counting from 0 at the right, and add the results. 1111 1111 is 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255, and 101010 is 32 + 8 + 2 = 42. Digits after a binary point use negative powers, so 1.1 is 1 + 1/2 = 1.5.
How do you convert decimal to hexadecimal?
Divide by 16 repeatedly and write the remainders from last to first, using A–F for 10–15. For 255, 255 ÷ 16 = 15 remainder 15, then 15 ÷ 16 = 0 remainder 15, giving FF; for 1295 the remainders 15, 0 and 5 give 50F. Each hex digit stands for exactly 4 bits, which is why hex is used to write bytes: FF is 1111 1111.
What is two's complement?
Two's complement is how CPUs store signed integers: a negative number −x in w bits is stored as 2^w − x, so the top bit carries a weight of −2^(w−1). In 16 bits, −42 is 65,536 − 42 = 65,494, or FFD6 in hex. An 8-bit value runs from −128 to 127, so 128 wraps to −128. C23 (ISO/IEC 9899:2024) requires two's complement for signed integers.
Why is 0.1 not exact in binary?
A fraction ends in base b only if its denominator, in lowest terms, has no prime factors other than those of b. 0.1 is 1/10, and 10 = 2 × 5, so in binary it repeats forever: 0.000110011…, written 0.0(0011). That is why 0.1 + 0.2 gives 0.30000000000000004 in IEEE 754 double precision, the number type behind JavaScript numbers and Python floats.
What is octal used for?
Octal (base 8) groups binary digits in threes, and its main use today is Unix file permissions: each digit adds read (4), write (2) and execute (1) for the owner, group and others, so chmod 755 means rwxr-xr-x. Decimal 255 is 377 in octal. Python 3 and JavaScript write octal literals as 0o755; C writes them with a leading zero, 0755.
This calculator includes 7 worked examples with independently sourced answers. They run in the test suite, and you can run them here too.
Put this calculator on your site
Paste this where you want the calculator to appear. It is free to use, it stays in step with corrections made here, and the credit line below it links back so your readers can find the full version.