1 - Binary Conversions

Converting from Binary (Unsigned)

To convert a binary number to decimal, separate each bit from most significant bit (MSB) to least significant bit (LSB). We can apply the following formula:
n = akb^k + ak-1b^(k-1) + … + a1*b + a0, where each a represents the bit value.

Example: Let A = (1100 1111)_2. To calculate the decimal equivalent:

  • n = 1b^7 + 1b^6 + 0b^5 + 0b^4 + 1b^3 + 1b^2 + 1b^1 + 1b^0
  • = 1128 + 164 + 032 + 016 + 18 + 14 + 12 + 11
  • = 128 + 64 + 0 + 0 + 8 + 4 + 2 + 1
  • = 207

Converting to Binary (Unsigned)

To convert a decimal number to binary, repeatedly perform integer division by 2 and keep track of the remainders. This process continues until the quotient is 0. The first remainder obtained becomes the least significant bit (LSB) and the last remainder becomes the most significant bit (MSB).

Example: Converting (55)_10 to binary:

  • 55 = 2*27 + 1 ⟹ remainder 1
  • 27 = 2*13 + 1 ⟹ remainder 1
  • 13 = 2*6 + 1 ⟹ remainder 1
  • 6 = 2*3 + 0 ⟹ remainder 0
  • 3 = 2*1 + 1 ⟹ remainder 1
  • 1 = 2*0 + 1 ⟹ remainder 1

Reading the remainders from last to first gives: (110111)_2. This can also be expressed with leading zeroes:

  • (0011 0111)2 or (0000 0000 0011 0111)2 for variations.

Signed Representations

To denote signed numbers, one bit is allocated as a sign bit:

  • MSB = 1: Negative number
  • MSB = 0: Positive number

This allows for the representation of both positive and negative integers. Even with signs, zero is included in this representation.

Sign-Magnitude Representation

In Sign-Magnitude representation, the MSB indicates sign, while the other bits indicate the magnitude (absolute value). The value is positive if MSB is 0 and negative if it is 1.

Example: For A = (1100 1111)_2, converting gives:

  • Exclude the MSB for magnitude calculation:
  • n = ak-1b^(k-1) + … + a1b + a0
  • n = 164 + 032 + 016 + 18 + 14 + 12 + 1*1 = 79
  • Since MSB is 1, we take -79 as the required value.

Converting to Binary Using Sign-Magnitude

To convert a positive decimal to binary in sign-magnitude:
Example: Convert (49)_10 to binary in 8 bits:

  • Convert 49 to binary →
  • (49)10 = (110001)2 → (0011 0001)_2

To convert a negative decimal like (-55)_10:

  • Calculate positive form → 55 = (110111) → (0011 0111) → Set MSB to 1 → (-55) = (1011 0111)_2

1’s Complement Representation

In the 1’s complement form, negative numbers are represented by taking the bitwise inverse of their positive counterparts. The first bit remains the sign bit indicating positivity/negativity.

Example: Converting A = (0100 1111)_2:

  • Since MSB = 0, the number is positive. Calculate:
  • n = 64 + 8 + 4 + 2 + 1 = 79.

For A = (1100 1111)_2:

  • MSB = 1, hence negative, inverse gives (0011 0000)_2. Compute value:
  • n = -48.

2’s Complement Representation

The 2’s complement is the preferred way of representing signed integers. A bitwise inverse followed by an addition of 1 is used to find negative equivalents.

Example: For A = (0100 1111)_2:

  • MSB is 0, thus positive → n = 79.

For A = (1100 1111)_2:

  • MSB is 1. Apply inverse → gives (0011 0001) → add 1 → (1100 1000)_2 → n = -49.

So representing (-55)_10 using 2's complement, we compute:

  • For 55 → (0011 0111)2 then invert and add 1 → (-55) = (1100 1001)2