{
  "id": "twos-complement",
  "version": "e1d06edf4d97",
  "status": "published",
  "name": "Two's Complement Calculator",
  "question": "What is the 2's complement of x?",
  "summary": "Converts a signed whole number to its n-bit two’s complement binary pattern (invert the bits of |x| and add 1), or a two’s complement pattern back to its decimal value, with hex and the steps.",
  "category": "logic",
  "subcategory": "binary",
  "url": "https://www.acalculator.org/logic/twos-complement-calculator",
  "markdown": "https://www.acalculator.org/logic/twos-complement-calculator.md",
  "kind": "function",
  "method": "For x ≥ 0, the pattern is x in n binary digits. For x < 0, write |x| in n bits, invert every bit and add 1, which equals 2ⁿ + x. A pattern whose first bit is 1 has the value (unsigned value) − 2ⁿ.",
  "assumptions": [
    "n bits of two’s complement hold −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1; a number outside that range has no n-bit pattern.",
    "A typed bit pattern’s width is its number of digits, so its first digit is the sign bit.",
    "Whole numbers only; arithmetic is exact (no rounding) up to 64 bits."
  ],
  "inputs": {
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "properties": {
      "to": {
        "title": "Convert",
        "description": "Decimal to a two’s complement bit pattern, or a bit pattern to decimal.",
        "type": "string",
        "enum": [
          "binary",
          "decimal"
        ]
      },
      "n": {
        "title": "Decimal number",
        "description": "A whole number, negative or positive, such as -28.",
        "type": "string",
        "maxLength": 30
      },
      "bits": {
        "title": "Number of bits",
        "description": "The width of the pattern, from 2 to 64 bits (8, 16, 32 and 64 are common).",
        "type": "integer",
        "minimum": 2,
        "maximum": 64
      },
      "b": {
        "title": "Two’s complement bits",
        "description": "The bit pattern, 1 to 64 binary digits; its length is the width, and its first bit is the sign.",
        "type": "string",
        "maxLength": 90
      }
    }
  },
  "outputs": {
    "pattern": {
      "label": "Two’s complement",
      "description": "The n-bit two’s complement pattern, in groups of 4 bits.",
      "format": "text"
    },
    "value": {
      "label": "Decimal value",
      "description": "The signed value of the typed pattern.",
      "format": "integer"
    },
    "hex": {
      "label": "Hexadecimal",
      "description": "The same bits in hexadecimal.",
      "format": "text"
    },
    "unsigned": {
      "label": "Same bits as unsigned",
      "description": "The value of the bits read as an unsigned number, 0 to 2ⁿ − 1.",
      "format": "integer"
    },
    "range": {
      "label": "Range at this width",
      "description": "The smallest and largest values n bits of two’s complement hold: −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1.",
      "format": "text"
    },
    "steps": {
      "label": "Steps",
      "description": "The working, step by step.",
      "format": "text"
    },
    "summary": {
      "label": "Answer",
      "description": "The answer in one sentence.",
      "format": "text"
    }
  },
  "defaultAnswer": {
    "inputs": {
      "to": "binary",
      "n": "-28",
      "bits": 8,
      "b": "1110 0100"
    },
    "outputs": {
      "pattern": "1110 0100",
      "hex": "0xE4",
      "unsigned": "228",
      "range": "−128 to 127",
      "steps": "Write 28 in 8 bits: 0001 1100; Invert every bit: 1110 0011; Add 1: 1110 0100",
      "summary": "−28 in 8-bit two’s complement is 1110 0100"
    },
    "text": "−28 in 8-bit two’s complement is 1110 0100."
  },
  "examples": [
    {
      "given": {
        "to": "binary",
        "n": "-28",
        "bits": 8
      },
      "expect": {
        "pattern": "1110 0100",
        "hex": "0xE4",
        "unsigned": 228,
        "range": "−128 to 127"
      },
      "source": "Thomas Finley, Two’s Complement, Cornell University CS 104 notes (invert the digits and add one; −28 in 8 bits is 11100100; n bits hold −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1), https://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html (retrieved 2026-10-02); Python 3: format(-28 & 0xFF, \"08b\")"
    },
    {
      "given": {
        "to": "decimal",
        "b": "11111111111111111111111111111111"
      },
      "expect": {
        "value": -1,
        "hex": "0xFFFFFFFF",
        "unsigned": 4294967295
      },
      "source": "Thomas Finley, Two’s Complement, Cornell University CS 104 notes (invert the digits and add one; −28 in 8 bits is 11100100; n bits hold −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1), https://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html (retrieved 2026-10-02)"
    },
    {
      "given": {
        "to": "binary",
        "n": "57",
        "bits": 8
      },
      "expect": {
        "pattern": "0011 1001",
        "hex": "0x39"
      },
      "source": "Thomas Finley, Two’s Complement, Cornell University CS 104 notes (invert the digits and add one; −28 in 8 bits is 11100100; n bits hold −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1), https://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html (retrieved 2026-10-02): 69 − 12 = 57; Python 3: format(57, \"08b\")"
    },
    {
      "given": {
        "to": "binary",
        "n": "-9,223,372,036,854,775,808",
        "bits": 64
      },
      "expect": {
        "hex": "0x8000000000000000",
        "pattern": "1000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000"
      },
      "source": "Thomas Finley, Two’s Complement, Cornell University CS 104 notes (invert the digits and add one; −28 in 8 bits is 11100100; n bits hold −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1), https://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html (retrieved 2026-10-02): −2⁶³ is the smallest 64-bit value; Python 3: hex(-2**63 & (2**64-1))"
    }
  ],
  "sources": [
    "Thomas Finley, Two's Complement, Cornell University CS 104 notes (invert the digits and add one; −28 in 8 bits is 11100100; 0xFFFFFFFF is −1; n bits hold −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1; 69 − 12 = 57 by addition). https://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html (retrieved 2026-10-02)"
  ],
  "related": [
    "binary",
    "hex",
    "xor"
  ],
  "changelog": []
}
