Created: 2023-10-13 12:40
Status: #idea
Subject: Mathematics Programming
Tags: Byte number Integer

Two's Complement

Two's complement is the most common method of representing signed integers on computers.

  • it uses the first binary digit on the left to signify if it is positive 0 or negative 1.
  • This reduces the range of positive numbers that can be represented (using 2n bits, where n is the number of bits) from 2n to 2n1.

Converting Positive to Negative, Vice-versa

We invert all the bits, making the first bit 1, then we add 1 to the new binary number.

  • It also works vice-versa, from negative to positive.

Two's Complement Conversion.png

Getting the Value of a Negative Binary

For example, if we have a Character a which is 01100001 or 97 in ASCII, its Two's Complement is 10011111.

  • to get 10011111's value which is -97, we represent the first bit as -128 or 27 and then the other bits as positive numbers.

This then becomes -128 + (1+2+4+8+16) == -97.

References