ECE 190 Lecture 02 January 20, 2011

Bits and Operations on

Lecture Topics  Unsigned and signed integer representations  Conversion between binary and decimal representations  Arithmetic and logical operations on binary numbers  Floating-point 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- 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 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: . 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 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 January 20, 2011

. A B | FUNC 0 0 | 1 0 1 | 0 1 0 | 1 1 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 B | AND 0 0 | 0 0 1 | 0 1 0 | 0 1 1 | 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

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 << o Right shift >>  Examples: o 5 + 10 * 25 / 11 <- this expression will be evaluated using integer arithmetic rounding results to the smaller integer value o 5 | 2 ^ 10 << 2 <- integer values will be treated as bit fields and appropriate bit manipulations will be performed in the order defined by the operators precedence

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

9 V. Kindratenko ECE 190 Lecture 02 January 20, 2011

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.  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

Relation to C  In C language, real numbers are stored using 32-bit or 64-bit IEEE 754 floating point representation o Examples of 32-bit floating point numbers in C: 2.5f, -1.56f <- note f character o Examples of 64-bit floating point numbers in C: 2.5, -1.56 <- note absence of f character  To declare a variable of a floating point type, we use float (for 32-bit) or double (for 64-bit) keywords o Example: float a; <- we declared a variable of type float; it will be stored in computer memory in 32-bit IEEE 754 representation o Example: double b; <- we declared a variable of type double; it will be stored in computer memory in 64-bit IEEE 754 representation

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)

10 V. Kindratenko ECE 190 Lecture 02 January 20, 2011

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 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

Relation to C  In C, bit vector can be represented as unsigned int type  ASCII value can be represented as char data type o char is an 8-bit signed integer o since there are only 256 values in the ASCII table, char is sufficient o example of char values: ‘a’, ‘B’, ‘#’, ‘!’  Char can be used to represent 1- integer values as well: o char 1 byte 8 bits range: -128 -> +127 o unsigned char 1 byte 8 bits range: 0 -> +255  hexadecimal numbers are represented in C using a special notation, 0x, put in front of integer numbers o Examples: 0xAB12, -0x123F

3 fundamental data types in C are int (integer values), float (real values), and char (character)

11 V. Kindratenko