There are a number of methods that let you convert a number in, say, base 10, to a number

in, say, base 2. We'll start with going from base 2 to base 10.

We'll just look here at 6-bit unsigned (i.e. nonnegative) binary values. 010100 in binary

is what in base 10? It's easiest to read off from right-to-left: we have 0x2 to the power

0 plus 0x2 to the power 1 plus 1x2 to the power 2 plus 0x2 to the power 3 plus 1x2 to the

power 4 plus 0x2 to the power 5: 0 + 0 + 4 + 0 + 16 + 0 = 20 in base 10. Why work right-

to-left rather than left-to-right? The latter method would require that you count bits

to see that the leftmost bit is 2 to the power 5, and if you miscounted--the answer will

be wrong. You can also write down something like:

0 1 0 1 0 0

--- --- --- --- --- ---

32 16 8 4 2 1

and then add up the values below the lines where there's a 1 above the line--16 + 4 = 20.

-------------------------------------------------------------------

Converting from base 10 to base 2. Let's take the decimal number 23. We can write down

--- --- --- --- --- ---

32 16 8 4 2 1

Then we ask "Is the value greater than or equal to 32?" If the answer was "yes" we would

write a 1 in above the 32 and subtract 32 to see what we had left. But here 32 > 23. So we

write a 0 above the 32. "Is the value greater than or equal to 16?" The answer here is "yes"

so we write a 1 above the 16 and subtract 16 from 23 to get 7--we still have to convert the

7 part. "Is the value (i.e. 7) greater than or equal to 8?" No--so we write 0 above the 8.

"Is the value (7) greater than or equal to 4?" Yes--so we write a 1 above the 4 and subtract

4 from 7 to get 3. "Is the value (now 3) greater than or equal to 2?" Yes, so we write 1 in

above the 2 and subtract 2 from 3 to get 1. "Is the value (now 1) greater than or equal to 1?"

Yes, so we write a 1 above the 1, and subtract 1 from 1 to get 0--and we're done:

0 1 0 1 1 1

--- --- --- --- --- ---

32 16 8 4 2 1

A second approach uses remainders. We take our base 10 value (23) and divide it by 2

(the base we are trying to convert to). The result is 11 with a remainder of 1. Next,

we divide 11 by 2 to get 5 with a remainder of 1. We divide 5 by 1 to get 2 with a

remainder of 1. We divide 2 by 2 to get 1 with a remainder of 0. Finally, we divide 1

by 2 to get 0 with a remainder of 1. Here are the remainders:

step remainder

---------------

1 1

2 1

3 1

4 0

5 1

so as an unsigned 5-bit number, 23 = 10111

-------------------------------------------------------------

Signed Binary

We've looked at just unsigned integers. Converting nonnegative numbers (leftmost bit

is a 0) is just like converting unsigned binary. But suppose we have a 6-bit signed binary

111111 -- what does this convert to? Well, here, IT DEPENDS!! It really does depend.

Take a look at my notes on the binary representation of integers! In sign-and-magnitude,

111111 = - 31. In 1's complement, 111111 = -0 = 0. In 2's complement, 111111 = -1. To

see what 101101 converts to--you MUST know which representation you're talking about.

In sign-and-magnitude, 101101 is the negative of 001101 = 13. In 1's complement, flip the

bits: 010010 = 18. In 2's complement, to get the negative of 101101, first flip the bits:

010010 and add 1 --> 010011 = 19. Basically, what's happening is that we have Y = -X

where Y is a negative number and X is nonnegative: e.g. Y = 101101, and we want to know

what X is. We can change the small equation Y = -X to: -Y = -(-X), and -(-X) is the

same as X. So that's why, for example, in 1's complement, to find out what 101101 is the

negative of, just flip the bits.

Hexadecimal (base 16) numbers.

Hexadecimal (usually spoken of as "hex") have digits 0-9, A, B, C, D, E, and F.

(We need 16 digits here). We can remember phone numbers, Social Security

Numbers, student ID numbers, passwords, etc, but it often becomes difficult

to try to remember too many different numbers. Suppose we have a 32-bit

integer: (we'll group this by 8 bits): 00000101 11000101 00000100 11111001.

Try remembering those 32 bits or consider even telling someone else what the

binary bits are: "zero-zero-zero-zero-zero-one-zero-one...etc"--no joy here.

But it's not unusual to have to specify locations--addresses--in the computer

and sometimes even values--such as the address of a printer port. Addresses

are specified in hex--it's much easier than binary. Note that 16 is 2 to the

power 4: 1 hex digit equals 4 binary bits, 2 hex digits = 1 8-bit byte, etc.

So a 32-bit binary value such as the above can be represented as 8 hex digits.

Conversion between hex and binary is trivially easy.

binary hex decimal binary hex dec binary hex dec binary hex dec

------------------- -------------- ------------- -------------

0000 0 0 0100 4 4 1000 8 8 1100 C 12

0001 1 1 0101 5 5 1001 9 9 1101 D 13

0010 2 2 0110 6 6 1010 A 10 1110 E 14

0011 3 3 0111 7 7 1011 B 11 1111 F 15

So to convert from binary to hex, group the binary bits from right to left by 4's and

just write down the hex digit below it:

00000101 11000101 00000100 11111001

0 5 C 5 0 4 F 9

And to convert from hex to binary, replace each hex digit by its equivalent 4 binary bits.

A hex number such as B5F means that this is Bx16 to the power 2 plus 5x16 to the power

1 plus Fx16 to the power 0, or, to put everything in decimal:

11x16x16 + 5x16 + 15x1 = 2816 + 80 + 15 = 2911

You can even convert a decimal number such as 2911 to hex by dividing by the base

(i.e. 16) and keeping track of remainders:

2911 divided by 16 is 181 with a remainder of 15. 181 divided by 16 is 11 with a

remainder of 5, 11 divided by 16 is 0 with a remainder of 11. Remember that 15 in

decimal is F in hex and that 11 in decimal is B in hex, so 2911 = B5F. But doing this

for most people would be an exercise in masochism. Most of us would simply go from

decimal to binary and then from binary to hex.

Note: if you look at some of the html source, you might see text="000000" or

bgcolor="ffffff". In the quotes are 3 pairs of hex digits, each pair of digits driving

one of the blue-green-red color guns (this isn't saying that the first pair is blue, etc).

ffffff results in white, 000000 results in black, 0000ff looks bluish, etc. 2 hex digits is

8 bits--you get a range of 256 (2 to the 8th) shades for each "gun". There's very little

difference between 0000ff and 0000fe, but the difference is a little greater between

0000ff and 0000ef, and greater still between 0000ff and 000088, for example. These

pairs of hex digits tell html how you want the background color and font.

---------------------------------------------------------------

Weird bases.

Suppose you've been smoking something you shouldn't have and decide you want

to play with base negative 3 (i.e. -3). Valid digits are 0, 1, and 2 (as they are for

base 3). So what is 211 in base -3? It's 2x(-3) to the power 2 plus 1x(-3) to the

power 1 plus 1x(-3) to the power 0, or 2x9 + 1x(-3) + 1x0 = 16. If we add the digit

1 in front to ask about 1211 in base -3 this contributes 1x(-3) to the power 3 or

1x(-27) so 1211 in base -3 would be -27 + 16 = -11, a negative value. Weird!

This is just for curiosity, and will not be on the exams. Thank goodness!