wisconsin
Binary Arithmetic

Binary Arithmetic

Curated by Last modified: 2 min read

Base 2 and Base 10 Conversion

Base 2 to Base 10

To convert a two’s complement binary number to a decimal number, use the process:

  1. If the MSB is 1, the number is negative. Convert the number to its positive form by taking the two’s complement. (Invert all bits and add 1)
  2. Multiply each bit by where is the position of the bit in the binary number.
  3. If the MSB was 1, change the sign of the number to negative.

Base 10 to Base 2

To convert a decimal number to a two’s complement binary number, use the process:

  1. If negative, change to positive decimal number (absolute value)
  2. Subtract largest power of 2 from the number and set the bit in the binary number to 1.
  3. Repeat step 2 until the number is 0.
  4. Choose the required width and verify that the value fits its signed range; pad the magnitude to that width before negating.
  5. If the original number was negative, take the two’s complement of the binary number.

Binary Addition

With two’s complement binary numbers, addition is performed as follows:

  1. Start on the right (LSB) and add the two bits.
  2. Add the digits and carry over any overflow to the next column.

Overflow

Overflow occurs when the result of an arithmetic operation is too large to be represented by the number of bits available.

Unsigned Overflow

Unsigned overflow occurs when the result of an arithmetic operation is too large to be represented by the number of bits available.

This can happen when the sum of two numbers is greater than the maximum value that can be represented by the number of bits available.

Signed Overflow

Signed overflow occurs when the result of an arithmetic operation is too large to be represented by the number of bits available.

This can happen when the signs of both addends are the same, but the sign of the result is different.

Mismatched Sizes

To add two numbers of different sizes, sign-extend a signed two’s-complement operand, or zero-extend an unsigned operand, to the common width.

Related: lecture-02, lecture-03, lecture-04, lecture-05

Source

Original retained in wisconsin-legacy.

Linked to this page