Base converter and two's complement

Enter a number, choose its base and a bit width, and see it in binary, octal, decimal and hexadecimal at once. Negative numbers are encoded in two's complement, and the same bit pattern is shown read as unsigned and as signed, along with the representable ranges, the bitwise inverse and the negation. It is a quick way to check number-representation exercises after working them by hand.

Inputs

Results

Binary
–
Hexadecimal
–
Octal
–
Decimal (unsigned)
–
Decimal (signed, two's complement)
–

Negation

All bits inverted (one's complement)
–
Inverted plus 1 (two's complement)
–

Range at this width

Unsigned range
–
Signed range
–

Negative numbers are encoded in two's complement at the selected width, and spaces and underscores in the input are ignored.

Converting to binary, octal and hex

To convert a decimal number to binary, divide by 2 repeatedly and read the remainders from the last to the first. To go back, multiply each binary digit by its weight of 1, 2, 4, 8 and so on and add them up. Because 16 is 2 to the 4th power, splitting a binary number into groups of four bits from the right gives one hexadecimal digit (0–9, A–F) per group; octal works the same way with groups of three. The default of −5 at 8 bits is 1111 1011. 1111 is F and 1011 is B, so it is 0xFB in hex, and read as unsigned it is 128 + 64 + 32 + 16 + 8 + 2 + 1 = 251. Grouped in threes as 011 111 011, it is 373 in octal. The tool groups binary and hex output in fours so you can see how the two line up.

Two's complement and why “invert and add 1” works

In n-bit two's complement, a negative number −x is stored as the bit pattern for 2ⁿ − x. Inverting every bit (the one's complement) gives (2ⁿ − 1) − x, so adding 1 gives exactly 2ⁿ − x. That is why inverting and adding 1 negates a number. 5 at 8 bits is 0000 0101; inverted it is 1111 1010, and adding 1 gives 1111 1011, matching 256 − 5 = 251. An adder that works modulo 2ⁿ gets the same result from adding 2ⁿ − x as from subtracting x, so subtraction can be done with an adder, and one circuit handles both unsigned and signed numbers. For the default of −5 (1111 1011), the Negation panel shows the inverse 0000 0100 and, after adding 1, 0000 0101 (+5).

Ranges and why −128 negates to itself

n bits hold unsigned integers from 0 to 2ⁿ − 1 and two's complement signed integers from −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1. That is 0 to 255 and −128 to 127 at 8 bits, 0 to 65535 and −32768 to 32767 at 16 bits, and 0 to 4294967295 and −2147483648 to 2147483647 at 32 bits. Zero sits on the side where the top bit is 0, so there is one fewer positive number than negative numbers, and the most negative value cannot be negated. Enter −128 at 8 bits (1000 0000): inverting gives 0111 1111, adding 1 gives 1000 0000 again, and the tool shows −128 coming back unchanged. This asymmetry is why taking the absolute value of the minimum integer, or multiplying it by −1, overflows in a program.

Two readings of one bit pattern, and sign extension

A bit pattern carries no sign of its own; its value depends on whether you read it as unsigned or as two's complement. If the top bit is 0 the two readings agree, and if it is 1 the signed value is the unsigned value minus 2ⁿ. 1111 1011 is 251 unsigned and 251 − 256 = −5 signed. To widen a signed number, copy the top bit into all the new upper bits (sign extension): −5 becomes FFFB at 16 bits and FFFF FFFB in hex at 32 bits, still −5. Filling the upper bits with zeros instead (zero extension) gives 00FB, which is 251. In the tool, keep decimal −5 and change the width to see sign extension, or enter hex FB and change the width to see zero extension.

Overflow in addition, and how it shows up in exercises

An n-bit adder produces the sum modulo 2ⁿ and drops any carry beyond the top bit. For unsigned addition, overflow means a carry out of the most significant bit; for signed addition, it means two positive numbers giving a negative sum or two negative numbers giving a positive one. Enter 128, the result of 127 + 1, at 8 bits: the pattern is 1000 0000, which is 128 unsigned but −128 signed. Enter decimal −1 at 32 bits and the unsigned value is 4294967295, the same as computing 0 − 1 with a 32-bit unsigned int in C. Base conversion, negative numbers in two's complement, the range of n bits and shifts are standard material in introductory computer-architecture courses and IT certification exams. When a value is out of range, the tool keeps only the low bits and shows a warning, just as a register would.

Practice computer arithmetic questions

What is the result of arithmetic right-shifting the 8-bit signed integer −8 (1111 1000) by one bit?

  1. A124 (0111 1100)
  2. B−16 (1111 0000)
  3. C−4 (1111 1100)
Show answer and explanation

Answer: C. −4 (1111 1100)

An arithmetic right shift copies the sign bit into the vacated top bit, so 1111 1000 becomes 1111 1100, which is −4 in two's complement. Even for negative numbers, shifting right by one divides by 2. A logical right shift, which inserts 0, gives 0111 1100 = 124, changing the sign. −16 is the result of shifting left by one. Note also that in C, which kind of shift applies to negative signed integers is implementation-defined.

In C, what is the result of subtracting 1 from 0 in a 32-bit unsigned integer (unsigned int)?

  1. A−1
  2. B4294967295
  3. CIt stays at 0
Show answer and explanation

Answer: B. 4294967295

Unsigned integers have no negatives, and C defines their results modulo 2³² (wrapping around). 0 − 1 becomes 2³² − 1 = 4294967295, all ones in binary. That is why a loop that counts an unsigned variable down with the condition i >= 0 never ends: the condition is always true. Unsigned types cannot represent −1, and they do not saturate at 0.

How is overflow detected when adding two unsigned integers?

  1. ACheck whether the result's most significant bit became 1
  2. BCheck whether the two inputs' most significant bits are equal
  3. CCheck whether a carry came out of the most significant bit
Show answer and explanation

Answer: C. Check whether a carry came out of the most significant bit

In unsigned addition, a carry out of the most significant bit means the result exceeded the largest value the bit width can hold. CPUs record this carry in a carry flag, so checking it detects overflow. A result whose top bit is 1 is merely a large number, not an overflow. Comparing the carry into and out of the top bit is the method for detecting signed overflow.

FAQ

What is −5 in 8-bit two's complement?

5 at 8 bits is 0000 0101. Invert every bit to get 1111 1010, then add 1 to get 1111 1011, which is 0xFB in hex and 251 when read as unsigned. Computing 256 − 5 = 251 gives the same pattern.

What is a quick way to convert binary to hex?

Split the bits into groups of four from the right and replace each group with one digit from 0–9 and A–F. For example, 1011 0110 is B (1011) and 6 (0110), so B6. If the number of bits is not a multiple of four, pad the left with zeros first. For octal, use groups of three.

Why is the 8-bit signed range −128 to 127?

Of the 256 patterns, the 128 with the top bit set are the negative numbers −128 to −1, and the 128 with the top bit clear are zero and the positive numbers 0 to 127. Zero uses one of the non-negative slots, so +128 cannot be represented. In general n bits cover −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1.

What is the difference between one's complement and two's complement?

One's complement just inverts every bit, which leaves two zeros, +0 (0000 0000) and −0 (1111 1111), and addition needs an end-around carry that feeds the carry out back into the lowest bit. Two's complement inverts and then adds 1, so there is only one zero and the same adder works for unsigned and signed numbers. That is why mainstream computers today use two's complement for integers.

What is hex FF as unsigned and as signed?

At 8 bits it is 255 unsigned and −1 in signed two's complement. Enter the same FF at 16 bits and it becomes 00FF, which is 255 even when read as signed. The value depends on both the bit width and whether you read it as signed.

Related terms

Two's complement Sign extension Arithmetic shift Full adder Saturating arithmetic Condition code Immediate operand INT8

Related calculators

Semiconductor, Electronics and Computing Calculators →