# EECS 452

(Taken from EECS 370...)
### Binary/Hexadecimal/Decimal Conversion

Binary, hexadecimal and decimal are all different ways to express
numbers. You are all familiar from gradeschool with decimal notation,
which uses base 10. Binary and hexadecimal work exactly the same way,
except they are base 2 and base 16, respectively.
Here's a table to get you started on how to think about the
different notations:

decimal hex binary

0 0 0

1 1 1

2 2 10

3 3 11

4 4 100

5 5 101

6 6 110

7 7 111

8 8 1000

9 9 1001

10 A 1010

11 B 1011

12 C 1100

13 D 1101

14 E 1110

15 F 1111

16 10 10000

17 11 10001

18 12 10010

19 13 10011

20 14 10100

. . .

. . .

. . .

**Decimal to binary conversion:**
To do decimal to binary conversion, one algorithm you can use is to
simply divide the decimal number by two and continue recording the
remainder until you get to a quotient of zero. For example, suppose I
began with the number 11:

11/2 = 5 Remainder: 1

5/2 = 2 Remainder: 1

2/2 = 1 Remainder: 0

1/2 = 0 Remainder: 1

The binary number you get out of this is simply 1011. You can verify
that this is the right number by converting the binary back to decimal
as follows:
2^3 2^2 2^1 2^0

8 4 2 1

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

1 0 1 1

8 + 0 + 2 + 1 = 11!

**Hex conversions:**
Given a binary number, you can easily convert to the hexadecimal
representation by grouping the binary number into groups of 4 digits.
The possible values for the hex number are up in the table above.
Given a decimal number, you can use a similar algorithm to the one
above for binary conversions, repeatedly dividing by 16, remembering
that the remainder can be represented by the digits 0 -> 9 and the
letters A -> F as shown in the table above.

Example Conversion:

73289 (decimal) = 0001 0001 1110 0100 1001 (binary) = 0x11E49 (hex)

**Note:** hexadecimal numbers are often expressed beginning with
a 0x

**Questions:**

Convert the following decimal numbers to both binary and hex:

1) 32

2) 5236

3) 21387

Convert the following hex numbers to binary and decimal:

1) 0x67A0

2) 0x002AC

3) 0x70AFF

Convert the following binary numbers to hex and decimal:

1) 000111010

2) 100100101

3) 001001111

### Two's Complement

Two's complement is the way computers use to express integers.
Positive integers are expressed exactly as the binary numbers above are
expressed. Two's complement makes it easy for computers to express
negative integers. Suppose we wanted to express the number -34 in
8-bits (for simplicity's sake -- we could just as easily be working
with 32 or 128-bits)
Step 1: Begin by writing out 34 in binary form:

00100010

Step 2: Invert all of the digits:
11011101

Step 3: Add 1:
11011110

Presto! You have your negative number. So -34 is expressed as
11011110. That means any number with a 1 in its most significant bit
(leftmost) is negative. Likewise, given a two's complement number, you
can easily find out what it represents by inverting the digits back and
adding a 1.
**Questions:**

- Convert the following decimal numbers into 32-bit two's
complement:

a) 145

b) -23

c) -8132

- Convert the following 32-bit two's complement numbers into
decimal:

a) 1000 0000 0000 0100 0000 0000 0010 0000

b) 1111 1111 1111 1111 1111 1111 1111 1001

c) 1111 1111 1101 1111 1111 1111 1000 0110