Decimal to two's complement converter

Two's complement is how processors store negative integers. Pick a bit width to see the value as it sits in memory.

Loading the tool…

Processing and privacy: The result is calculated inside your browser and your data is not sent to any server.

How to use it

  1. Enter decimal integer and bit width.
  2. The result appears straight away and updates as you edit.
  3. Copy the result, or copy a link that keeps the same values to share it.

How it is calculated

For a positive number: its binary value padded with zeros to the width. For a negative one: write its absolute value in binary, invert every bit and add 1, which equals 2 to the power n plus the number.

Example

Inputs: Decimal integer = -42, Bit width = 8-bit. Result: Two's complement: 11010110; Range at this width: -128 to 127.

-42
11010110

Limits and caveats

  • Whole numbers only; fractional values need a floating-point format.
  • A number outside the chosen width is rejected rather than wrapped.

Questions about this tool

How is -42 written in 8-bit two's complement?

42 = 00101010. Invert the bits to get 11010101, then add 1 to get 11010110, which is D6.

What range fits in 8 bits?

-128 to 127. In general, from minus 2 to the power (n-1) up to 2 to the power (n-1) minus 1.

Why do computers use two's complement?

Addition and subtraction use the same circuit for positive and negative numbers, and zero has only one form.