Base Converter & Two's Complement

Convert between decimal, hex, binary and octal at any bit width — signed or unsigned — and copy ready-made Verilog, VHDL and C literals.

Verilog—
VHDL—
C / C++—
Set bits—
Range at this width—

This free number base converter translates between binary, decimal, hexadecimal and octal — binary to decimal, decimal to hex, hex to binary and back — at 8, 16, 32 or 64 bits. It is also a two's complement calculator: switch between signed and unsigned to see how the same bit pattern reads either way. Ready-to-paste Verilog, VHDL and C literals are generated for every value.

How to use the converter

Type in any field and the others update. Hex, binary and octal are treated as raw bit patterns at the chosen width; the decimal field shows that pattern as a signed (two's complement) or unsigned number. Prefixes like 0x and 0b, spaces and underscores are ignored, so you can paste values straight from code or a waveform viewer.

Hex, binary and decimal chart (0–15)

Each hex digit is exactly four binary bits, so longer values convert one digit at a time.

DecimalHexBinaryOctal
0000000
1100011
2200102
3300113
4401004
5501015
6601106
7701117
88100010
99100111
10A101012
11B101113
12C110014
13D110115
14E111016
15F111117

Integer ranges by bit width

WidthUnsigned rangeSigned (two's complement) range
8-bit0 to 255-128 to 127
16-bit0 to 65,535-32,768 to 32,767
32-bit0 to 4,294,967,295-2,147,483,648 to 2,147,483,647
64-bit0 to 18,446,744,073,709,551,615-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Two's complement explained

To find the two's complement representation of a negative number:

  1. Write the positive value in binary: 1234 = 0000 0100 1101 0010 (16-bit).
  2. Invert every bit: 1111 1011 0010 1101.
  3. Add 1: 1111 1011 0010 1110 = 0xFB2E, which is −1234.

Reading it back, a set MSB means the value is negative: subtract 2ᴺ from the unsigned value. 0xFB2E = 64,302 unsigned, and 64,302 − 65,536 = −1234.

Sign extension

When widening a signed value, copy the MSB into the new upper bits: 8-bit 0xF6 (−10) becomes 16-bit 0xFFF6. Zero-extending it instead (0x00F6) turns it into +246 — a classic RTL bug. In Verilog, declare both operands signed or use $signed() so the tools sign-extend for you.

Frequently asked questions

How do I convert binary to decimal?

Multiply each bit by its place value — 1, 2, 4, 8, 16 and so on from the right — and add them up. 1011 0101 is 128 + 32 + 16 + 4 + 1 = 181. Type the binary value in the converter and it shows the decimal, hex and octal values instantly.

How do I convert decimal to hex?

Divide by 16 repeatedly and write down the remainders, last one first, using A–F for 10–15. 181 ÷ 16 = 11 remainder 5, and 11 is B, so 181 = 0xB5. Or type 181 in the decimal field above.

How do I calculate the two's complement of a number?

Write the positive number in binary at the chosen width, invert every bit, then add 1. For −5 in 8 bits: 5 = 0000 0101, inverted = 1111 1010, plus 1 = 1111 1011 (0xFB). Set the converter to signed and type −5 to check.

What is two's complement?

Two's complement is how almost all processors and digital logic represent signed integers. The most significant bit has a negative weight: in 8 bits, 1000 0000 is −128 and 1111 1111 is −1. To negate a number, invert all bits and add 1.

How do I convert hex to binary?

Each hex digit maps to exactly four bits: 0 = 0000 through F = 1111. So 0xA5 is 1010 0101. That one-to-one mapping is why hex is the standard shorthand for binary in hardware and software.

What range can an N-bit number hold?

Unsigned: 0 to 2ᴺ − 1 (8 bits: 0–255, 16 bits: 0–65,535, 32 bits: 0–4,294,967,295). Signed two's complement: −2ᴺ⁻¹ to 2ᴺ⁻¹ − 1 (8 bits: −128 to 127, 16 bits: −32,768 to 32,767).

How do I write a hex constant in Verilog or VHDL?

In Verilog/SystemVerilog use a sized literal such as 8'hA5 or 16'b1010_0101_0000_1111. In VHDL use x"A5" for a std_logic_vector, or to_signed / to_unsigned for numeric types. The converter generates these for the value you enter.

Standard formulas, tested results

This calculator uses standard engineering formulas, shown in the method section above, and is checked against hand-worked examples. How the calculators are made.

Related calculators