CS 202 - Notes 2016-02-29

Converting between bases

We have been separating the notion of value from representation. The value “twenty six”, can be represented in different bases: 110102, 328, 2610, or 1A16

We can convert a number in any base b to decimal using this equation:

d3d2d1d0 = d3 * b3 + d2 * b2 + d1 * b1 + d0 * b0

Example: 110112 = 1 * 16 + 1 * 8 + 0 * 4 + 1 * 2 + 1 * 1 = 16 + 8 + 2 + 1 = 27

Example: 2A16 = 2 * 16 + 10 * 1 = 42

To convert from decimal to another base, we start by rewriting this equation slightly, progressively factoring out the bs.

((d3 * b + d2) * b + d1) * b + d0

Now consider some value V. We want to to know what d3, d2, d1, and d0.

V = ((d3 * b + d2) * b + d1) * b + d0

If we divide V by b, we get

V/b = ((d3 * b + d2) * b + d1) R d0

Now, we take the quotient and divide that by b

((d3 * b + d2) * b + d1) / b = (d3 * b + d2) R d1

And again,

(d3 * b + d2) / b = d3 R d2

And again,

d3/ b = 0 R d3

Notice that the digits we were interested in have fallen out as the remainders of all of the division.

So, we have a process to take a decimal value and convert it to another base.

Math in binary (and other bases)

Math pretty much works the same in other bases, we just have different numbers of representations.

Example (base 2): 1 + 1 = 10

When our column runs out of representations, we add a 1 to the next column, same as you learned in elementary school

Similarly, for subtraction, if the top number is less than the lower number, we borrow from the next column over. The base determines what gets borrowed (base ten, we borrow a 10, base 2, we borrow a 2, etc).

Fixed bit width and signed numbers

There is a fixed number of bits available per number (hardware dependent – non-negotiable)

When we do math, we now have a problem if we have to borrow from or carry out from the last column. There are no more bits, we we either underflow or overflow and get a number we can’t represent.

Second issue: We need to be able to represent signed numbers and we only have bits

There are a couple of different solutions

Signed magnitude

Take the first bit and consider it to be a sign bit: 1 - negative, 0 - positive

Given three bits:

Bits Value
000 0
001 1
010 2
011 3
100 -0
101 -1
110 -2
111 -3

This has some use, but has some problematic factors as well

We have a negative 0, which is weird

Math is a bit harder. We have to start by looking at the sign bits. If they match, we add. If they don’t match, we subtract and then we have to use the sign bit from the number with the greatest magnitude.

Two’s compliment

Different approach that acknowledges 0s position in the center of the number line. We think of the numbers of being on a wheel, with 0 being in the middle. We make the break between negative and positive numbers at the point where the first bit changes. This means that we can still look at the first bit to determine if the number if positive or negative, despite the fact that the first bit is no longer a sign bit.

Given three bits:

Bits Value
000 0
001 1
010 2
011 3
100 -4
101 -3
110 -2
111 -1

To negate a two’s compliment (called “taking the two’s compliment”), we flip all of the bits and add 1.

Example: negate(010) => 110

Example: negate(111) => 001

This helps us figure out the value of a two’s compliment number that starts with a 1. We can’t use our base conversion formula, so we negate the number, giving us a positive number that we can plug into our equation.

Example: What is the value of the 4-bit two’s compliment number 1010? The first bit is 1, so it is negative. So, negate it. Flip the bits => 0101, and add 1 => 0110. Now, we can plug 0110 into our base conversion equation to give us that value six. So, the original value must have been negative six.

The advantage is that math now works again. We can add numbers together as if they were unsigned. The only issue is that we can carry out from the last bit, and actually have the right answer. When we add two’s compliment numbers together, we can tell that overflow or underflow occurred by looking at both the carry in and the carry out of the last bit. If they match, we are good, if they don’t, our answer is wrong.

We don’t both with subtraction. If we want to subtract, we negate the second number and add instead.