32
Binary numbers

Binary numbers

  • Upload
    garvey

  • View
    24

  • Download
    3

Embed Size (px)

DESCRIPTION

Binary numbers. Primary memory. Memory = where programs and data are stored Unit = bit “BIT” is a contraction for what two words? Either a 1 or a 0 (because computers are made primarily of transistors which act as switches). - PowerPoint PPT Presentation

Citation preview

Page 1: Binary numbers

Binary numbers

Page 2: Binary numbers

Primary memory

• Memory = where programs and data are stored

– Unit = bit• “BIT” is a contraction for what two words?• Either a 1 or a 0 (because computers are made

primarily of transistors which act as switches).

– Smallest addressable (i.e., can be read from or written to memory) unit is the byte.

• byte = 8 bits (an 8-bit number)

Page 3: Binary numbers

Binary numbers

• How do computers represent numbers and do arithmetic?

• How do we? What digits do we use? Why?– What is 903?

• 9 is the MSD above; 3 is the LSD.

Page 4: Binary numbers

Binary numbers

• Can we represent numbers is other bases (radix)?– What is 11012 in base 10?

– What is 110110 in base 10?

– What is 11013 in base 10?

– Note that they are different!

Page 5: Binary numbers

Potential programming asssignment

Write a program that reads in a string (not an int) representing a binary number and outputs the corresponding base 10 number.

Do not use any methods from Java’s Integer class!

Page 6: Binary numbers

Potential programming asssignment

Write a program that reads in a string (not an int) (representing a # in base b), reads in the base b (an int), and outputs the corresponding base 10 number.

(What Java String method can be used to obtain an individual char at a particular position in a string?)

Do not use any methods from Java’s Integer class!

Page 7: Binary numbers

Binary numbers

• What are the range of digits for base 10 numbers?– For binary?– For base 5?– For some arbitrary base k?

– Is 11022 a valid base 2 number?

– Is 12644 a valid base 4 number?

Page 8: Binary numbers

Numbers in mathematics

• I = the integers– Finite or infinite?

• R = the reals– Contains rational numbers like 2/7 and irrational

numbers like – Finite or infinite?

• C = the complex numbers– Finite or infinite?

Page 9: Binary numbers

Numbers in computers

• All represented by a finite sequence of bits (1’s and 0’s).

• I = the integers– Finite/bounded.– char (8 bits), short (16 bits), int (32 bits), long long

(64 bits)

• R = the reals– Finite/bounded. (So what about ?)– float (32 bits), double (64 bits), long double (128 bits)

Page 10: Binary numbers

Numbers in computers

• R = the reals– Finite/bounded. (So what about ?)– Representation is called floating point.– float (32 bits), double (64 bits), long double (128

bits)– Can also be represented by fixed point and BCD

(binary coded decimal).

Page 11: Binary numbers

Common ranges of values (Java)

• int -2 billion to +2 billion

• double 4.9x10-324 (closest to 0)to 1.8x10+308

• float 1.4x10-45 (closest to 0) to 3.4x10+38

Page 12: Binary numbers

Converting from base 10 to base 2

• Convert 6110 to base 2.

• Method 1: Subtract largest power of 2 repeatedly.– Also works for both ints and floats.– First, make a table of powers of 2.

• 28 = 256• 27 = 128• 26 = 64• 25 = 32• 24 = 16• 23 = 8• 22 = 4• 21 = 2• 20 = 1

Page 13: Binary numbers

Converting from base 10 to base 2• Convert 6110 to base 2.

• Method 1: Subtract largest power of 2 repeatedly.1. Make a table of powers of 2.

• 28 = 256• 27 = 128• 26 = 64• 25 = 32• 24 = 16• 23 = 8• 22 = 4• 21 = 2• 20 = 1

2. Repeatedly subtract(largest power of 2)

msb

lsb

Reading forward, the result is 1111012. 0

211

201

214

5

218

13

2116

29

2132

61

0

1

2

3

4

5

Page 14: Binary numbers

Use this method to convert from base 10 to base 3.

• Method 1: Subtract largest powers of 3 repeatedly.Step 1: Create table of powers of 3.

• 34 = 81• 33 = 27• 32 = 9• 31 = 3• 30 = 1

Step 2: Repeatedly subtract (largest) multiples of largest power of 3. (Why? Recall digit range for a particular base.)

Page 15: Binary numbers

Use this method to convert from base 10 to base 3.

• Method 1: Subtract largest powers of 3 repeatedly.Step 1: Create table of powers

of 3.• 34 = 81• 33 = 27• 32 = 9• 31 = 3• 30 = 1

Step 2: Repeatedly subtract (largest) multiples of largest power of 3.

• Convert 6110 to base 3.

msd

lsd

Reading forward, the result is 20213.

0

311

1

326

307

3254

61

0

1

2

3

Page 16: Binary numbers

Potential programming assignment

Write a program that takes as input a base 10 number (an int) and outputs the corresponding base 2 number.

Do I need to remind you again? Do not use any methods from Java’s Integer class!

Page 17: Binary numbers

Converting base 10 floating point numbers to base 2 using Method 1 (repeated subtraction)

Step 1: Create table of powers of 2.24 = 1623 = 822 = 421 = 220 = 12-1 = 0.52-2 = 0.252-3 = 0.1252-4 = 0.06252-5 = 0.031252-6 = 0.015625

Step 2: Subtract largest power of 2 repeatedly.

Page 18: Binary numbers

Converting base 10 floating point numbers to base 2 using Method 1 (repeated subtraction)

Step 1: Create table of powers of 2.24 = 1623 = 822 = 421 = 220 = 12-1 = 0.52-2 = 0.252-3 = 0.1252-4 = 0.06252-5 = 0.031252-6 = 0.015625

Step 2: Subtract largest power of 2 repeatedly.

Convert 11.07812510 to base 2.

Reading forward, the result is 1011.0001012.

0

21015625.0

20015625.0

210625.0

20

20

20078125.0

211

078125.1

212

20078125.3

218

078125.11

6

5

4

3

2

1

0

1

2

3

Page 19: Binary numbers

Conversion methods

Method 1. Using a table, repeatedly subtract largest multiple.- directly works for both ints and floats- requires a table

Method 2: Repeated (integer) division (with remainder) by desired base.- works for ints but needs to be “modified” to work for floats- doesn’t require a table

Page 20: Binary numbers

Method 2: Repeated (integer) division (with remainder) by desired base.

Convert 6110 to base 2. lsb

msb

Reading backwards, the result is 1111012.

12%102/1

12%312/3

12%732/7

12%1572/15

02%30152/30

12%61302/61

Page 21: Binary numbers

Method 2: Repeated (integer) division (with remainder) by desired base.

Convert 6110 to base 3.

lsd

msd

Reading backwards, the result is 20213.

23%203/2

03%623/6

23%2063/20

13%61203/61

Page 22: Binary numbers

Method 2 for fp numbers

• Convert 61.687510 to base 2.– We just saw how to convert 6110 to base 2 (by

repeated division with desired base).– So all we need to do is convert 0.687510 to base 2.

– This can (analogously) be accomplished by repeated multiplication by the desired base.

Page 23: Binary numbers

Method 2

• Convert 0.687510 to base 2 by repeated multiplication by desired base.

• Then read integer part forwards.

210 1011.06875.0

0.020.0

0.020.0

0.125.0

5.1275.0

75.02375.0

375.126875.0

Page 24: Binary numbers

Method 2

• Convert 0.110 to base 2 by repeated multiplication by desired base.

• Then read integer part forwards.

Do you see the repetition/loop?

210 ...000110011.01.0

4.022.0

2.126.0

6.128.0

8.024.0

4.022.0

2.021.0

Page 25: Binary numbers

Method 2

• Convert 0.110 to base 2 by repeated multiplication by desired base.

• Then read integer part forwards.

Do you see the repetition/loop?

210 ...000110011.01.0

4.022.0

2.126.0

6.128.0

8.024.0

4.022.0

2.021.0

same

same

Page 26: Binary numbers

Converting between arbitrary bases

• In general, to convert from base u to base v, we typically:– Convert from base u to base 10.– Then convert from base 10 to base v.

• When converting from base 2 to two other important bases, we can skip the intermediate conversion through base 10 and convert directly from base 2 to these two other bases (and vice versa).

Page 27: Binary numbers

Other important bases related to base 2: base 8

• Octal, base 8– Digits: 0..7

base 8 base 20 0001 0012 0103 0114 1005 1016 1107 111

• Problems:– Convert 1008 to base 10.

– Convert 1008 to base 2.

– Convert 11012 to base 10.

– Convert 11012 to base 8.

– Convert 7028 to base 10.

– Convert 7028 to base 2.

– Convert 8028 to base 2.

Page 28: Binary numbers

Other important bases related to base 2: base 16

• Hex (hexadecimal), base 16– Digits: 16 of them (more than base 10) so we’ll will need 6 extra symbols: {0..9,a..f}

base 16 base 10 base 2 base 80 0 0000 001 1 0001 012 2 0010 023 3 0011 034 4 0100 045 5 0101 056 6 0110 067 7 0111 078 8 1000 109 9 1001 11a 10 1010 12b 11 1011 13c 12 1100 14d 13 1101 15e 14 1110 16f 15 1111 17

Page 29: Binary numbers

Problems:

• Convert 7258 to base 2 to base 16 to base 10.

Page 30: Binary numbers

Problems:

• Convert 7258 to base 2 to base 16 to base 10.– 111 010 1012

– 1 1101 01012 = 1d516

– 1x162 + 13x161 + 5x160 = 46910

Page 31: Binary numbers

Convert 153.51310 to base 8.Approach: convert 153 and 0.513

separately.

208/2

328/19

1198/153

984.18248.0

248.58656.0

656.68832.0

832.08104.0

104.48513.0

So the result is 231.40651… base 8.

remainder

Page 32: Binary numbers

Problem: Convert 3115 to X10 to Y2.

• 3x52+1x51x1x50 = 75+5+1 = 8110.

8111664

1010001

msb 102/1

012/2

122/5

052/10

0102/20

0202/40

lsb 1402/81

2