Bits and Operations on Bits

Bits and Operations on Bits

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 Signed magnitude 000 0 001 1 010 2 011 3 100 -0 101 -1 110 -2 111 -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 Signed magnitude 1’s complement 000 0 0 001 1 1 010 2 2 011 3 3 100 -0 -3 101 -1 -2 110 -2 -1 111 -3 -0 2 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 3-1 3-1 -(2) =-410, the largest number is 2 -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; <- we declared a variable of type int; it will be stored in computer memory in 2’s complement representation unsigned modifier put in front of int keyword can be used to indicate that the leftmost bit should not be treated as a sign bit, instead it should be treated as part of the magnitude o Example: unsigned int b; <- we declared a variable b that will be stored in computer memory using (unsigned) magnitude representation If unsigned modifier is not used, signed modifier is implied o int a is the same as signed int a The size of type int (number of bits allocated in computer memory to store a variable of type int) is architecture-dependent. E.g., on a 32-bit architecture, size of int is 32 bits. C allows to specify types of integer numbers that occupy a pre-defined number of bits in memory. This is done with the help of short and long type modifiers: Type Bytes Bits Range short int 2 16 -(2)15 to 215-1, or -32,768 -> +32,767 unsigned short int 2 16 0 to 216-1, or 0 -> +65,535 long int 4 32 -2,147,483,648 -> +2,147,483,647 unsigned int 4 32 0 -> +4,294,967,295 long long int 8 64 unsigned long long int 8 64 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 k-2 1 0 o A10 = ak-2*2 + …a1*2 +a0*2 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 6 5 4 3 2 1 0 o The magnitude is 0*2 +1*2 +1*2 +1*2 +0*2 +0*2 +1*2 =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 k-1 k-2 1 0 the left-most (sign) bit as follows: A10 = -ak-1*2 + ak-2*2 + …a1*2 +a0*2 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: .

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    11 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us