Convert between decimal and two's complement binary for 8, 16, 32, 64-bit widths. View hex, octal, range, overflow detection, and arithmetic operations.
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.
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 (and dangerous) in systems programming. This calculator lets you instant check conversions, visualize the bit layout, detect overflow, and practice arithmetic — all without guessing or making manual errors. Whether you are debugging C code, solving a CS homework problem, or reverse-engineering a binary format, this tool has you covered.
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.
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).
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).
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.
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.
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.
In n-bit two's complement, there are 2ⁿ total patterns. One pattern is used for zero, so the remaining 2ⁿ − 1 patterns split unevenly: 2ⁿ⁻¹ − 1 positive and 2ⁿ⁻¹ negative. The extra negative value (−2ⁿ⁻¹) has no positive counterpart.
Overflow occurs when adding two positive numbers yields a negative result (MSB flips to 1) or adding two negative numbers yields a positive result (MSB flips to 0). Adding a positive and a negative number can never overflow.
Two's complement flips bits and adds 1 to negate; one's complement only flips bits. Two's complement has unique zero and asymmetric range; one's complement has ±0 and symmetric range. Two's complement is simpler in hardware.
When converting a smaller two's complement number to a larger width (e.g., 8-bit to 16-bit), the sign bit is copied into all new high bits. This preserves the value: −42 in 8 bits (11010110) becomes 1111111111010110 in 16 bits.
Negating −128 (10000000): flip bits → 01111111, add 1 → 10000000 = −128 again. The mathematical result (+128) exceeds the 8-bit range, so the operation silently wraps around — a classic integer overflow bug.