Presentation is loading. Please wait.

Presentation is loading. Please wait.

Homework Hints Algorithms.

Similar presentations


Presentation on theme: "Homework Hints Algorithms."— Presentation transcript:

1 Homework Hints Algorithms

2 Converting Positive Numbers
Base 2 to base 16 Base 16 to base 2 Base 16 to base 10 Base 2 to base 10 Base 10 to base 2 Base 10 to base 16

3 Converting positive numbers: base 2 to base 16
Put the digits into groups of 4 starting at the right If the last group has last than 4 digits, extend it with leading 0s Convert each group of 4 according to the following translation 0000 -> 0, > 1, > 2, >3 0100 -> 4, > 5, > 6, > 7 1000 -> 8, > 9, > A, > B 1100 -> C, > D, > E, > F

4 Example: to base 16 3249

5 Converting positive numbers: base 16 to base 2
Convert each digit into a group of 4 according to the following translation 0 -> 0000, 1 -> 0001, 2 -> 0010, 3 -> 0011 4 -> 0100, 5 -> 0101, 6 -> 0110, 7 -> 0111 8 -> 1000, 9 -> 1001, A -> 1010, B -> 1011 C -> 1100, D -> 1101, E -> 1110, F -> 1111

6 Example: 8EF16 to base 2 8 = 1000 E = 1110 F = 1111

7 Converting positive numbers: base 16 to base 10
Expand the number into its component values: face-value x position value Position value rightmost digit, position value = 160 Moving right to left, the exponent in each position increases by 1 (161, 162, 163, … ) Face value 0 - 9 are their face values A - F are 10 – 15 respectively Convert each exponent to its decimal equivalent Multiply each face-value pair together Add the values together

8 Example: CAB16 to base 10 C x 162 + A x 161 + B x 160 =
12 x x x 1 = = 3243

9 Converting positive numbers: base 2 to base 10
Expand the number into its component values: face-value x position value Position value rightmost digit, position value = 20 Moving right to left, the exponent in each position increases by 1 (21, 22, 23, … ) Face value 0 = 0, 1 = 1 Eliminate all the 0 terms Simplify each 1x2n to 2n Convert each exponent to its decimal equivalent Add the values together

10 Example: to base 10 1x x x29 + 0x28 + 1x27 + 0x26 + 1x25 + 0x24 + 1x23 + 0x22 + 1x21 + 1x20 = 1x x x27 + 1x25 + 1x23 + 1x21 + 1x20 = = = 3243

11 Converting positive numbers: base 10 to base 2
Divide the number by 2 Use the remainder as the next number (beginning at the right) If the result is 0 then stop Use the result as the next number and go to step 1

12 Example: 327 to base 2 327 / 2 = 163 r 1 answer 1

13 Converting positive numbers: base 10 to base 16
Divide the number by 16 Use the remainder as the next number (beginning at the right) If the remainder is less than 10, use the number directly, else convert it to a letter using 10->A, 11->B, 12->C, 13->D, 14->E, 15->F If the result is 0 then stop Use the result as the next number and go to step 1

14 Example: 327 to base 16 327 / 16 = 20 r 7 answer 716

15 Two’s Complement Convert a positive base 10 number to binary two’s complement Convert a negative base 10 number to binary two’s complement Convert a positive base 10 number to hexadecimal two’s complement Convert a negative base 10 number to hexadecimal two’s complement

16 Convert a positive base 10 number to binary two’s complement
Convert the number to base 2 as described on slide 11 Fill in all leading spaces with 0’s

17 Example: 52 to binary two’s complement
Extend to 16-bit field:

18 Convert a negative base 10 number to binary two’s complement
Convert the positive value of number to binary as described on slide 11 Fill in all leading spaces with 0’s Flip all digits: 0 -> 1, 1 -> 0 Add binary 1 to the number, remembering the following rules: 0+0=0, 0+1=1, 1+0=1, 1+1=10

19 Example: -52 to binary two’s complement
extend to 16 bits Flip the bits and add 1

20 Convert a positive base 10 number to hexadecimal two’s complement
Convert the number to base 2 as described on slide 11 Fill in all leading spaces with 0’s Put the digits into groups of 4 starting at the right Convert each group of 4 according to the following translation 0000 -> 0, > 1, > 2, >3 0100 -> 4, > 5, > 6, > 7 1000 -> 8, > 9, > A, > B 1100 -> C, > D, > E, > F

21 Example: 52 to hexadecimal two’s complement
52/2 = 26 r 1 1 26/2 = 13 r 0 01 13/2 = 6 r 1 101 6/2 = 3 r 3/2 = 1 r 1/2 = 0 r 003516

22 Convert a negative base 10 number to binary two’s complement
Convert the positive value of number to binary as described on slide 11 Fill in all leading spaces with 0’s Flip all digits: 0 -> 1, 1 -> 0 Add binary 1 to the number, remembering the following rules: 0+0=0, 0+1=1, 1+0=1, 1+1=10 Put the digits into groups of 4 starting at the right Convert each group of 4 according to the following translation 0000 -> 0, > 1, > 2, >3 0100 -> 4, > 5, > 6, > 7 1000 -> 8, > 9, > A, > B 1100 -> C, > D, > E, > F

23 Example: -52 to binary two’s complement
FFCB16

24 Two’s Complement Operations
Binary addition Binary subtraction Binary comparison Hexadecimal addition Hexadecimal subtraction Hexadecimal comparison

25 Binary addition Add the two numbers
If the carry into the leftmost digit is not the same as the carry out, define it as an overflow

26 Example

27 Binary subtraction Convert the second number into it’s negative by
Flipping each bit (0->1, 1->0) Add binary 1 to the number, remembering the following rules: 0+0=0, 0+1=1, 1+0=1, 1+1=10 Add the two numbers If the carry into the leftmost digit is not the same as the carry out, define it as an overflow and stop

28 Example negate >

29 Binary comparison Subtract the two numbers as described in slide 27
If result is positive (first bit 0), first number is greater If result is negative (first bit 0 and not all others are 0), first number is smaller If result is zero, numbers are equal

30 Example First number is greater than the second

31 Hexadecimal addition Convert both numbers into binary as described in slide 5 Add the two numbers If the carry into the leftmost digit is not the same as the carry out, define it as an overflow and stop Put the digits into groups of 4 Convert each group of 4 according to the following translation 0000 -> 0, > 1, > 2, >3 0100 -> 4, > 5, > 6, > 7 1000 -> 8, > 9, > A, > B 1100 -> C, > D, > E, > F

32 Example: BA16 + AB16 BA16 AB16 016516

33 Alternative algorithm: hexadecimal addition table
1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E

34 Example: BA16 + AB16 (1) BA16 +AB16 516 16516

35 Hexadecimal subtraction
Convert both numbers into binary as described in slide 5 Convert the second number into it’s negative by Flipping each bit (0->1, 1->0) Add binary 1 to the number, remembering the following rules: 0+0=0, 0+1=1, 1+0=1, 1+1=10 Add the two numbers If the carry into the leftmost digit is not the same as the carry out, define it as an overflow

36 Example: BA16 - AB16 BA16 AB16 negate > 000F16

37 Alternative algorithm: hexadecimal subtraction table borrow not needed borrow needed
1 2 3 4 5 6 7 8 9 A B C D E F

38 Example: BA16 - AB16 A B 1A16 - A B16 F16


Download ppt "Homework Hints Algorithms."

Similar presentations


Ads by Google