THE BINARY SYSTEM
HOW A COMPUTER STORES DATA A computer is an information-processing machine that works by converting all kinds of information into binary numbers (1s and 0s). A computer treats any type of information (numbers, letters, words, etc.) as if it consisted simply of binary ones and zeros. For example, a computer can translate the letter “A” typed into its keyboard into a string of 1s and 0s, such as 1000001. One reason to do this is that once in binary form; the information can be stored and moved about more easily. For example, on a hard disc, the 1s could be stored as magnetized spots on the disc, while the 0s can be stored as unmagnetized spots. Once information has been converted to 1s and 0s, the computer can work on it. All of a computer’s functions are based on the movement and transformation of electrical pulses (representing 1s and 0s) in electrical circuits. Inside the computer are electrical circuits that perform computations on the 0s and 1s, such as adding and subtracting them.
The Decimal/Denary Number System (Base 10) The decimal number system that people use every day contains ten digits, 0 through 9. Start counting in decimal: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, Units/Ones 1 2 3 4 5 6 7 8 9 Oops! There are no more digits left. How do we continue counting with only ten digits?
The Decimal/Denary Number System We add a second column of digits, worth ten times the value of the first column. Start counting again: 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 Tens Ones 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 (Note that the right column goes back to zero here.), 21, 22, 23, ... , 94, 95, 96, 97, 98, 99,
The Decimal/Denary Number System Oops! Once again, there are no more digits left. The only way to continue counting is to add yet another column worth ten times as much as the one before. Continue counting: 100, 101, 102, ...997, 998, 999, 1000, 1001, 1002, .... H T O Th H T O 1 0 0 1 0 0 0 1 0 1 1 0 0 1 . . 9 9 9 9 9 9 9 You should get the picture at this point!
The Decimal/Denary Number System Example, the decimal number 365 can be written in expanded notation as: H T O 102 101 100 (3×100) + (6×10) + (5×1) Th H T O 1032= (1×1000) + (0×100) + (3×10) + (2×1). By writing numbers in this form, the value of each column becomes clear.
The Binary System/Base 2 The binary number system works in the exact same way as the decimal system, except that it contains only two digits, 0 and 1. Start counting in binary: 0, 1, Oops! There are no more binary digits. In order to keep counting, we need to add a second column worth twice the value of the column before. We continue counting again: 10, 11, Oops! It is time to add another column again. Counting further: 100, 101, 110, 111, 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111.... Watch the pattern of 1's and 0's. You will see that binary works the same way decimal does, but with fewer digits.
The Binary System/Base 2 Binary uses two digits, so each column is worth twice the one before. 27 26 25 24 23 22 21 20 128 64 32 16 8 4 2 1 N.B. Any number to the power 0 is 1 A A1 A = 1 & A1 = A (1-1) = A 0 = 1
Decimal (Base 10) Binary (Base 2) 0 one 1 1 one 2 10 0 one, 1 two 3 11 1 one, 1 two 4 100 0 one, 0 two, 1 four 5 101 1 one, 0 two, 1 four 6 110 0 one, 1 two, 1 four 7 111 1 one, 1 two, 1 four 8 1000 0 one, 0 two, 0 four, 1 eight
Conversion of a binary number to a decimal number To convert a number from binary to decimal, simply write it in expanded notation. For example, the binary number 101101 can be rewritten in expanded notation as 27 26 25 24 23 22 21 20 128 64 32 16 8 4 2 1 1 0 1 1 0 1 (1×32) + (0×16) + (1×8) + (1×4) + (0×2) + (1×1)= 32+0+8+4+0+1= 45 By simplifying this expression, you can see that the binary number 101101 is equal to the decimal number 45.
Converting from decimal to binary We use the method of repeated division by 2 to convert numbers from decimal to binary. Usually, when dividing a number by 2 the last binary digit (singles digit) must be a 1- the LSB. An even number will have a zero as the singles digit- the MSB. So to convert a Base 10 number to Base 2 the number is repeatedly divided by 2 until we have reached the point of 0 R 1. The remainders give us the binary digits.
Example: Convert decimal 25 to binary:- 25 ÷ 2 = 12 Remainder 1 12 ÷ 2 = 6 Remainder 0 6 ÷ 2 = 3 Remainder 0 3 ÷ 2 = 1 Remainder 1 1 ÷ 2 = 0 Remainder 1 1 1 0 0 1 The number 25 in decimal is equal to 110012 MSB LSB
QUESTIONS: Convert the following decimal numbers to binary. Use 8 bits to represent the numbers in base 2 16 182 97
BINARY ARITHMETIC OPERATIONS Binary numbers can be added, subtracted, multiplied or divided just like decimal numbers. ADDITION RULES!! 0 + 0 = 0 0 + 1 = 1 1 + 0 = 1 1 + 1 = 1 0 1 + 1 + 1 = 1 1 Carry bits
BINARY ADDITION To view binary addition: http://www.youtube.com/watch?v=Cqs90dhw_E4
Note, the fourth rule in Binary Addition involves a ‘carry’ operation: 21 20 1 + 1 = 1 0 The place value is increased by 2! This is similar to decimal where the place value is increased by ten. For example: 101 100 9 + 1 = 1 0
Base 2 Addition questions 111 111 111 101 + 110 + 111 + ______ _____ _____
HOME-WORK Read pages 57-58: Log On to It for CSEC (2nd Ed.):- Converting a decimal number to binary Adding binary numbers Do Exercise 1 Quest. 3, 4, 5 (Pg 59) Test Next Day!!!!!!!
REPRESENTATION SCHEMES In computing, signed number representations are required to encode negative numbers in binary number systems. In maths, negative numbers in any base are represented by prefixing them with a − sign. However, in computer hardware, numbers are represented in binary only without extra symbols, requiring a method of encoding the sign of the number.
REPRESENTATION SCHEMES Four methods that are used to represent signed numbers are: Sign-and-Magnitude One’s complement Two’s Complement Binary Coded Decimal (BCD)
SIGN AND MAGNITUDE The simplest way to indicate negation is signed magnitude .One may first approach the problem of representing a number's sign by allocating one sign bit to represent the sign: set that bit (often the most significant bit) to 0 for a positive number, and set to 1 for a negative number. The remaining bits in the number indicate the magnitude. Some early binary computers (e.g. IBM 7090) used this representation
SIGN AND MAGNITUDE Consider a number with n bits: The left-most bit represents the sign of the number +/- sign. "0" indicates that the number is positive, "1" indicates negative. The remaining n-1 bits represent the magnitude (size) of the number.
Representation of positive and negative numbers using Sign & Magnitude BINARY DECIMAL 0000 +0 0001 +1 0010 +2 0011 +3 0100 +4 0101 +5 0110 +6 0111 +7 1000 -0 1001 -1 1010 -2 1011 -3 1100 -4 1101 -5 1110 -6 1111 -7 What observations have you made from the table?
If you observed that there are two values for zero: +0 and -0 then you are RIGHT!
Example 1: Represent 45 in binary using Sign and Magnitude: 4510 = 001011012 Therefore +45 (S & M) = 00101101 0 (MSB) = +ve in Sign and Magnitude
Example 2: Represent -45 in binary using Sign and Magnitude: 4510 = 001011012 Therefore -45 (S & M) = 10101101 1 (MSB) = -ve in Sign and Magnitude
Example 3: Represent 1011 in decimal using Sign and Magnitude. 1 = -ve in Sign & Magnitude 011=(1x2) + (1x1) = 3 Therefore 1011 = -3 (using Sign & Magnitude) 22 21 20 4 2 1
Example 4: Represent 0011 in decimal using Sign and Magnitude. 0 = +ve in Sign & Magnitude 011=(1x2) + (1x1) = 3 Therefore 0011 = +3 (using Sign & Magnitude) 22 21 20 1
Questions 1. using Sign & Magnitude Representation, find the binary representation of the numbers: (a) 25 (b) -25 (c) 63 (d) -120 2. What is the decimal equivalent of the following binary numbers (using S & M scheme)? (a) 1000101 (b) 01000101 (c)11000101
HOME-WORK 1. Using Sign & Magnitude Representation, find the binary representation of the numbers: (a) 64 (b) -96 (c) 96 (d) -156 2. What is the decimal equivalent of the following binary numbers (using S & M scheme)? (a) 0010101 (b) 01100101 (c)10000001 * Read Page 55 (blue book) or pg 60 (purple book)- One’s & Two’s Complement
One’s Complement In one's complement, positive numbers are represented as usual in regular binary. Example: 5 in binary is 00000101. The One’s complement of 5 would be: 00000101 However, negative numbers are represented differently. To negate a number, replace all zeros with ones, and ones with zeros - flip the bits. One consequence of this is that there are two values for zero: +0 and -0.
For example, for a four-bit number (remember, the leading -- that is, leftmost -- bit is a sign bit) using 1’s Complement: Binary: 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Decimal: +0 +1 +2 +3 +4 +5 +6 +7 -7 -6 -5 -4 -3 -2 -1 -0
Questions Represent the following numbers in binary (using 8 bits) using 1’s complement: -108 23 4 -120
HOME-WORK 1. Represent the following decimal numbers using 1’s complement: -95 -36 100 2. Find the One’s Complement of: 1011001 0001010
TWO’S COMPLEMENT Two's complement is the way every computer chooses to represent integers. Using two's complement as the method for representing negative numbers allows us to have only one representation of zero, and to have effective addition and subtraction while still having the most significant bit as the sign bit. The leading bit of a number is the sign bit. Positive numbers have a zero as the leading bit; negative numbers have a one as the leading bit. There is only one value for zero: all bits are zero (as opposed to the system used for One's Complement Overflow can occur (if numbers are of the same sign)
For example, for a four-bit number (remember, the leading -- that is, leftmost -- bit is a sign bit) USING 2’S COMPLEMENT Binary: 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Decimal: +1 +2 +3 +4 +5 +6 +7 -8 -7 -6 -5 -4 -3 -2 -1
TWO’S COMPLEMENT In two's complement, positive numbers are represented as usual in regular binary. Example: 5 in binary is 00000101. The Two’s complement of 5 would be: 00000101 NO CHANGE!!
FINDING TWO’S COMPLEMENT (-VE NUMBERS) Convert the number to binary Find one’s complement Add 1 to the 1’s complement
Example: Suppose we're working with 8 bits and we want to find how -28 would be expressed in two’s complement notation: STEP 1: First we write out 28 in binary form. 28 = 00011100 STEP 2: Then we invert the digits. 0 becomes 1, 1 becomes 0. Therefore the 1’s Complement of: 00011100 = 11100011 STEP 3: Add 1 to the 1’s Complement: 11100011 + 1 = 11100100 Therefore, -28 = 11100100
QUESTIONS Represent the following numbers in binary (using 8 bits) in 2’s complement: -108 6 -123
QUESTIONS Find the two’s complement representation of the following using 8 bits: 1011 11001
HOME-WORK Represent the following numbers in binary (using 8 bits) in 2’s complement: -64 7 Find the two’s complement representation of the following using 8 bits: 101010 11101
HOME-WORK Cont’d Read Pg 62 -63 (Log On to IT for CSEC 2nd Ed.)- BCD and Data Representation. Review Sign and Magnitude, 1’s Complement and 2’s Complement and Addition for Test next day!
Adding a negative number- using Two’s complement Example: Find the value of 9-6 using four-bit two’s complement binary. 9 – 6 could also be written as 9 + (-6)
1. Convert 9 to binary 9= 10012 2. Find the Two’s Complement of -6: 6= 0110 1’s Complement = 1001 Add 1 = 1001 +1 = 1010 Therefore -6 (2’s Comp.) = 1010 3. Add 9 to -6 = 1001 + 1010 = 10011 Discard the extra bit because we are using 4-bit representation. 4. 9-6 (using two’s compl.) = 00112
HOME-WORK Find the value of the following using eight-bit two’s complement: 15-8 50-25 -8+12 Read Pg. 62-63 (Log On to IT for CSEC, 2nd Ed.: BCD and ASCII
BINARY CODED DECIMAL- BCD BCD is an encoding system for decimal numbers in which each digit is represented by its own binary sequence. In BCD, a digit is usually represented by four bits which, in general, represent the decimal digits 0 through 9. Other bit combinations are sometimes used for a sign.
BCD 0000 1 0001 2 0010 3 0011 4 0100 5 0101 6 0110 7 0111 8 1000 9 1001 + 1010 - 1011
Example 1: What is the BCD representation of -325? - = 1011 3 = 0011 2 = 0010 5 = 0101 Therefore 325 = 1011 0011 0010 0101
Example 2: What is the BCD representation of 325? + = 1010 3 = 0011 2 = 0010 5 = 0101 Therefore -325 = 1010 0011 0010 0101
Questions: What is the BCD representation of the following decimal numbers? 56 -63 -4 1005
DATA REPRESENTATION Two of the most common codes used to represent character sets in computers are: 1. ASCII (American Standard Code for Information Interchange). 2. EBCIDIC (Extended Binary Coded Decimal Interchange Code).
ASCII CODE 65-90 A to Z 97-122 a to z
Example 1: The ASCII code for the letter A is 1000001. Determine the 7-bit ASCII code for the letter G. A = 1000001 G is the 6th letter from A 6 = 110 Therefore: 1000001 + 110= 1000111 *see text book for other methods
Questions 1. What letter is represented by the binary representation 1001101 using ASCII? 2. The ASCII representation for the letter t is 1110100. What is the ASCII representation of the letter y? 3. Determine the ASCII representation of : (i) G (ii) d (iii) R