Unformatted text preview: ECE 190 Lecture 02 August 26, 2010 Bits and Operations on Bits Lecture Topics • • • • • Unsigned and signed integer representations Conversion between binary and decimal representations Arithmetic and logical operations on binary numbers Floating
point data representation Other representations Lecture materials Textbook chapter 2 ECE 110 notes Homework Posted on the course website (http://courses.engr.illinois.edu/ece190/) Due Wednesday September 1 at 5pm in the ECE190 drop box located in the basement of Everitt Lab 1 ECE 190 Lecture 02 August 26, 2010 Unsigned and signed integer representations Signed
magnitude • So far we defined a way to write positive (unsigned) integer numbers using 0 and 1 bits, but how about writing negative numbers? o We can use half of the distinct patterns made of k bits to represent positive values and the other half to represent negative values Example: with 3 bits we can represent integer numbers from
3 to +3; this leaves us with one 3
bit code not assigned We still need to decide what distinct patterns we should use for representing positive numbers vs. negative numbers o One way, called signed
magnitude, is to use the leading digit to indicate if the number is positive or negative Positive numbers will have 0 as the leading digit Negative numbers will have 1 as the leading digit Binary notation 000 001 010 011 100 101 110 111 Signed magnitude 0 1 2 3
0
1
2
3 The largest positive number is this example is 0112=310 The smallest negative number in this notation is 1112=
310 We still are not using one representation efficiently: 1002 1’s complement • Another way to represent both positive and negative integers, called 1’s complement, is based on the idea that all negative numbers can be represented by flipping digits in the positive numbers o Example: 0102=210. 1012=
210 o In this case, we still are not using one representation efficiently: 1112 o This representation was actually used in some early computers, e.g., CDC 6600 Binary notation 000 001 010 011 100 101 Signed magnitude 0 1 2 3
0
1 1’s complement 0 1 2 3
3
2 2 ECE 190 110 111
2
3 Lecture 02
1
0 August 26, 2010 2’s complement The schema that is actually used in today’s computers is called 2’s complement • • Positive numbers are represented in a straightforward way, with leading digit set to 0 o With k bits, 2k
1
1 positive numbers (from 1 to 2k
1
1) can be represented this way The negative integers are represented by counting backward and wrapping around o Example: 1112=
110, 1102=
210,… o The default rule is that all negative numbers have a leading bit set to 1 Negative numbers are defined in such a way that when added to the positive numbers with the same magnitude, 0 is obtained o This makes implementing digital logic particularly simpler than using any other binary representation Binary notation Signed magnitude 1’s complement 2’s complement 000 0 0 0 001 1 1 1 010 2 2 2 011 3 3 3 100
0
3
4 101
1
2
3 110
2
1
2 111
3
0
1 k
1 With such annotation, using k bits, we can define 2
1 positive numbers, 0, and 2k
1
1 negative numbers, or 2*(2k
1
1) + 1= 2k
1 numbers altogether. What do we do with the unused representation, 1002 in our example? o We note that when we continue counting backward, the next value after 1012=
310 is 1002, and thus it is logical to assume that 1002=
410. Thus, 2’s complement annotation allows us to define numbers in the range from
(2)k
1 to 2k
1
1 o Example: k=3: the smallest number represented in 2’s complement using 3 bits is
(2)3
1=
410, the largest number is 23
1
1=310. • • • How exactly do we get 2’s complement representation for negative numbers? • • Take the positive number A2, flip all its bits (this gives us the complement of A2), and add 1 to the complement of A2; the result is
A2:
A2 = complement(A2)+12 Example: 2’s complements representation for
1310 o Start with the positive number that has the same magnitude: 1310=011012 o Find its complement by flipping every bit: complement(011012)=100102 o Add 1 to the complement: 100102 + 12 = 100112 o Verify that the number we got is really
1310 This should hold: 1310+(
1310) = 010 3 ECE 190 Lecture 02 Let’s verify: 011012 + 100112 01101 10011 100000 August 26, 2010 Note that a carry out of five bits is produced, that is the sum is really 1000002. This carry out bit is always ignored in 2’s complement representation 4 ECE 190 Lecture 02 August 26, 2010 Conversion between binary and decimal representations Binary to decimal conversion for 2’s complement integers • Recall that 2’s complement binary integer number consisting of k bits is written in the form ak
1ak
2…a1a0 where ai is either 0 or 1 o Example: 110001112 Examine the leading bit, ak
1 o If it is 0, then the integer is positive and we can compute its value right away o If it is 1, then the integer is negative and we first need to obtain 2’s complement representation of the positive number whose magnitude is the same
A2 = complement(A2)+12 Compute the magnitude o A10 = ak
2*2k
2 + …a1*21 +a0*20 If the binary number was negative, put “
“ in front of A Example: convert 110001112 to the decimal notation: o Leading bit is set to 1, thus this is a negative number o Find 2’s complement representation of the positive number with the same magnitude: Flip bits and add 12: 001110002+12=001110012 o The magnitude is 0*26+1*25+1*24+1*23+0*22+0*21+1*20=3210+1610+810+110=57 o Apply sign; the answer is
57 • • • • Decimal to binary conversion using 2’s complement integer format • • • The textbook describes a method based on comparing with descending powers of 2 and subtracting A simpler technique is based on short division by two with remainder Overall algorithm: o Convert, the magnitude of a given number to its binary representation Start by writing the number to be converted Divide it by 2 Write quotient of the division underneath the number Write remainder of the division next to the number Repeat last 3 steps until the quotient becomes 0 Write down the sequence of remainders, starting from the bottom, this is your answer o If the number is positive, we are done o If the number is negative, find the negative of 2’s complement representation of the number Example: 15610=?2 • 5 ECE 190 156 0 78 0 39 1 19 1 9 1 4 0 2 0 1 1 0 0 Lecture 02 August 26, 2010 • o The answer is 15610=0100111002 Example:
15610=?2 o The answer is
15610=1011001002 6 ECE 190 Lecture 02 August 26, 2010 Arithmetic and Logical operations on binary numbers Addition and subtraction • Addition of 2’s complement binary numbers is carried out similarly to the decimal numbers: o Proceed from right to left, one digit at a time o At each point we generate a sum digit and a carry Instead of generating carry after 9, as in decimal notation, we generate carry after 1 since 1 is the largest binary digit o Example: 01011 00011 01110 Subtraction is addition preceded by converting the second number to 2’s complement negative: A2
B2 = A2+(
B2) o Example: 011102
010012=011102+(
010012)=011102+101112 Adding number to itself equals shifting bits to the left o Example: 00111011 00111011 01110110 How to add numbers with different number of binary digits? o The numbers need to be converted to the same length o Rule 1: the value of a positive number does not change if we extend the sign bit 0 as many bit positions to the left as needed o Rule 2: the value of a negative number does not change if we extend the sign bit 1 as many bit positions to the left as needed o Since this is the sign bit that gets extended, this operation is referred to as Sign
EXTension, or SEXT o Example: 00001101 00001101 100101 11100101 11110010 Overflow means that the result of adding two numbers cannot be represented in the number of bits allotted. In the example below we add 9+11, but with 5 bits the largest positive number that can be represented is 15. Note that the sign bit of this result is wrong o Example: 01001 01011 10100 • • • • 7 ECE 190 o Lecture 02 August 26, 2010 o Overflow is easy to detect: If the leading bit becomes 1 when adding two positive numbers, overflow has occurred If the leading bit becomes 0 when adding two negative numbers, overflow has occurred Overflow means that the operation we are trying to perform exceeds the capabilities of the machine Logical operations on binary numbers Another class of basic operations that can be performed on binary numbers is logical operations. They are called this way because a bit value of 0 can be interpreted as false and a bit value of 1 can be interpreted as true. • A convenient way to represent behavior of logical operations is to use the truth table that specifies the output of a logical operation for a given set of inputs o Example: A 0 0 1 1 B 0 1 0 1      FUNC 1 0 1 1 • AND binary logical function o requires two source operands, each is a logical variable taking a value of 0 or 1 o the output of AND is 1 only if both operands are 1, otherwise it is 0 o The truth table for AND function is A 0 0 1 1 B 0 1 0 1      AND 0 0 0 1 o o Applying AND function to two bit patterns of m bits each means applying the operation individually to each pair of the corresponding bits Example: 11000110001 AND 11110000011 = ? • 11000110001 11110000011 11000000001 AND function has a unique use: masking out bits that are not needed Masking allows to isolate the bits in a particular location while ignoring the rest of the bits Masking requires a bit pattern, called bitmask, such that bits at the location of interest are set to 1 and the rest of bits are set to 0 The input bit pattern is ANDed with the bitmask resulting in zeroing everything but the bits of interest Example: bits 2 & 3 are of interest in an 4
bit word a3a2a1a0 8 ECE 190 • Lecture 02 August 26, 2010 • • • • a3a2a1a0 1 1 0 0 ← bit mask a3a200 (inclusive
) OR binary logical function o requires two source operands, each is a logical variable taking a value of 0 or 1 o the output of OR is 1 if either of the two operands is 1, otherwise it is 0 o The truth table for OR function is A B  OR 000 011 101 111 o Applying OR function to two bit patterns of m bits each means applying the operation individually to each pair of the corresponding bits, as with AND Example: 11000110001 OR 11110000011 = ? • 11000110001 11110000011 11110110011 (exclusive
) XOR binary logical function o requires two source operands, each is a logical variable taking a value of 0 or 1 o the output of OR is 1 only if two operands are different, otherwise it is 0 o The truth table for XOR function is A B  XOR 000 011 101 110 o XOR has a unique use: two bit patterns are identical if XOR output for them has all zeros NOT function o Is a unary logical operation, it operates only on one operand (input value) o Also known as complement operation o The truth table for NOT function is A  NOT 01 10 o NOT is applied to bit patterns the same way as other operations, bit by bit: Example: NOT 11000110001 = ? • 11000110001 00111001110 AND, OR, and NOT are three fundamental logical functions, any other logical functions can be created from them o Example: XOR can be implemented as (NOT a AND b) OR (a AND NOT b) 9 ECE 190 Lecture 02 August 26, 2010 Floating
point data representation • Using 16
bit 2’s complement data type, we can represent integer numbers in the range from
215 to +215
1. o The precision of this representation is 15 bits o The range of this representation is 215 Issues with this notation o How do we represent a value larger than 215? o How do we represent a non
integer value, say 2.34*10234? Floating point data type is the solution for this problem o Instead of using all the bits to represent the precision of a value, some bits are allocated to store the range of a value Support for floating
point data types is specified in ISA There is an existing standard that is supported by most computer systems, called IEEE Standard for Floating Point Arithmetic, or IEEE 754 The standard defines several types of floating point numbers, we will take a look at what is known as float, which requires 32 bits to represent a number o 1 bit for the sign o 8 bits for the range (the exponent field) o 23 bits for precision (the fraction field), or significant digits in the scientific notation terminology o This notation roughly corresponds to the scientific notation in the form a*10b 1 8 23 • • • • • sign exponent fraction • (−1)s × 1.fraction × 2exponent
127 where 1<= exponent <= 254 o The fraction is normalized, meaning that exactly one non
zero binary digit appears to the left of the binary point Since there is only one such digit, 1, in binary notation, it is ignored in the representation, but is implicitly assumed as always present; this effectively counts towards using 24 bits instead of 23 bits for the fraction representation o The exponent field uses 8 bits to store the exponent as an unsigned number
128 For example, if the exponent is 810, the value stored in the exponent field is 13510: 13510
12710=810 There are two special cases for the exponent field: all 0s and all 1s. 10 ECE 190 • • Lecture 02 August 26, 2010 • For the case of all 0s: (−1)s × 0.fraction × 2
126 For the case of all 1s, the interpretation of the value stored may be either NaN or +/
infinity, depending on the fraction value Example: 00111101100000000000000000000000 o 0 01111011 00000000000000000000000 o Leading bit is 0 – positive number o 011110112=12310, 12310
12710=
4 – exponent o 1. 000000000000000000000002 o Thus, the number is +1*1.0*2
4 = 1/16 11 ECE 190 Lecture 02 August 26, 2010 Other representations of information • Bit vector o A way to use bits to represent things other than numbers, e.g., state of the system with respect of being free or busy o Use n
bit binary pattern, called bit vector, to keep track of the state of some units, with 1 indicating that unit is free and 0 indicating that unit is busy BUSYNESS bit vector as an example Bitmask as another example American Standard Code for Information Interchange (ASCII) o A way to interchange information between computer equipment made by different manufacturers o Each key on the keyboard is identified by its unique ASCII code stored using 8 bits Digit 3 has this ASCII code: 00110011 Lowercase e: 01100101 Carriage return: 00001101 Hexadecimal notation o Is a numeral system with base of 16 o Uses digits from 0 to 9 and characters from A to F to represent a value o Its primary use is for a human
friendly representation of binary coded values It simply reduces the number of digits by a factor of 4 from the binary notation o 16 “digit” symbols used in the hexadecimal notation are sufficient to uniquely represent 4
bit binary patterns Example: • 0011110101101110 0011 1101 0110 1110 3 D 6 E Much easier to remember and work with 3D6F16 number than with the 16
bit binary pattern o Conversion between decimal and hexadecimal notations can be done using division
remainder algorithm similar to the one used for decimal
to
binary conversion, using 16 as the divisor o Conversion between the binary and hexadecimal notation is simple too: groups of 4
bit binary patterns correspond to a unique hexadecimal digit • • 12 ...
View
Full
Document
This note was uploaded on 02/21/2011 for the course MATH 243 taught by Professor Gao during the Spring '08 term at University of Illinois, Urbana Champaign.
 Spring '08
 Gao
 Logic, Negative Numbers

Click to edit the document details