23
CS1800 Summer 2014 Binary Numbers

CS1800 Summer 2014 Binary Numbers. Decimal Integers What does a decimal number like "87294" really mean? More generally

Embed Size (px)

Citation preview

CS1800 Summer 2014

Binary Numbers

Decimal Integers

What does a decimal number like "87294" really mean?

dndn−1...d2d1d0

=dn10n +dn−110

n−1 +...+d2102 +d110

1 +d0100

= dkk=0

n

∑ 10k

8⋅104 + 7⋅103 +2⋅102 +9⋅10 + 4⋅1

8⋅104 + 7⋅103 +2⋅102 +9⋅101 + 4⋅100

More generally

Notation/Vocabulary

• Decimal numbers are written base 10• If we have to distinguish the base, we write it

in as a subscript, e.g. 8729410

• 8729410 is read as “87294 base 10”• We used summation notation on the last slide

and we use it in the text.

dkk=0

n

∑ 10k = "the sum from k equals 0 to n of dk10k"

Integers base b

• If n is a non-negative integer and b is an integer b >= 2, n can be expressed uniquely base b.

• The “digits” must be greater than or equal to 0 and less than or equal to b-1

• This is a polynomial in b with coefficients dk

(dndn−1L d2d1d0 )b=dnb

n +dn−1bn−1 +...+d2b

2 +d1b1 +d0b

0

= dkk=0

n

∑ bk

Base b to Base 10

• 1102 =

• 1108 =

• 11016 =

• 1103 =

• 1105 =

• 11060 =

Binary Integers

• 101102 = 124+023+122+12+0

= 16 + 4 + 2 = 2210

• We can only use 0 and 1.• These are called bits.• Learn the first ten powers of 2.• 1 2 4 8 16 32 64 128 256 512 1024• 210 = 1024 = 1K ~1000 = 103

• 220 = 1,048,576 = ~1000000 = 106

Counting on your Fingers

• http://en.wikipedia.org/wiki/Finger_binary• How high can you count?

Bytes

kilobyte (kB) 103 210

megabyte (MB) 106 220

gigabyte (GB) 109 230

terabyte (TB) 1012 240

A byte is eight bits

A nibble is four bits

Some Arithmetic

• In base 10, multiplying and dividing by 10 is easy• x10 just add a 0 on the right

87294x10 = 872940• /10 just drop the 0 on the right

872940/10 = 87294• The same works base 2 • 101101 times 2 = 1011010• 1011010 divided by 2 = 101101 • What about x4, x8, x16?

Decimal to Binary ConversionMost Significant Bit to Least Significant Bit

• 100510 =• Remember the powers of 2• Find the largest power of 2 less than 100510

• 100510 = 51210 + 49310

= 29 + 49310

= 10000000002 + 49310

• Now we need to find the the binary representation of 49310

100510 = 51210 + 49310

= 51210 + 25610 + 23710 = 51210 + 25610 + 12810 + 10910

= 51210 + 25610 + 12810 + 6410 + 4510

= 51210 + 25610 + 12810 + 6410 + 3210 + 1310

= 51210 + 25610 + 12810 + 6410 + 3210 + 810 + 410 + 110 = 11111011012

• Use 0s as place-keepers for the missing powers of 2

Try a few

• Convert these to Binary• 2910 =

• 7310 =

• 6310 =

• 25810 =

Try a few - Answers

• Convert these to Binary• 2910 = 1610 + 810 + 410 + 110

= 124 + 123 + 122 + 021 + 120 = 111012

• 7310 = 6410 + 810 + 110

= 126 + 025 + 024 + 123 + 022 + 021 + 120 = 10010012

• 6310 = 6410 - 110

= 10000002 - 12 = 1111112

• 25810 = 25610 + 210

= 1000000002 + 102 = 1000000102

How does the card trick work?

1 3 5 7 9 11 13 1517 19 21 23 25 27 29 3133 35 37 39 41 43 45 4749 51 53 55 57 59 61 63

2 3 6 7 10 11 14 1518 19 22 23 26 27 30 3134 35 38 39 42 43 46 4750 51 54 55 58 59 62 63

4 5 6 7 12 13 14 1520 21 22 23 28 29 30 3136 37 38 39 44 45 46 4752 53 54 55 60 61 62 63

 8 9 10 11 12 13 14 1524 25 26 27 28 29 30 3140 41 42 43 44 45 46 4756 57 58 59 60 61 62 63

16 17 18 19 20 21 22 2324 25 26 27 28 29 30 3148 49 50 51 52 53 54 5556 57 58 59 60 61 62 63

32 33 34 35 36 37 38 3940 41 42 43 44 45 46 4748 49 50 51 52 53 54 5556 57 58 59 60 61 62 63

2910 = 0111012

Binary to Decimalnumbers without subscripts are base 10

• By adding powers 101012 = 16 + 4 + 1• By multiply and add101012 = 101002 + 1

= (10102)2 + 1 = ((1012)2 + 0)2 + 1

= (((102)2 + 1)2 + 0)2 + 1

= (((12 + 0)2 + 1)2 + 0)2 + 1----------------------------------------------------- now evaluate = ((( 2 )2 + 1)2 + 0)2 + 1 = (( 5 )2 + 0)2 + 1 = ( 10 )2 + 1 = 21

Simply, start on the left

• 101012 • (12 + 0)• (12 + 0)2 + 1• ((12 + 0)2 + 1)2 + 0• (((12 + 0)2 + 1)2 + 0)2 + 1

Try a few

• 11012 =

• 1101112 =

• 101101012 =

• 1110010110101002 =

Try a few - solutions

• 11012 = ((12 + 1)2 + 0)2 + 1

= ((3)2 + 0)2 + 1 = (6)2 + 1 = 13• 1101112 = ((((12 + 1)2 + 0)2 + 1)2 + 1)2 +

1= ((((3)2 + 0)2 + 1)2 + 1)2 + 1

= (((6)2 + 1)2 + 1)2 + 1 = ((13)2 + 1)2 + 1 = (27)2 + 1 = 55

Try a few - solutions

• 101101012 == ((((((12 + 0)2 + 1)2 + 1)2 + 0)2 + 1)2 + 0)2 + 1= ((((((2)2 + 1)2 + 1)2 + 0)2 + 1)2 + 0)2 + 1= (((((5)2 + 1)2 + 0)2 + 1)2 + 0)2 + 1= ((((11)2 + 0)2 + 1)2 + 0)2 + 1= (((22)2 + 1)2 + 0)2 + 1= ((45)2 + 0)2 + 1= (90)2 + 1 = 181

• 1110010110101002 =

Decimal to Binary ConversionLeast Significant Bit to Most Significant Bit

• 100510 is odd so its binary representation ends in 11005 odd subtract 1 and divide by 2 1

502 even subtract 0 and divide by 2 01

251 odd subtract 1 and divide by 2 101

125 odd subtract 1 and divide by 2 1101

62 even subtract 0 and divide by 2 01101

31 odd subtract 1 and divide by 2 101101

15 odd subtract 1 and divide by 2 1101101

7 odd subtract 1 and divide by 2 11101101

3 odd subtract 1 and divide by 2 111101101

1 odd subtract 1 and divide by 2 1111101101

Try a few

• 1310 =

• 10110 =

• 22010 =

• 99910 =

Try a few - solutions

• 1310 =

• 10710 =

13 odd 112/2 = 6 even 016/2 = 3 odd 1012/2 = 1 odd 1101

101 odd 1100/2 = 50 even 0150/2 = 25 odd 10124/2 = 12 even 010112/2 = 6 even 001016/2 = 3 odd 1001012/2 = 1 odd 1100101

Try a few - solutions

• 99910 = 999 odd 1998/2 = 499 odd 11498/2 = 249 odd 111248/2 = 124 even 0111124/2 = 62 even 0011162/2 = 31 odd 10011130/2 = 15 odd 110011114/2 = 7 odd 111001116/2 = 3 odd 1111001112/2 = 1 odd 1111100111

Addition in Binary

11112

10112+ 12

10112+1002

101

11

12+ 12

11002