Two's Complement Calculator: Convert Signed Integers at Any Bit Width

Enter a signed or unsigned decimal, or a binary, hex, or octal pattern, at 1 to 256 bits. See every reading of the same bits, the sign bit, the invert-then-add-1 working, sign extension and truncation, and A op B results with carry and overflow flags. Exact BigInt math runs in your browser; nothing is sent anywhere.

Input

Whole number with an optional sign, such as -5. Spaces and underscores are ignored.

Examples

Ready.

Values

Signed decimal
Unsigned decimal
Binary (nibbles)
Hexadecimal
Octal
Sign bit

Binary copies as plain digits without spaces. Hex and octal copy with their 0x and 0o prefixes.

    Bit layout

    Negation: invert the bits, then add 1

    
        

      Sign extension and truncation

      The same pattern moved to the common widths
      Width Operation Hex pattern Signed value Value kept?

      Arithmetic at this width

      A and B accept decimal (-5 or 200; values above the signed maximum are read as unsigned) or a 0x, 0b, or 0o pattern. The width and the wrap option come from the Input panel.

      
          

          How it works and limits

          • Representation. An n-bit pattern with unsigned value u reads as signed u when the top (sign) bit is 0, and as u - 2^n when it is 1. Equivalently, the sign bit weighs -2^(n-1). Ranges are -2^(n-1) to 2^(n-1) - 1 signed and 0 to 2^n - 1 unsigned.
          • Exact integers. All values use JavaScript BigInt, so 64-, 128-, and 256-bit results are exact. Only whole numbers are accepted; decimals, fractions, and exponents are rejected with a message.
          • Patterns. Binary, hex, and octal digits are zero-extended on the left when shorter than the width (never sign-extended). Patterns wider than the width are an error unless wrapping is on, which keeps the low n bits.
          • The minimum value. -2^(n-1) has no positive counterpart. Negating it overflows back to itself, which the negation panel shows with the overflow flag.
          • 1-bit width. The only bit is the sign bit: pattern 0 is 0 and pattern 1 is -1. No positive number exists, so +1 is out of range and negating -1 overflows to -1.
          • Flags. Carry is the unsigned carry out of the top bit (for subtraction it is shown as a borrow, as x86 CF does; ARM's C flag is the inverse). Signed overflow means the exact signed result does not fit. Shift counts are applied literally, so counts of n or more shift every bit out; C, Java, JavaScript, and x86 mask or leave such counts undefined.
          • Not covered. One's complement and sign-magnitude formats, floating point (IEEE 754), division, and byte order are outside this tool.