Converting Between Bases

Converting from base-n

Multiply each digit by increasing powers e.g. (201 base 3) →

Converting to base-n

  • For integers, repeatedly divide the number by n until the quotient is 0, the remainder at each step is the answer, with the first remaineder being the least significant digit
    1. For example, converting to base 3
    2. So the answer is
  • For fractions, repeatedly multiply the number by n until the decimal part is 0, or until the desired number of decimal places is reached. The whole number part at each step is the answer, with the first one being the most significant digit. The whole number part should be set to 0 before each successive multiplication.
    1. For example, converting to base 2
    2. (0)
    3. (1)
    4. (0)
    5. (1)
    6. So the answer is

Converting between binary and octal / hexadecimal

  • Binary → Octal: group bits into groups of 3, then convert each group
    • e.g.
  • Binary → Hexadecimal: group bits into groups of 4, then convert each group
    • e.g.
  • Octal/Hexadecimal → Binary: Directly convert each digit into binary and join everything together (just the reverse of above)

Representing Negative Numbers in Binary

Sign and Magnitude

The MSB is the sign (0 for +, 1 for -), and the rest of the bits represent the number

1s Complement

  • For a number x, its negated value represented in 1s complement,
    • The MSB still represents the sign, but for negative numbers, all the other bits are flipped
  • Alternatively, a number’s negative in 1s complement representation can be found by negating all its bits
  • This is so that arithmetic can be directly performed on the numbers (e.g. so that -5 + 5 = 0 can be directly calculated)
  • To add two numbers in 1s complement, add them normally. If there is a carry out bit, add 1 to the result
  • To subtract two numbers, add the 1s complement of the second number to the first number e.g. 5 - 2 = 5 + (-2)

2s Complement

  • For a number x, its negated value represented in 2s complement,
    • The MSB still represents the sign
  • Alternatively, a number’s negative in 2s complement representation can be found by negating all its bits, then adding 1 to the result
    • Can be done manually: Starting from right to left, zeros will remain the same, the first 1 encountered will remain as 1, and everything to the left of the first 1 will be inverted
  • This is so that we do not waste one number representing -0
  • Note: to add two numbers, we can do it as per normal, but disregard the final carry bit
  • To convert fractional numbers to 2s complement, negate all bits and add 1, but ignore the decimal point when adding 1
  • Subtraction can be performed by finding the second number’s 2s complement, then adding it to the first number. Ignore the carry out bit
  • Note that we need to account for overflows, by checking if the sign of the output number is expected (based on the two input numbers)
    • This happens when the carry out bit is not equal to the carry on the most significant bit

Radix diminished complement

  • n-1’s complement for a base-n number
  • The value of in base-r, with digits and decimal digits is
  • Alternatively, just “flip” every digit: for digit ,

Excess Representation

  • Split the range of numbers into two, with one half representing negative numbers, and the other representing positive
  • To convert to excess-n representation, add n to the number, then convert to binary
  • e.g. excess-4 (4 refers to the number that 000 maps to, usually for n-bit numbers) representation on 3 bit numbers:
    • 000 → -4, 001 → -3, 010 → -2 â€Ķ 100 → 0, 101 → 1, â€Ķ
  • for excess-n representation in bits, there are negative numbers and positive numbers

Representing Real Numbers

Fixed Point Representation

  • The number of bits allocated for the whole number part and the fractional part are fixed

Floating Point Representation

  • Most computers use IEEE 754 representation
    • Single-precision (32 bits): 1 bit sign, 8 bit exponent with bias 127 (excess-127), 23 bit mantissa
    • Double-precision(64 bits): 1 bit sign, 11 bit exponent with bias 1023 (excess-1023), 52 bit mantissa
  • Number is stored in 3 parts: sign, exponent, mantissa
    • (sign) mantissa 2exponent
  • sign
    • only applies to mantissa
    • 0: positive, 1: negative
  • mantissa
    • Is normalised, and has an implicit leading bit of 1
    • → and is stored in the mantissa
    • → and is stored in the mantissa
  • To manually convert:
    • Convert number to binary
    • Normalise mantissa
    • Convert exponent to excess-127 (or 1023)
    • Combine the parts to form the number
  • NaN and Infinity
    • Infinity: Exponent all 1s, Mantissa all 0
    • NaN: Exponent all 1s, Mantissa non-zero

Table

dechexbin
000000
110001
220010
330011
440100
550101
660110
770111
881000
991001
10a1010
11b1011
12c1100
13d1101
14e1110
15f1111