810 likes | 936 Views
Computer Science 1000. Digital Information. What is a number? An arithmetical value, expressed by a word, symbol, or figure, representing a particular quantity – Oxford English Dictionary in other words, a number has two components: quantity: the "amount"
E N D
Computer Science 1000 Digital Information
What is a number? • An arithmetical value, expressed by a word, symbol, or figure, representing a particular quantity – Oxford English Dictionary • in other words, a number has two components: • quantity: the "amount" • representation: how that amount is represented
Number – Example • consider the following quantity of people • how can we represent this number in print? • 7 • the word seven • VII (Roman Numerals) • (tally marks)
Number • the point is, there is more than one way to represent the same quantity • the choice depends largely on the application
Decimal Number System • also referred to as base 10 • a widely used number system, and probably the one you are used to • each number is represented as a sequence of digits: 0 1 2 3 4 5 6 7 8 9 • the quantity of people from the previous slide is represented as: 7
Decimal Number System • why is decimal so widely used? From: A Manual of Greek Mathematics By Thomas Little Heath
Binary Number System • also referred to as base 2 • the system that computers use to store numbers • each number is represented as a sequence of digits: 0 1 • the quantity of people from the previous slide is represented as: 111
Binary Number System • why do computers use a binary number system? • most electronic storage has two states: on/off • RAM: capacitor charged or not charged • HD: polarity of magnetic field • processor: open/closed switch • difficult to store information reliably in more than two states • quantum computers are an exception
Binary Numbers • our upcoming discussion will be to familiarize you with the binary number system (plus a few others) • for this discussion, we will assume that our quantities are: • positive • integral (no fractions) • we will relax these assumptions later on
Binary Number • any positive, integral quantity that can be represented in decimal can also be represented in binary • the rules for representation are very similar in both systems
Decimal Numbers • what does base 10 really mean? • each digit in the number represents a quantity to be multiplied by a power of 10 • these values are all summed together • for example, consider the number 1234 = 1 x 1000 + 2 x 100 + 3 x 10 + 4 x 1 = 1 x 103 + 2 x 102 + 3 x 101 + 4 x 100 1234
Leading Zeroes • suppose I wanted to write 1234 using 5 digits • what is the correct representation? • Answer: 01234 • these are known as leading zeroes • they do not affect the quantity, as their power of 10 is multiplied by zero • in decimal, leading zeroes are typically not reported • for binary applications like computing, leading zeroes are often shown
Binary Numbers • the digits in base 2 represent a similar idea as in decimal • each digit represents a value to be multiplied to a power • however, instead of multiplying to a power of 10, they are multiplied to a power of 2 • for example, consider the binary number 1101 1101 = 1 x 8 + 1 x 4 + 0 x 2 + 1 x 1 = 1 x 23 + 1 x 22 + 0 x 21 + 1 x 20 In other words, the number 1101 in binary is the number 13 in decimal.
Binary Conversion to Decimal: • for each binary digit: • copy the digits vertically on its own line • I typically write the left-most digit on the top, and the right-most digit on the bottom • multiply each digit by a power of two • bottom digit: 20 = 1 • increase by one power for each vertical step • add all values together
Binary Conversion to Decimal: • example: convert 11001 to decimal • Step 1: Write the values vertically • 1 • 1 • 0 • 0 • 1
Binary Conversion to Decimal: • example: convert 11001 to decimal • Step 2: Multiply each value by a power of 2 • 1 x 24 = 1 x 16 = 16 • 1 x 23 = 1 x 8 = 8 • 0 x 22 = 0 x 4 = 0 • 0 x 21 = 0 x 2 = 0 • 1 x 20 = 1 x 1 = 1
Binary Conversion to Decimal: • example: convert 11001 to decimal • Step 3: Add numbers together • 1 x 24 = 1 x 16 = 16 • 1 x 23 = 1 x 8 = 8 • 0 x 22 = 0 x 4 = 0 • 0 x 21 = 0 x 2 = 0 • 1 x 20 = 1 x 1 = 1 • Decimal: 25
Binary Conversion to Decimal: • Example 2: Recall that letters are represented in the computer using numbers. The ASCII value for 'K' is 1001011 in binary. What is this number in decimal?
Binary Conversion to Decimal: • 1 x 26 = 1 x 16 = 64 • 0 x 25 = 0 x 16 = 0 • 0 x 24 = 0 x 16 = 0 • 1 x 23 = 1 x 8 = 8 • 0 x 22 = 0 x 4 = 0 • 1 x 21 = 1 x 2 = 2 • 1 x 20 = 1 x 1 = 1 • Decimal: 75 http://jeroenstaneke.girlshopes.com/binarytoascii/
Converting Decimal to Binary • converting from decimal to binary is straightforward • converting to binary from decimal is a bit more involved • however, it is easily accomplished if you follow the steps
Converting Decimal to Binary – Steps • To convert number X to binary • Repeat following steps until X = 0 • if X is odd, then write a one • if X is even, then write a zero • divide X by 2, and ignore any remainder or fraction Write right-to-left
Example: Convert the number 19 to binary • Repeat following steps until X = 0 • if X is odd, then write a one • if X is even, then write a zero • divide X by 2, and ignore any remainder or fraction X = 19 9 • since 19 is odd, we write a one • we then divide 19 by 2 to get 9.5 • since we ignore the fraction, our new number is 9 1 Result: Remember: Write right-to-left
Example: Convert the number 19 to binary • Repeat following steps until X = 0 • if X is odd, then write a one • if X is even, then write a zero • divide X by 2, and ignore any remainder or fraction X = 19 9 4 • since 9 is odd, we write a one • we then divide 9 by 2 to get 4.5 • since we ignore the fraction, our new number is 4 1 1 Result: Remember: Write right-to-left
Example: Convert the number 19 to binary • Repeat following steps until X = 0 • if X is odd, then write a one • if X is even, then write a zero • divide X by 2, and ignore any remainder or fraction X = 19 9 4 2 • since 4 is even, we write a zero • we then divide 4 by 2 to get 2 • since there is no fraction, our new number is 4 1 0 1 Result: Remember: Write right-to-left
Example: Convert the number 19 to binary • Repeat following steps until X = 0 • if X is odd, then write a one • if X is even, then write a zero • divide X by 2, and ignore any remainder or fraction X = 19 9 4 2 1 • since 2 is even, we write a zero • we then divide 2 by 2 to get 1 • since there is no fraction, our new number is 1 0 1 0 1 Result: Remember: Write right-to-left
Example: Convert the number 19 to binary • Repeat following steps until X = 0 • if X is odd, then write a one • if X is even, then write a zero • divide X by 2, and ignore any remainder or fraction X = 19 9 4 2 1 0 • since 1 is odd, we write a one • we then divide 1 by 2 to get 0.5 • since we ignore the fraction, our new number is 0 0 1 1 0 1 Result: Remember: Write right-to-left
Example: Convert the number 19 to binary • Repeat following steps until X = 0 • if X is odd, then write a one • if X is even, then write a zero • divide X by 2, and ignore any remainder or fraction X = 19 9 4 2 1 0 • since 1 is odd, we write a one • we then divide 1 by 2 to get 0.5 • since we ignore the fraction, our new number is 0 Since X is now zero, we are done. Hence, 19 as a binary number is 10011 0 1 1 0 1 Result: Remember: Write right-to-left
Example: Convert the number 19 to binary • check your work: convert 10011 back to decimal • 1 x 24 = 1 x 16 = 16 • 0 x 23 = 0 x 8 = 0 • 0 x 22 = 0 x 4 = 0 • 1 x 21 = 1 x 2 = 2 • 1 x 20 = 1 x 1 = 1 • Decimal: 19 Result checks out.
Example 2: Convert the number 53 to binary X = 53 26 13 6 3 1 0 110101 Result: • 1 x 25 = 1 x 32 = 32 • 1 x 24 = 1 x 16 = 16 • 0 x 23 = 0 x 8 = 0 • 1 x 22 = 0 x 4 = 4 • 0 x 21 = 1 x 2 = 0 • 1 x 20 = 1 x 1 = 1 • Decimal: 53 Result checks out.
Decimal to Binary – An Alternative Approach • the steps for converting from decimal to binary can also be written as follows: • the two approaches are equivalent, since: • if X is odd, then dividing by 2 produces a remainder of 1 • if X is even, then dividing by 2 produces a remainder of 0 • you are welcome to use either method in your lab • Repeat following steps until X = 0 • divide X by 2 • assign the remainder digit to result
Other Bases • most people use base 10 encodings • computers use base 2 encodings • however, there are two other popular encodings, particularly in computer science • octal – base 8 • hexadecimal (hex) – base-16 • we can extend our previous statement about binary and decimal representations to: • any positive, integral quantity that can be represented in decimal can also be represented in binary, octal, or hex
Octal Number System • also referred to as base 8 • octal numbers have special uses • older computers • file permissions (shown later) • each number is represented as a sequence of digits: 0 1 2 3 4 5 6 7 • the quantity of people, in octal: 7
Octal Numbers • the digits in base 8 represent powers of 8 • for example, consider the octal number 3624 3624 = 3 x 512 + 6 x 64 + 2 x 8 + 4 x 1 = 3 x 83 + 6 x 82 + 2 x 81 + 4 x 80
Octal Conversion to Decimal: • example: convert 3624 to decimal • use our binary converter, but use powers of 8 • 3 x 83 = 3 x 512 = 1536 • 6 x 82 = 6 x 64 = 384 • 2 x 81 = 2 x 8 = 16 • 4 x 80 = 4 x 1 = 4 • Decimal: 1940
Decimal Conversion to Octal • can modify our decimal to binary converter to get octal • however, we typically are more interested in converting octal numbers to binary • it turns out, it is really easy to convert octal numbers to binary • if one then desires the decimal representation: • convert the decimal to binary • convert binary to octal
Octal to Binary • for starters, let's consider the single-digit octal numbers, written in binary • note that we will use leading zeroes, so that they are all three binary digits long
Octal to Binary • the binary representation for the octal number 467 is: • 100110111 • notice anything? • each group of three digits in the binary number make up an octal digit • i.e. 100110111 = 100 110 111 = 467
Converting Octal to Binary – Steps • To convert octal number X to binary • Repeat for each octal digit, left to right • write the three binary digits that correspond to the octal digit • If desired, remove any leading zeroes from the result
Converting Octal to Binary • example: convert octal 7234 to binary • Solution: 111010011100 011 100 111 010 7 2 3 4
Converting Octal to Binary • example: convert octal 1762 to binary • Solution: 001111110010 1111110010 (no leading zeroes) 110 010 001 111 1 7 6 2
Converting Octal to Binary – Steps • To convert binary number X to octal • For each group of three digits in X (right to left) • write the corresponding octal digit from our table (right to left) • if the number of binary digits remaining is 1 or 2, then add leading zeroes to make three digits
Converting Binary to Octal • example: convert binary 111010011100 • Solution: 7234 7 2 3 4 011 100 111 010
Converting Binary to Octal • example: convert binary 1111110010 • Solution: 1762 1 7 6 2 110 010 1 111 001 add leading zeroes
Octal – Uses • one common place where octal numbers are used are for filepermissions in a Linux system • briefly stated, there are three types of file permissions • read: a user can read the contents of a file • write: a user can save contents to the file • execute: a user can execute (run) the file • there are three types of users: • the owner of the file • people in the owner's group • everyone else
Octal – Uses • permissions in files are either yes/no (binary) • hence, we can represent all possible permissions with 9 binary bits • first bit: does the owner of the file have read permission • second bit: does the owner of the file have write permission • third bit: does the owner of the file have execute permission • fourth bit: does the owner's group have read permission • fifth bit: does the owner's group have write permission • sixth bit does the owner's group have execute permission • seventh bit: does everyone have read permission • eighth bit: does everyone have write permission • ninth bit: does everyone have execute permission
Octal – Uses • we can represent these 9 bits using three octal digits • first digit: user permissions • second digit: group permissions • third digit: everyone else's permissions • e.g. suppose that a file has permissions: 750 • in binary: 111 101 000 • file owner has all permissions • owner's group has read and execute permission • everyone else has no permission
Hex Number System • also referred to as base 16 • hex numbers have special uses • memory addresses • colours • like our other systems (decimal, binary, octal), represented as a sequence of digits • 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 .... • what are the other digits?
Hex Number System • hex uses the letters A to F to represent its remaining 6 digits: • 10 in decimal is A in hex • 11 in decimal is B in hex • 12 in decimal is C in hex • 13 in decimal is D in hex • 14 in decimal is E in hex • 15 in decimal is F in hex • e.g. 4E7F represents a hexidecimal number • note that some people write hex in lower case (4e7f) • the quantity of people, in hex: 7
Hex Numbers • the digits in base 16 represent powers of 16 • for example, consider the hex number 4E7F 4E7F = 4 x 163 + E x 162 + 7 x 161 + F x 160 = 4 x 4096 + 14 x 256 + 7 x 16 + 15 x 1
Hex Conversion to Decimal: • example: convert 4E7F to decimal • use our binary converter, but use powers of 16 • 4 x 163 = 4 x 4096 = 16384 • E x 162 = 14 x 256 = 3584 • 7 x 161 = 7 x 16 = 112 • F x 160 = 15 x 1 = 15 • Decimal: 20095