230 likes | 339 Views
CS1800 Fall 2013. Binary Numbers. Decimal Integers. What does a decimal number like "87294" really mean?. More generally. Notation/Vocabulary. Decimal numbers are written base 10 If we have to distinguish the base, we write in as a subscript, e.g. 87294 10
E N D
CS1800 Fall 2013 Binary Numbers
Decimal Integers What does a decimal number like "87294" really mean? More generally
Notation/Vocabulary • Decimal numbers are written base 10 • If we have to distinguish the base, we write 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.
Integers base b • If n is a non-negative integer and bis an integer b>= 2, n can be expressed uniquelybase b. • The “digits” must be greater than or equal to 0and less than or equal to b-1 • This is a polynomial in bwith coefficientsdk
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 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? 2910 = 0111012
Binary to Decimalnumbers without subscripts are base 10 • By adding powers 101012 = 16 + 4 + 1 • By multiply and add 101012 = 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 1
Try a few • 1310 = • 10110 = • 22010 = • 99910 =
Try a few - solutions • 1310 = • 10710 =
Try a few - solutions • 99910 =