{
  "id": "hex",
  "version": "d886ba5b7cb3",
  "status": "published",
  "name": "Hex Calculator",
  "question": "What is the answer in hex?",
  "summary": "Adds, subtracts, multiplies or divides two hexadecimal (base 16) whole numbers exactly at any size, shows the result in hex, decimal and binary, and converts decimal numbers to hex.",
  "category": "logic",
  "subcategory": "binary",
  "url": "https://www.acalculator.org/logic/hex-calculator",
  "markdown": "https://www.acalculator.org/logic/hex-calculator.md",
  "kind": "function",
  "method": "Each hex number is read in base 16 (digit × 16ⁿ), the whole numbers are added, subtracted, multiplied or divided exactly, and the result is written back in base 16.",
  "assumptions": [
    "Numbers are whole (no hex fractions) 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.",
    "Numbers may have up to 100 characters; a 0x prefix, spaces, commas and underscores are ignored. Results use capital letters."
  ],
  "inputs": {
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "properties": {
      "a": {
        "title": "First hex number",
        "description": "The first number in hexadecimal: the digits 0 to 9 and the letters A to F, with an optional minus sign.",
        "type": "string",
        "maxLength": 100
      },
      "op": {
        "title": "Operation",
        "description": "What to do with the two numbers: add, subtract, multiply, or divide (whole-number division with a remainder).",
        "type": "string",
        "enum": [
          "add",
          "sub",
          "mul",
          "div"
        ]
      },
      "b": {
        "title": "Second hex number",
        "description": "The second number in hexadecimal.",
        "type": "string",
        "maxLength": 100
      },
      "dec": {
        "title": "Decimal number to convert",
        "description": "Any whole decimal number to write in hexadecimal.",
        "type": "string",
        "maxLength": 100
      }
    }
  },
  "outputs": {
    "hex": {
      "label": "Result in hex",
      "description": "The result in hexadecimal, capital letters; for division, the whole-number quotient.",
      "format": "text"
    },
    "remainder": {
      "label": "Remainder (hex)",
      "description": "For division: what is left over, A − quotient × B, in hexadecimal.",
      "format": "text"
    },
    "decimal": {
      "label": "Result in decimal",
      "description": "The result as a decimal whole number.",
      "format": "integer"
    },
    "binary": {
      "label": "Result in binary",
      "description": "The result in binary (base 2).",
      "format": "text"
    },
    "aDecimal": {
      "label": "First number in decimal",
      "description": "The first hex number in decimal.",
      "format": "integer"
    },
    "bDecimal": {
      "label": "Second number in decimal",
      "description": "The second hex number in decimal.",
      "format": "integer"
    },
    "decHex": {
      "label": "Decimal number in hex",
      "description": "The decimal number to convert, written in hexadecimal.",
      "format": "text"
    }
  },
  "defaultAnswer": {
    "inputs": {
      "a": "8AB",
      "op": "add",
      "b": "B78"
    },
    "outputs": {
      "hex": "1423",
      "decimal": "5155",
      "binary": "1010000100011",
      "aDecimal": "2219",
      "bDecimal": "2936"
    },
    "text": "8AB + B78 = 1423 in hex (5,155 in decimal)."
  },
  "examples": [
    {
      "given": {
        "a": "8AB",
        "op": "add",
        "b": "B78"
      },
      "expect": {
        "hex": "1423",
        "decimal": 5155,
        "aDecimal": 2219,
        "bDecimal": 2936,
        "binary": "1010000100011"
      },
      "source": "hand calculation in content.mdx, column by column with carries; place values 16ⁿ (Wikipedia, \"Hexadecimal\", https://en.wikipedia.org/wiki/Hexadecimal)"
    },
    {
      "given": {
        "a": "8AB",
        "op": "sub",
        "b": "B78"
      },
      "expect": {
        "hex": "-2CD",
        "decimal": -717
      },
      "source": "hand calculation in content.mdx: 2,219 − 2,936 = −717 = −(2 × 256 + 12 × 16 + 13) = −2CD"
    },
    {
      "given": {
        "a": "FF",
        "op": "mul",
        "b": "10"
      },
      "expect": {
        "hex": "FF0",
        "decimal": 4080
      },
      "source": "hand calculation in content.mdx: 255 × 16 = 4,080 = FF0 (multiplying by 10 in hex shifts one place)"
    },
    {
      "given": {
        "a": "B78",
        "op": "div",
        "b": "8AB"
      },
      "expect": {
        "hex": "1",
        "remainder": "2CD",
        "decimal": 1
      },
      "source": "hand calculation in content.mdx: 2,936 = 1 × 2,219 + 717, and 717 = 2CD"
    },
    {
      "given": {
        "a": "-1A",
        "op": "div",
        "b": "4"
      },
      "expect": {
        "hex": "-6",
        "remainder": "-2",
        "decimal": -6
      },
      "source": "hand calculation in content.mdx: −26 = −6 × 4 − 2"
    },
    {
      "given": {
        "a": "FFFFFFFFFFFFFFFF",
        "op": "add",
        "b": "1"
      },
      "expect": {
        "hex": "10000000000000000",
        "decimal": "18446744073709551616"
      },
      "source": "hand calculation in content.mdx: 2⁶⁴ − 1 + 1 = 2⁶⁴ = 18,446,744,073,709,551,616"
    },
    {
      "given": {
        "a": "8AB",
        "op": "add",
        "b": "B78",
        "dec": "48879"
      },
      "expect": {
        "decHex": "BEEF"
      },
      "source": "hand calculation in content.mdx: 48,879 = 11 × 4,096 + 14 × 256 + 14 × 16 + 15 = BEEF"
    }
  ],
  "sources": [
    "Wikipedia, Hexadecimal (base 16, digits 0–9 and A–F, place values powers of 16, conversion by repeated division). https://en.wikipedia.org/wiki/Hexadecimal",
    "Knuth, D. E. (1997). The Art of Computer Programming, Vol. 2: Seminumerical Algorithms, 3rd ed., §4.1 Positional number systems, and §4.3.1 (classical multiple-precision addition, subtraction, multiplication and division)."
  ],
  "related": [
    "xor",
    "subnet",
    "download-time"
  ],
  "changelog": []
}
