ECE 190
Lecture 02
January 20, 2011
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
Homework Posted on the course website (http://courses.engr.illinois.edu/ece190/) Due Wednesday January 26 at 5pm in the ECE190 drop box located in the basement of Everitt Lab
1
V. Kindratenko
ECE 190
Lecture 02
January 20, 2011
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 110 111
Signed magnitude 0 1 2 3 -0 -1 -2 -3
2
1’s complement 0 1 2 3 -3 -2 -1 -0
V. Kindratenko
ECE 190
Lecture 02
January 20, 2011
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 With such annotation, using k bits, we can define 2k-1-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, let’s verify it: 011012 + 100112 01101 10011 100000
3
V. Kindratenko
ECE 190
Lecture 02
January 20, 2011
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
In mathematics, radix complement is the number which, added to the given n-digit number in radix r, results in r0. When using binary numbers, r=2, thus the name of 2’s complement. 1’s complement is the diminished radix complement for r=2.
Relation to C
In C language, integer numbers are stored using 2’s complement representation o Examples of literal values: 2, -5, 128, -10000 o Examples of an expression using integer literal values: 10+20 C allows the programmer to refer to values symbolically, by name. Such symbol names are called variables A variable declaration requires variable type and name To declare a variable of an integer type, we use int keyword o Example: int a; +65,535 -2,147,483,648 -> +2,147,483,647 0 -> +4,294,967,295
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
4
V. Kindratenko
ECE 190
Lecture 02
January 20, 2011
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 Another way to convert a 2’s complement number to decimal is to assign a weight of −2m-1 to the left-most (sign) bit as follows: A10 = -ak-1*2k-1 + ak-2*2k-2 + …a1*21 +a0*20
Decimal to binary conversion using 2’s complement integer format
Convert, the magnitude of a given number to its binary representation using the short division method presented in the last lecture If the number is positive, we are done If the number is negative, find the negative of 2’s complement representation of the number Example: -15610=?2 o Convert its magnitude first: 15610=0100111002 o Since the number is negative, find 2’s complement representation for the number with the opposite sign: -15610=1011001002
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)
5
V. Kindratenko
ECE 190
Lecture 02
January 20, 2011
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 SignEXTension, 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 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 o 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 for some hypothetical function FUNC:
6
V. Kindratenko
ECE 190
Lecture 02
A 0 0 1 1
B 0 1 0 1
| | | | |
January 20, 2011
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
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 o 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 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 0 0 | 0 0 1 | 1 1 0 | 1 1 1 | 1 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 = ?
7
V. Kindratenko
ECE 190
Lecture 02
January 20, 2011
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 0 0 | 0 0 1 | 1 1 0 | 1 1 1 | 0 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 0 | 1 1 | 0 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)
Relation to C
C provides arithmetic and bit manipulation operators for manipulating integer numbers Arithmetic operators include o Addition + o Subtraction o Multiplication * o Division / o Modulus % o Increment ++ o Decrement -Bitwise operators include o Bitwise NOT ~ o Bitwise AND &
8
V. Kindratenko
ECE 190
Lecture 02
January 20, 2011
o Bitwise OR | o Bitwise XOR ^ o Left shift > Examples: o 5 + 10 * 25 / 11