Homework Hints - PowerPoint PPT Presentation

About This Presentation
Title:

Homework Hints

Description:

Homework Hints Algorithms 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 ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 39
Provided by: Diane304
Learn more at: http://wwwx.cs.unc.edu
Category:
Tags: hints | homework

less

Transcript and Presenter's Notes

Title: Homework Hints


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 -gt 0, 0001 -gt 1, 0010 -gt 2, 0011 -gt3
  • 0100 -gt 4, 0101 -gt 5, 0110 -gt 6, 0111 -gt 7
  • 1000 -gt 8, 1001 -gt 9, 1010 -gt A, 1011 -gt B
  • 1100 -gt C, 1101 -gt D, 1110 -gt E, 1111 -gt F

4
Example0110010010010012 to base 16
  • 011 0010 0100 1001
  • 3 2 4 9
  • 3249

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

6
Example8EF16 to base 2
  • 8 1000
  • E 1110
  • F 1111
  • 100011101111

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
ExampleCAB16 to base 10
  • C x 162 A x 161 B x 160
  • 12 x 256 10 x 16 11 x 1
  • 3072 160 11
  • 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
Example1100101010112 to base 10
  • 1x211 1x210 0x29 0x28 1x27 0x26 1x25
    0x24 1x23 0x22 1x21 1x20
  • 1x211 1x210 1x27 1x25 1x23 1x21 1x20
  • 211 210 27 25 23 21 20
  • 2048 1024 128 32 8 2 1
  • 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
Example327 to base 2
  • 327 / 2 163 r 1 answer 1
  • 163 / 2 81 r 1 answer 11
  • 81 /2 40 r 1 answer 111
  • 40 / 2 20 r 0 answer 0111
  • 20 / 2 10 r 0 answer 00111
  • 10 / 2 5 r 0 answer 000111
  • 5 / 2 2 r 1 answer 1000111
  • 2 / 2 1 r 0 answer 01000111
  • 1 / 2 0 r 1 answer 101000111

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-gtA, 11-gtB, 12-gtC, 13-gtD, 14-gtE, 15-gtF
  • If the result is 0 then stop
  • Use the result as the next number and go to step
    1

14
Example327 to base 16
  • 327 / 16 20 r 7 answer 716
  • 20 / 16 1 r 4 answer 4716
  • 1 / 16 0 r 1 answer 14716

15
Twos Complement
  • Convert a positive base 10 number to binary twos
    complement
  • Convert a negative base 10 number to binary twos
    complement
  • Convert a positive base 10 number to hexadecimal
    twos complement
  • Convert a negative base 10 number to hexadecimal
    twos complement

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

17
Example 52 to binary twos complement
  • 52/2 26 r 1 1
  • 26/2 13 r 0 01
  • 13/2 6 r 1 101
  • 6/2 3 r 0 0101
  • 3/2 1 r 1 10101
  • 1/2 0 r 1 110101
  • Extend to 16-bit field
  • 0000000000110101

18
Convert a negative base 10 number to binary twos
complement
  • Convert the positive value of number to binary as
    described on slide 11
  • Fill in all leading spaces with 0s
  • Flip all digits 0 -gt 1, 1 -gt 0
  • Add binary 1 to the number, remembering the
    following rules
  • 000, 011, 101, 1110

19
Example -52 to binary twos complement
  • 52/2 26 r 1 1
  • 26/2 13 r 0 01
  • 13/2 6 r 1 101
  • 6/2 3 r 0 0101
  • 3/2 1 r 1 10101
  • 1/2 0 r 1 110101
  • extend to 16 bits
  • 0000000000110101
  • Flip the bits and add 1
  • 1111111111001010
  • 1
  • 1111111111001011

20
Convert a positive base 10 number to hexadecimal
twos complement
  • Convert the number to base 2 as described on
    slide 11
  • Fill in all leading spaces with 0s
  • Put the digits into groups of 4 starting at the
    right
  • Convert each group of 4 according to the
    following translation
  • 0000 -gt 0, 0001 -gt 1, 0010 -gt 2, 0011 -gt3
  • 0100 -gt 4, 0101 -gt 5, 0110 -gt 6, 0111 -gt 7
  • 1000 -gt 8, 1001 -gt 9, 1010 -gt A, 1011 -gt B
  • 1100 -gt C, 1101 -gt D, 1110 -gt E, 1111 -gt F

21
Example 52 to hexadecimal twos complement
  • 52/2 26 r 1 1
  • 26/2 13 r 0 01
  • 13/2 6 r 1 101
  • 6/2 3 r 0 0101
  • 3/2 1 r 1 10101
  • 1/2 0 r 1 110101
  • 0000000000110101
  • 0000 0000 0011 0101
  • 003516

22
Convert a negative base 10 number to binary twos
complement
  • Convert the positive value of number to binary as
    described on slide 11
  • Fill in all leading spaces with 0s
  • Flip all digits 0 -gt 1, 1 -gt 0
  • Add binary 1 to the number, remembering the
    following rules
  • 000, 011, 101, 1110
  • Put the digits into groups of 4 starting at the
    right
  • Convert each group of 4 according to the
    following translation
  • 0000 -gt 0, 0001 -gt 1, 0010 -gt 2, 0011 -gt3
  • 0100 -gt 4, 0101 -gt 5, 0110 -gt 6, 0111 -gt 7
  • 1000 -gt 8, 1001 -gt 9, 1010 -gt A, 1011 -gt B
  • 1100 -gt C, 1101 -gt D, 1110 -gt E, 1111 -gt F

23
Example -52 to binary twos complement
  • 52/2 26 r 1 1
  • 26/2 13 r 0 01
  • 13/2 6 r 1 101
  • 6/2 3 r 0 0101
  • 3/2 1 r 1 10101
  • 1/2 0 r 1 110101
  • 0000000000110101
  • 1111111111001010
  • 1
  • 1111111111001011
  • 1111 1111 1100 1011
  • FFCB16

24
Twos 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
  • 00000000101110102
  • 00000000101010112
  • 00000001011001012

27
Binary subtraction
  • Convert the second number into its negative by
  • Flipping each bit (0-gt1, 1-gt0)
  • Add binary 1 to the number, remembering the
    following rules
  • 000, 011, 101, 1110
  • 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
  • 00000000101110102
  • - 00000000101010112
  • negate 00000000101010112 -gt
  • 1111111101010100
  • 1
  • 1111111101010101
  • 00000000101110102
  • 11111111010101012
  • 00000000000011112

29
Binary comparison
  • Subtract the two numbers as described in slide 27
  • Comparison
  • 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
  • 00000000101110102
  • - 00000000101010112
  • 00000000000011112
  • 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 -gt 0, 0001 -gt 1, 0010 -gt 2, 0011 -gt3
  • 0100 -gt 4, 0101 -gt 5, 0110 -gt 6, 0111 -gt 7
  • 1000 -gt 8, 1001 -gt 9, 1010 -gt A, 1011 -gt B
  • 1100 -gt C, 1101 -gt D, 1110 -gt E, 1111 -gt F

32
Example BA16 AB16
  • BA16 AB16
  • 1011 1010 1010 1011
  • 0000000010111010 0000000010101011
  • 0000000010111010
  • 0000000010101011
  • 0000000101100101
  • 0000 0001 0110 0101
  • 016516

33
Alternative algorithmhexadecimal addition table
0 1 2 3 4 5 6 7 8 9 A B C D E F
0 0 1 2 3 4 5 6 7 8 9 A B C D E F
1 1 2 3 4 5 6 7 8 9 A B C D E F 10
2 2 3 4 5 6 7 8 9 A B C D E F 10 11
3 3 4 5 6 7 8 9 A B C D E F 10 11 12
4 4 5 6 7 8 9 A B C D E F 10 11 12 13
5 5 6 7 8 9 A B C D E F 10 11 12 13 14
6 6 7 8 9 A B C D E F 10 11 12 13 14 15
7 7 8 9 A B C D E F 10 11 12 13 14 15 16
8 8 9 A B C D E F 10 11 12 13 14 15 16 17
9 9 A B C D E F 10 11 12 13 14 15 16 17 18
A A B C D E F 10 11 12 13 14 15 16 17 18 19
B B C D E F 10 11 12 13 14 15 16 17 18 19 1A
C C D E F 10 11 12 13 14 15 16 17 18 19 1A 1B
D D E F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C
E E F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D
F F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E
34
Example BA16 AB16
  • (1)
  • BA16
  • AB16
  • ------------
  • 516
  • BA16
  • AB16
  • ------------
  • 16516

35
Hexadecimal subtraction
  • Convert both numbers into binary as described in
    slide 5
  • Convert the second number into its negative by
  • Flipping each bit (0-gt1, 1-gt0)
  • Add binary 1 to the number, remembering the
    following rules
  • 000, 011, 101, 1110
  • 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
  • 1011 1010 1010 1011
  • 0000000010111010 0000000010101011
  • 00000000101110102
  • - 00000000101010112
  • negate 00000000101010112 -gt
  • 1111111101010100
  • 1
  • 1111111101010101
  • 00000000101110102
  • 11111111010101012
  • 00000000000011112
  • 0000 0000 0000 11112
  • 000F16

37
Alternative algorithmhexadecimal subtraction
table borrow not needed borrow needed
0 1 2 3 4 5 6 7 8 9 A B C D E F
0 0 1 2 3 4 5 6 7 8 9 A B C D E F
1 F 0 1 2 3 4 5 6 7 8 9 A B C D E
2 E F 0 1 2 3 4 5 6 7 8 9 A B C D
3 D E F 0 1 2 3 4 5 6 7 8 9 A B C
4 C D E F 0 1 2 3 4 5 6 7 8 9 A B
5 B C D E F 0 1 2 3 4 5 6 7 8 9 A
6 A B C D E F 0 1 2 3 4 5 6 7 8 9
7 9 A B C D E F 0 1 2 3 4 5 6 7 8
8 8 9 A B C D E F 0 1 2 3 4 5 6 7
9 7 8 9 A B C D E F 0 1 2 3 4 5 6
A 6 7 8 9 A B C D E F 0 1 2 3 4 5
B 5 6 7 8 9 A B C D E F 0 1 2 3 4
C 4 5 6 7 8 9 A B C D E F 0 1 2 3
D 3 4 5 6 7 8 9 A B C D E F 0 1 2
E 2 3 4 5 6 7 8 9 A B C D E F 0 1
F 1 2 3 4 5 6 7 8 9 A B C D E F 0
38
Example BA16 - AB16
  • A
  • B 1A16
  • - A B16
  • ------------
  • F16
Write a Comment
User Comments (0)
About PowerShow.com