{
  "id": "binary",
  "version": "db1ec70730e6",
  "status": "published",
  "name": "Binary Calculator",
  "question": "What is the binary answer?",
  "summary": "Adds, subtracts, multiplies, divides, ANDs, ORs, or XORs two binary numbers exactly at any size, with the result in binary, decimal, and hex.",
  "category": "logic",
  "subcategory": "binary",
  "url": "https://www.acalculator.org/logic/binary-calculator",
  "markdown": "https://www.acalculator.org/logic/binary-calculator.md",
  "kind": "function",
  "method": "Each binary number is read in base 2 (digit × 2ⁿ), the whole numbers are combined exactly, and the result is written back in base 2 and base 16.",
  "assumptions": [
    "Numbers are whole (no binary point) and may be negative; a minus sign is written in front, not as two’s complement.",
    "Division gives the whole-number quotient rounded toward zero and the remainder, A − quotient × B, which has the sign of A.",
    "AND, OR, and XOR compare the numbers bit by bit and take numbers of 0 or more only.",
    "Numbers may have up to 200 characters; a 0b prefix, spaces, and underscores are ignored."
  ],
  "inputs": {
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "properties": {
      "a": {
        "title": "First binary number",
        "description": "The first number in binary: the digits 0 and 1, with an optional minus sign.",
        "type": "string",
        "maxLength": 200
      },
      "op": {
        "title": "Operation",
        "description": "Add, subtract, multiply, divide (whole-number quotient and remainder), or the bitwise AND, OR, or XOR.",
        "type": "string",
        "enum": [
          "add",
          "sub",
          "mul",
          "div",
          "and",
          "or",
          "xor"
        ]
      },
      "b": {
        "title": "Second binary number",
        "description": "The second number in binary.",
        "type": "string",
        "maxLength": 200
      }
    }
  },
  "outputs": {
    "binary": {
      "label": "Result in binary",
      "description": "The result in binary; for division, the whole-number quotient.",
      "format": "text"
    },
    "remainder": {
      "label": "Remainder (binary)",
      "description": "For division: what is left over, A − quotient × B, in binary.",
      "format": "text"
    },
    "decimal": {
      "label": "Result in decimal",
      "description": "The result as a decimal whole number.",
      "format": "integer"
    },
    "hex": {
      "label": "Result in hex",
      "description": "The result in hexadecimal (base 16), capital letters.",
      "format": "text"
    },
    "aDecimal": {
      "label": "First number in decimal",
      "description": "The first number in decimal.",
      "format": "integer"
    },
    "bDecimal": {
      "label": "Second number in decimal",
      "description": "The second number in decimal.",
      "format": "integer"
    }
  },
  "defaultAnswer": {
    "inputs": {
      "a": "1011",
      "op": "add",
      "b": "110"
    },
    "outputs": {
      "binary": "10001",
      "decimal": "17",
      "hex": "11",
      "aDecimal": "11",
      "bDecimal": "6"
    },
    "text": "1011 + 110 = 10001 in binary (17 in decimal)."
  },
  "examples": [
    {
      "given": {
        "a": "1011",
        "op": "add",
        "b": "110"
      },
      "expect": {
        "binary": "10001",
        "decimal": 17,
        "hex": "11",
        "aDecimal": 11,
        "bDecimal": 6
      },
      "source": "OpenStax, Contemporary Mathematics, §4.4 Addition and Subtraction in Base Systems (base 2 addition table: 1 + 1 = 10). https://openstax.org/books/contemporary-mathematics/pages/4-4-addition-and-subtraction-in-base-systems; hand calculation in content.mdx"
    },
    {
      "given": {
        "a": "1001",
        "op": "add",
        "b": "11011"
      },
      "expect": {
        "binary": "100100",
        "decimal": 36
      },
      "source": "OpenStax, Contemporary Mathematics, §4.4 Addition and Subtraction in Base Systems, Example 4.33: 1001 + 11011 = 100100 (base 2). https://openstax.org/books/contemporary-mathematics/pages/4-4-addition-and-subtraction-in-base-systems"
    },
    {
      "given": {
        "a": "110",
        "op": "sub",
        "b": "1011"
      },
      "expect": {
        "binary": "-101",
        "decimal": -5
      },
      "source": "OpenStax, Contemporary Mathematics, §4.4 Addition and Subtraction in Base Systems. https://openstax.org/books/contemporary-mathematics/pages/4-4-addition-and-subtraction-in-base-systems; hand calculation in content.mdx: 6 − 11 = −5 = −101"
    },
    {
      "given": {
        "a": "1011",
        "op": "mul",
        "b": "110"
      },
      "expect": {
        "binary": "1000010",
        "decimal": 66,
        "hex": "42"
      },
      "source": "OpenStax, Contemporary Mathematics, §4.5 Multiplication and Division in Base Systems. https://openstax.org/books/contemporary-mathematics/pages/4-5-multiplication-and-division-in-base-systems; hand calculation in content.mdx: 11 × 6 = 66"
    },
    {
      "given": {
        "a": "1011",
        "op": "div",
        "b": "110"
      },
      "expect": {
        "binary": "1",
        "remainder": "101",
        "decimal": 1
      },
      "source": "OpenStax, Contemporary Mathematics, §4.5 Multiplication and Division in Base Systems. https://openstax.org/books/contemporary-mathematics/pages/4-5-multiplication-and-division-in-base-systems; hand calculation in content.mdx: 11 = 1 × 6 + 5"
    },
    {
      "given": {
        "a": "1100",
        "op": "xor",
        "b": "1010"
      },
      "expect": {
        "binary": "110",
        "decimal": 6
      },
      "source": "OpenStax, Contemporary Mathematics, §4.4 Addition and Subtraction in Base Systems (binary place values). https://openstax.org/books/contemporary-mathematics/pages/4-4-addition-and-subtraction-in-base-systems; hand calculation in content.mdx: bit by bit, 1100 XOR 1010 = 0110"
    },
    {
      "given": {
        "a": "1100",
        "op": "and",
        "b": "1010"
      },
      "expect": {
        "binary": "1000",
        "decimal": 8
      },
      "source": "hand calculation in content.mdx: bit by bit, 1100 AND 1010 = 1000"
    },
    {
      "given": {
        "a": "1100",
        "op": "or",
        "b": "1010"
      },
      "expect": {
        "binary": "1110",
        "decimal": 14
      },
      "source": "hand calculation in content.mdx: bit by bit, 1100 OR 1010 = 1110"
    }
  ],
  "sources": [
    "OpenStax, Contemporary Mathematics, §4.4 Addition and Subtraction in Base Systems, retrieved 2026-10-01. https://openstax.org/books/contemporary-mathematics/pages/4-4-addition-and-subtraction-in-base-systems",
    "OpenStax, Contemporary Mathematics, §4.5 Multiplication and Division in Base Systems, retrieved 2026-10-01. https://openstax.org/books/contemporary-mathematics/pages/4-5-multiplication-and-division-in-base-systems"
  ],
  "related": [
    "hex",
    "xor",
    "binary-to-text"
  ],
  "changelog": []
}
