-42
—
127
-128
256
1
42
-42
—
127
-128
256
1
42
The Two’s Complement Calculator converts a signed decimal integer to its two’s complement binary representation, the standard method used by virtually all modern computers to represent signed integers. Understanding two’s complement is essential for any programmer working with low-level code, embedded systems, or debugging binary data.
In an unsigned binary system, an \(n\)-bit number can represent values from 0 to \(2^n - 1\). But how do we represent negative numbers? Several encoding schemes exist, but two’s complement has become the universal standard because it has elegant mathematical properties: there is exactly one representation of zero, addition and subtraction use the same hardware circuit, and overflow detection is straightforward.
In two’s complement, the most significant bit (MSB) serves as the sign bit: 0 for positive, 1 for negative. However, unlike simple sign-magnitude representation, the remaining bits are not just the magnitude. For negative numbers, the two’s complement representation is obtained by inverting all bits of the absolute value and adding 1. Equivalently, for a negative number \(-x\) in \(n\) bits, the stored unsigned value is \(2^n - x\).
For an 8-bit system, two’s complement represents values from −128 to +127. For 16-bit, the range is −32,768 to +32,767. For 32-bit, the range is −2,147,483,648 to +2,147,483,647. This asymmetry (one more negative value than positive) is a characteristic feature of two’s complement.
Two’s complement is used in every modern CPU architecture: x86, ARM, RISC-V, MIPS, and more. Programming languages like C, C++, Java, Rust, and Go all use two’s complement for signed integer types (int, long, short). Understanding how it works helps you debug issues like integer overflow (when adding two positive numbers yields a negative result), sign extension (widening a value from 8 to 32 bits), and casting between signed and unsigned types.
This calculator shows the unsigned two’s complement value that would be stored in memory for a given signed decimal input. For positive numbers, the value is unchanged. For negative numbers, you can see the unsigned representation that the hardware actually stores. It also displays the valid range for the selected bit width, helping you understand overflow boundaries.
Whether you are studying computer architecture, debugging assembly code, analyzing memory dumps, or learning how CPUs perform arithmetic, this tool provides instant insight into the two’s complement encoding used by your computer.
Two’s complement encoding for an \(n\)-bit system:
For a non-negative number \(x\) where \(0 \le x \le 2^{n-1}-1\), the representation is simply \(x\).
For a negative number \(-x\) where \(1 \le x \le 2^{n-1}\):
$$\text{Two's Complement} = 2^n + (-x) = 2^n - x$$
This is equivalent to the bit inversion plus one method:
$$\text{Two's Complement} = \overline{x} + 1$$
where \(\overline{x}\) is the bitwise NOT of \(x\). The valid range is:
$$-2^{n-1} \le x \le 2^{n-1} - 1$$
The total number of representable values is \(2^n\).
The Two’s Complement Value shows the unsigned integer that would be stored in memory. For positive inputs, this equals the input. For negative inputs (e.g., −42 in 8-bit), the result is \(256 - 42 = 214\), meaning the byte 0xD6 (11010110) is stored. The Max Positive and Min Negative values show the representable range for the selected bit width. Is Negative indicates whether the sign bit is set. If your input exceeds the valid range, the result may not be meaningful.
Inputs
Results
-42 in 8-bit two's complement = 214 unsigned (11010110 binary). Computed as 256 + (-42) = 214.
Inputs
Results
100 is positive and within 8-bit range (0-127), so the two's complement value is simply 100 (01100100 binary).
Two's complement is the most common method for representing signed integers in binary. The MSB acts as a sign bit, and negative numbers are represented by inverting all bits of the magnitude and adding 1. This allows addition and subtraction to use the same circuitry.
Two's complement has a single representation of zero (unlike one's complement which has +0 and -0), uses the same addition/subtraction circuit for both positive and negative numbers, and makes overflow detection simple via the carry and overflow flags.
Take the absolute value, write it in binary, invert all bits (0 becomes 1 and vice versa), then add 1. Alternatively, subtract the absolute value from 2^n, where n is the bit width. For -42 in 8 bits: 256 - 42 = 214.
An 8-bit two's complement number ranges from -128 to +127, a total of 256 values. The most negative value (-128 = 10000000) has no positive counterpart in 8 bits, which is why negating -128 in 8-bit arithmetic causes overflow.
When the result of an arithmetic operation exceeds the representable range, overflow occurs. Adding two positive numbers may produce a negative result, or adding two negative numbers may produce a positive result. CPUs set an overflow flag to indicate this condition.
Sign extension is the process of increasing the bit width of a two's complement number while preserving its value. The sign bit is replicated into the new higher-order bits. For example, -42 in 8-bit (11010110) becomes 11111111 11010110 in 16-bit.
In 8-bit two's complement, positive values range from 0 to 127 (01111111). The value 128 would require 10000000, but that pattern is already used to represent -128. This asymmetry is inherent to the two's complement system.
Zero in two's complement is represented as all zeros (00000000 for 8-bit). Negating zero: invert to get 11111111, add 1 to get 100000000 (9 bits). The overflow bit is discarded, leaving 00000000 — zero. There is only one zero representation.
Most modern languages use two's complement for signed integers: C, C++, Java, Rust, Go, C#, and Swift. The C23 standard explicitly mandates two's complement. JavaScript uses 64-bit floating point but converts to 32-bit two's complement for bitwise operations.
The two's complement of 0 in any bit width is 0. Inverting all zeros gives all ones, and adding 1 causes a carry that overflows beyond the bit width, leaving 0. This unique representation of zero is a key advantage of the two's complement system.
Roboculator Team
The Roboculator Team explains calculations, planning tools, and practical formulas in clear language for real-life situations.
How helpful was this calculator?
Be the first to rate!
Hexadecimal Calculator
Programming & Developer Calculators
Programmer Calculator (Hex/Oct/Bin/Dec)
Programming & Developer Calculators
Ones Complement Calculator
Programming & Developer Calculators
Floating-Point IEEE 754 Converter
Programming & Developer Calculators
Fractional Bits Converter
Programming & Developer Calculators
Hamming Code Calculator
Programming & Developer Calculators