Two's Complement Calculator

Convert between decimal and two's complement binary for 8, 16, 32, 64-bit widths. View hex, octal, range, overflow detection, and arithmetic operations.

Range: -128 to 127
Decimal
42
Signed decimal value
Two's Complement
00101010
8-bit two's complement binary
Hexadecimal
0x2A
Hex representation of the binary
Unsigned Value
42
Interpreting the same bits as unsigned
Octal
0o52
Octal representation
Sign Bit
0 (non-negative)
MSB indicates sign in two's complement
Overflow?
✓ No overflow
Valid range: -128 to 127

Bit Layout

0
0
1
0
1
0
1
0

Representation Comparison

SystemBinaryNotes
Two's Complement00101010Standard in modern CPUs
One's Complement00101010Negate by flipping bits
Sign-Magnitude00101010MSB = sign, rest = magnitude
Hexadecimal0x2A4 bits per hex digit
Octal0o523 bits per octal digit

Two's Complement Ranges

BitsMinimumMaximumValues
8-bit-128127256
16-bit-327683276765,536
32-bit-214748364821474836474,294,967,296
64-bit-922337203685477580892233720368547758072⁶⁴ ≈ 1.84×10¹⁹

Negation Steps (how to negate in two's complement)

1. Start with binary: 00101010

2. Flip all bits:    11010101

3. Add 1:          11010110

4. Result (−42): 11010110 = -42 in decimal

Planning notes, formulas, and examples

About the Two's Complement Calculator

Two's complement is the standard representation for signed integers in virtually every modern computer. In an 8-bit system, positive numbers range from 0 to 127 (MSB = 0), while negative numbers range from −1 (11111111) to −128 (10000000). To negate a number, you flip all bits and add 1 — this elegant rule eliminates the negative-zero problem of one's complement and simplifies hardware implementation.

Two's complement has one asymmetry: the range is −2ⁿ⁻¹ to +2ⁿ⁻¹ − 1, meaning there is one more negative value than positive. For 8 bits, that is −128 to +127. Negating −128 would give +128, which overflows — a common source of bugs. Understanding overflow behavior is crucial for writing robust code in languages like C, C++, Java, and Rust that use fixed-width integers.

This calculator converts between decimal and two's complement binary for 8, 16, 32, and 64-bit widths. It shows binary, hexadecimal, and octal output, detects overflow, performs addition and subtraction in the selected bit width, and compares the representation with one's complement and sign-magnitude. The bit-level visualization and step-by-step negation breakdown make the underlying algorithm crystal clear.

When This Page Helps

Two's complement is the foundation of integer arithmetic in every modern programming language. Bugs involving integer overflow, sign extension, and bit manipulation are among the most common problems in systems programming. This page keeps the conversions, bit layout, overflow checks, and arithmetic behavior together so you can inspect the representation instead of guessing at the bit pattern.

How to Use the Inputs

  1. Select the conversion mode: Decimal to Binary or Binary to Decimal
  2. Choose the bit width (8, 16, 32, or 64 bits)
  3. Enter a number or click a preset for common examples
  4. Optionally select an arithmetic operation (add, subtract, negate) and enter operand B
  5. View the bit layout with color-coded sign bit
  6. Compare representations: two's complement, one's complement, sign-magnitude
  7. Check the range table and negation walkthrough for reference
Formula used
Two's complement negation: −x = flip all bits + 1 = 2ⁿ − x. Range for n bits: −2ⁿ⁻¹ to +2ⁿ⁻¹ − 1. Addition and subtraction use standard binary addition with overflow detection.

Example Calculation

Result: Binary: 11010110, Hex: 0xD6, Unsigned: 214

+42 = 00101010. Flip bits: 11010101. Add 1: 11010110 = 0xD6. As unsigned, this is 214. The sign bit is 1 (negative).

Tips & Best Practices

  • Negation: flip all bits and add 1 — that's the only rule you need
  • The only number you cannot negate without overflow is the minimum value (e.g., −128 in 8-bit)
  • Unsigned and signed addition use the exact same hardware — only the overflow check differs
  • Right-shifting a negative number in C is implementation-defined — some compilers do arithmetic shift, others logical
  • Java's byte type is signed 8-bit two's complement: −128 to 127, not 0 to 255
  • Overflow in addition occurs when two positive numbers give negative or two negatives give positive

How Two's Complement Arithmetic Works

Addition in two's complement is identical to unsigned addition — the hardware simply adds the bit patterns. The magic is that the encoding maps negative numbers to high unsigned values such that unsigned addition wraps around correctly. For example, 42 + (−42) = 00101010 + 11010110 = 1|00000000; the carry-out is discarded, leaving 00000000 = 0. Subtraction A − B is implemented as A + (−B) = A + flip(B) + 1. This means a CPU only needs an adder and a bit-flipper to handle all four signed operations (add, subtract, negate, compare).

Overflow and Undefined Behavior

In C and C++, signed integer overflow is undefined behavior — the compiler is free to assume it never happens and optimize accordingly. This leads to subtle bugs: `if (x + 1 > x)` can be optimized away because the compiler assumes the addition doesn't overflow. Rust panics on overflow in debug builds and wraps in release. Java defines all integer arithmetic as two's complement with wrapping behavior. Understanding two's complement ranges is essential for writing safe code in any of these languages.

Applications Beyond Arithmetic

Two's complement encoding enables efficient comparison: signed comparison can be done with a single subtraction and checking the sign bit. It enables arithmetic right shift (sign-preserving division by powers of 2): shifting 11010110 right by 1 gives 11101011 (−42 → −21). Bitwise operations (AND, OR, XOR, NOT) work directly on two's complement values. These properties make it the universal choice for integer representation in hardware from embedded microcontrollers to supercomputers.

Sources & Methodology

Last updated:

Frequently Asked Questions

  • Two's complement is a binary encoding for signed integers used by all modern CPUs. Positive numbers are standard binary with MSB = 0. Negative numbers are formed by flipping all bits of the positive value and adding 1. It eliminates negative zero and simplifies addition/subtraction hardware.