* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Download appa
Survey
Document related concepts
Transcript
Numbering Systems • A numbering system in mathematics specifies the representation of allowable numbers – We describe a numbering system by its base – Base K means that you can use digits 1..K-1 where each digit’s worth is a power of K (K0, K1, K2, K3, K4, etc) • We need to understand numbering systems in computing because computers store and process binary, base 2 – So we have to have some understanding of binary – In these notes, we will concentrate on understanding binary and also introduce two related bases, octal (8) and hexadecimal (16) Binary • The reason what we are interested in binary is that our computers are digital devices – They operate by computing and storing values that are in one of two states: on or off (high current, no or low current) – We will assign 1 and 0 to represent these two states • In binary, base 2, we only have digits 0 and 1 – Each digit in a binary number represents a power of 2 (1, 2, 4, 8, 16, 32, 64, etc) – This takes getting used to especially if you need to convert numbers, for instance when applying a netmask Powers of 2 i 0 1 2 3 4 5 6 7 8 9 10 20 30 40 2i 1 2 4 8 16 32 64 128 256 512 1024 1048576 1073741824 1099511627776 Written in Binary 1 10 100 1000 10000 100000 1000000 10000000 100000000 1000000000 10000000000 100000000000000000000 Not shown Not shown Bits, Bytes, Words • The basic unit of storage in a computer, sometimes called a cell, stores 1 bit (1 binary digit, a 0 or a 1) • As we can’t express much in 1 bit, we group 8 bits together to form 1 byte – In 8 bits, we can store 28 = 256 combinations of 1s and 0s (e.g., 000000002, 010101012, 101000112, 111101012, 111111112) • We denote the base by placing it as a subscript at the end of the number, we can omit the 10 for a decimal number, taking that for granted if there is no subscript – We can store a number from 0 to 255 in 1 byte • Most computers operate on words at a time – The word size is the size of the typical data unit – This will usually be 32 or 64 bits – In 32 bits, we can store 232 different combinations of values – a bit over 4 billion What Do We Want to Store? • We need the ability to store the following types of information in binary – That is, we need to be able to any of the following types of information and find a way to represent them using nothing but 1s and 0s • • • • • • • • positive (unsigned) integer numbers positive and negative (signed) integer numbers numbers with fractional parts (decimal points) strings of characters program instructions images animated images (video) sounds (audio) Unsigned Numbers: Conversion • We will concentrate on the first category, the unsigned integer (0 and positive numbers) • Fortunately there is an easy way to do this – in fact you can express any positive number in any base, all we have to do is learn how to convert between bases (decimal or base 10 and binary or base 2) – We use the following formula to convert a binary 7 number into decimal i x i 0 i *2 Unsigned Numbers: Conversion • Let’s look at examples to understand that formula – We will limit ourselves to 8 bit numbers – 000111012 = 0 * 27 + 0 * 26 + 0 * 25 + 1 * 24 + 1 * 23 + 1 * 22 + 0 * 21 + 1 * 20 = 0 + 0 + 0 + 1 * 16 + 1 * 8 + 1 * 4 + 0 + 1 * 1 = 16 + 8 + 4 + 1 = 29 • notice that 20 = 1 – We can simplify this conversion approach by realizing that 0 * some number = 0 and 1 * some number = that number – Thus 000111012 = 24 + 23 + 22 + 20 = 29 Unsigned Numbers: Conversion • Let’s look at a few more examples – 000111012 = 0 * 27 + 0 * 26 + 0 * 25 + 1 * 24 + 1 * 23 + 1 * 22 + 0 * 21 + 1 * 20 = 16 + 8 + 4 + 1 = 29 – 000011112 = 8 + 4 + 2 + 1 = 15 – 101010102 = 128 + 32 + 8 + 2 = 170 – 111111112 = 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255 – 000000112 = 2 + 1 = 3 • What are the largest and smallest numbers we can store in 8 bits? 000000002 = 0 and 111111112 = 255 – Since 28 = 256, we can store numbers from 0 to 255 Unsigned Numbers: Conversion • How do we convert a decimal number to binary? – There are two approaches worth exploring – This first approach, the division method, is the common approach • Continually divide a number by 2 recording its quotient and remainder, divide the quotient each time until you reach 0 • The binary equivalent is made up of the remainders where the first remainder is the leftmost bit and the last remainder is the rightmost bit Unsigned Numbers: Conversion Number 81 40 20 10 5 2 1 35 17 8 4 2 1 Quotient 40 20 10 5 2 1 0 81 = 1010001 17 8 4 2 1 0 35 = 100011 Remainder 1 0 0 0 1 0 1 1 1 0 0 0 1 Explanation 81 / 2 = 40 and 1 / 2 40 / 2 = 20 and 0 / 2 20 / 2 = 10 and 0 / 2 10 / 2 = 5 and 0 / 2 5 / 2 = 2 and 1 / 2 2 / 2 = 1 and 0 / 2 1 / 2 = 0 and 1 / 2 35 / 2 = 17 and 1 /2 17 / 2 = 8 and 1 / 2 8 / 2 = 4 and 0 / 2 4 / 2 = 2 and 0 / 2 2 / 1 = 1 and 0 / 2 1 / 2 = 0 and 1 / 2 Unsigned Numbers: Conversion • The other approach is a subtraction approach, in essence applying the binary decimal conversion in reverse – Given a number, find the largest power of 2 that can be subtracted from it and do so, continue do to this until you reach 0 – The number is made up of those powers of 2 – Write a 1 in the column for each power of 2 and 0 for all other columns Unsigned Numbers: Conversion • Let’s apply this to the decimal number 219 – Largest power of 2 less than or equal to 219 is 128 • 219 – 128 = 91 – Largest power of 2 less than or equal to 91 is 64 • 91 – 64 = 27 – Largest power of 2 less than or equal to 27 is 16 • 27 – 16 = 11 – Largest power of 2 less than or equal to 11 is 8 • 11 – 8 = 3 – Largest power of 2 less than or equal to 3 is 2 • 3–2=1 – Largest power of 2 less than or equal to 1 is 1 • 1–1=0 – Done – 219 then has a 128, 64, 16, 8, 2, 1 (27, 26, 24, 23, 21, 20) – So 219 = 110110112 Unsigned Numbers: Conversion • Additional examples – Convert 104 to binary: 104 = 64 + 32 + 8, or 104 = 26 + 25 + 23 = 011010002. – Convert 60 to binary: 60 = 32 + 16 + 8 + 4, or 60 = 25 + 24 + 23 + 22 = 001111002. – Convert 255 to binary: 255 = 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 27 + 26 + 25 + 24 + 23 + 22 + 21 + 20 = 111111112. Unsigned Numbers: Conversion • What about larger numbers? Here we will assume 16 bit numbers instead of 8 bit – 00001111000011112 to decimal = 3855 – 56313 to binary = 1101101111111001 – 10101010101010102 to decimal = 43690 – 9999 to binary = 0010011100001111 – 01011111010100002 to decimal = 24400 – 44044 to binary = 1010110000001100 • in 16 bits, we can store 216 = 65536 combinations of numbers that range from 0 to 65535 Signed Numbers • To store a number that could be negative or positive, we need a different representation • The most common signed representation is called two’s complement – There are also one’s complement and signed magnitude – What they all have in common is that the leftmost bit (called the leading or most significant bit) indicates the sign of the number – A sign bit of 0 means the number is positive (or 0) and a sign bit of 1 means the number is negative – We will not explore these here Hexadecimal and Octal • The computer stores billions of bits • If you had to look at memory, you would see an endless list of 0s and 1s – Even a small list of 0s and 1s is hard to focus on – So for convenience, we can convert binary numbers into octal (base 8) or hexadecimal (base 16) – Why these two representations? Because of the relationship between 2 and 8 (23 = 8) and 2 and 16 (24 = 16) Hexadecimal and Octal • Since 3 bits store a number between 0 and 7, which are the legal digits in octal, we will group a binary number into 3 bits – We convert each 3-bit value into its equivalent octal value – We work right to left, if the leftmost bits do not consist of a group of 3, add leading 0s to make it have 3 bits – The table to the right illustrates the conversion from binary to octal or octal to binary Octal 0 1 2 3 4 5 6 7 Binary 000 001 010 011 100 101 110 111 Hexadecimal and Octal • Convert 000111002 to octal – 00 011 100, add a leading 0 – 000 011 100 = 0 3 4 = 0348 • Convert 11111100101101110112 to octal: – 1 111 110 010 110 111 011, add two leading 0s – 001 111 110 010 110 111 011 = 1 7 6 3 6 7 3 = 17636738 • Convert 41548 to 16 bit binary – 100 001 101 100 – add 4 leading 0s – 00001000011011002. • Convert 2608 to 8 bit binary – 010 110 000 = 0101100002 • notice that our answer is really 9 bits • we drop the leading 0 to give us 101100002 Hexadecimal and Octal • When converting between binary and hexadecimal, we have a problem – Hexadecimal, base 16, groups 4 bits together to form a hex digit – In 4 bits, we have numbers 0 to 15 – We do not have individual digits to represent 10 through 15 – So instead, we use letters A-F • See the table to the right Decimal 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Binary 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Hexadecimal 0 1 2 3 4 5 6 7 8 9 A B C D E F Hexadecimal and Octal • Further hexadecimal examples – 101110111111012 • 10 1110 1111 1101, add 2 leading 0s • 0010 1110 1111 1101 = 2 E F D = 2EFD16. – 10110000000102 • 1 0110 0000 0010, add 3 leading 0s • 0001 0110 0000 0010 = 16 0 2 = 160216. – A3B416 = 1010 0011 1011 0100 = 10100011101101002 – 123F16 = 0001 0010 0011 1111 = 010010001111112 Storage Capacities • The bit is the smallest unit of storage but computers do not access individual bits – Computers can either be byte addressable or word addressable • the difference dictates whether the computer can access an individual byte at a time or a full word, most computers are word addressable – As we saw, a byte is 8 bits so it can store 256 different combinations of values – Today’s computers use 32 or 64 bits for a word • a word can then store 232 (about 4 billion) or 264 (well over 16 quintillion!) combinations of values Storage Capacities • How much can we store in n bits? – If we have positive only numbers, then n bits stores numbers between 0 and 2n – 1 – If we have positive and negative numbers, n bits will store numbers between -2n-1 and +2n-1 -1 – For 232, this gives us a range from -2,147,483,648 to +2,147,483,647 – What about 64 bits? You’ll have to use a calculator to compute 264 and 263 Storage Capacities • What about character strings? – We use two popular representations • ASCII – older, stores characters in 7 bits (we don’t use the leading bit so in fact a character stored in ASCII is 1 byte long) – 7 bits gives us 128 different combinations to store the characters including both upper and lower case English letters • Unicode – since many countries do not use English, a representation was needed that can include other sets of characters than the English letters Storage Capacities • Unicode uses 16 bits per character giving us 216 = 65,336 different combinations – Unicode not only includes letters of most languages but a variety of other symbols including smiley faces • In order to make Unicode compatible with ASCII, the first 128 characters of Unicode are the same as the ASCII set – This makes it easy to convert an ASCII file into Unicode by just doubling the storage size of each byte Storage Capacities • Let’s consider an example: – The small, brown mouse ran in 4-ever in his cage to catch up with the cheese! • This sentence comprises 58 letters, 1 digit, 3 punctuation marks and 15 spaces = 77 characters • In ASCII, this would be stored in 77 bytes • In Unicode, this would be stored in 154 bytes Storage Capacities • What about beyond a byte, 2 bytes or word? • We tend to store very large files these days – Large text files of hundreds of thousands of characters – Programs that can range from a few million bytes to billions of bytes – Audio and video files that can be as large as billions of bytes – We use abbreviations to help us understand when we venture beyond a few hundred bytes Storage Capacities Storage in Bytes Abbreviation 1 1 byte 1024 (210) 1 Kilobyte 1 million (220) 1 Megabyte 1 billion (230) 1 Gigabyte 1 trillion (240) 1 Terabyte Equivalent Text 1 character 1000 characters, approximately 200 words in English which is approximately equal to 1 page of text 1 million characters or approximately 1000 pages of text, or between 1 and 5 books of text 1 billion characters would represent a few thousand books, a library A thousand libraries! Notice how each unit increases by 210 Typical main memory sizes are several Gigabytes and hard disks are around or more than 1 Terabyte Storage Capacities Storage in Bytes Abbreviation 1 1024 (210) 1 million (220) 1 billion (230) 1 byte 1 Kilobyte 1 Megabyte 1 Gigabyte 1 trillion (240) 1 Terabyte Equivalent Compressed Image/Video Nothing A 32x32 color image A 1000x1000 color image 2 ½ hours of compressed video, or ¼ of a movie on DVD 250 – 1000 movies (depending on the quality) Here we see the typical size of multimedia files (images, audio, video) Storage Capacities • Sizes for our memories and storage devices – On-chip cache: 16 – 32 Kilobytes for most computers, less or none for handheld devices – Off-chip cache: 1 – 2 Megabytes for most desktop and laptop computers, more for larger computers and less or none for handheld devices – DRAM (main memory): 8 Gigabytes up to 32 Gigabytes for desktop and laptop computers, 1 Terabyte or more for mainframe and supercomputers, under 4 Gigabytes for handheld devices – Flash memory: 1 – 256 Gigabytes, usually in the range of 2 – 8 Gigabytes – Optical disc: 750 Megabytes for CD, up to 8 Gigabytes for DVD, up to 128 Gigabytes for Blu Ray – Hard disk: 500 Gigabytes and 3 Terabyte – Magnetic tape: up to 5 Terabytes per tape Boolean Logic • The computer operates using boolean logic • Boolean logic is the algebra applied to binary values – although in logic, we usually express these values as True or False instead of 1 or 0 • There are four common boolean operations – AND, OR, NOT, XOR • there is also NAND, NOR and COINCIDENCE • These are the operations that the computer can perform on its binary data Boolean Logic • What do these operators do? – AND – outputs 1 if both of the two inputs are 1, otherwise outputs 0 – OR – outputs 1 if either of the two inputs are 1, otherwise outputs 0 – XOR – outputs 1 if the two inputs differ, otherwise outputs 0 – NOT – outputs 1 if the input is 0, otherwise outputs 0 • Not “flips” a bit Truth tables to demonstrate the result of each operator X 0 0 1 1 Y 0 1 0 1 X AND Y 0 0 0 1 X OR Y 0 1 1 1 X XOR Y 0 1 1 0 X 0 1 NOT X 1 0 Applying a Boolean Operation • Most of the Boolean operators operate on two binary numbers (NOT operates on a single number) • Perform the operation bit-wise – Line the two binary numbers up, one under the other, and apply the operator on the pair of bits in each column – Here we see AND being applied to 00111111, 01010101 Applying a Boolean Operation 00111111 OR 0 1 0 1 0 1 0 1 01111111 00111111 XOR 0 1 0 1 0 1 0 1 01101010 NOT 0 1 0 1 1 0 1 0 = 1 0 1 0 0 1 0 1 Here we see examples of applying netmasks (see chapter 12) AND 00000000.00000000.00001111.11111111 00001010.00001011.00001100.00001101 00000000.00000000.00001100.00001101 = 0.0.12.13 00000000.00000000.00001111.11111111 AND 10000000.00111010.11011101.00100111 00000000.00000000.00001101.00100111 = 0.0.13.39 Parity • Information in the computer is moved from component to component over different buses • Information should be reliably sent and received but mistakes can occur (rarely) – we would like to add an error checking mechanism – the simplest approach is known as the parity bit • The parity bit is added to each byte so that we have 9 values to send – there are two forms of parity, even and odd, we will assume even • In even parity, the number of 1 bits in the byte and parity bit must always be even – if odd, we have an error Parity • For example, 10101110 has 5 1 bits which is an odd number • We would add a 1 for a parity bit giving us – 10101110 1 • Now we have an even number of 1 bits (6) • If a device receives the 9 bits and there is an odd number of 1 bits, the device knows an error occurred somewhere – With this approach, we can determine an error but not which bit is erroneous – We also can detect 1 error but not 2 or more Parity • To compute the parity bit, XOR each pair of bits together and then XOR the results together until you reach one bit – this will be the parity bit • To determine if an error has occurred with 9 bits, do the same with the first 8 bits and the result should equal the 9th bit, if not, an error occurred