Number Systems (2.1.1) Concept of a register. Operations of register, Complementation, Ranges, Left and right shifts, Addition of two binary number, Numerical overflow, 2 s complement representation, Binary addition and subtraction (by complementation followed by addition) in 2 s complement. Concept of a Register A register is a small amount of very fast computer memory used to speed up the execution of computer programs. These are used to offer storage for most commonly used values, so they can be quickly accessed when they are needed. Modern computers work by loading information into the registers, operating on this data, and then loading it back to the main memory. This is often referred to as load-store architecture. There are various types of registers such as data-register, address-register and instruction-register which will be discussed in the CPU section. Ms. P. Sullivan Page 1 of 7
Complementation Addition can also be used on binary numbers. The following are some basic additions and their results: 0+ 0+ 1+ 1+ 0 1 0 1 0 1 1 10 Please note that 1 2 + 1 2 + 1 2 = 11 2. If we add 1 10 + 1 10 + 1 10 using decimals, the answer would be 3 10. If we convert 3 10 to binary it would be represented as 11 2. The following are some addition examples. 100+ 1001+ 111+ 011 0011 001 111 1100 1000 In the third example 111 2 + 001 2 the result is 1000 2 this is one bit more than any of the original numbers, which are made up of three bits. In this case the correct answer 1000 2 is noted down, but in some cases where the number of bits is restricted a numerical overflow occurs, which means that more bits than there are available will be used. Complementation is a method of representing positive and negative numbers. This system requires numbers to be represented by a fixed number of bits. There are two forms of complementation, one s complement and two s complement. Two s complement is preferred because subtraction can be performed by adding the two s complement of the number to be subtracted. One s complement is used to represent negative numbers. Assume that the number 45 10 will be used for this example. 45 10 in binary using 8 bits can be noted down as 00101101 2. However, if the number 45 10 was to change to -45 10, by using one s complement its binary representation must also be changed. This can be changed by simply converting the number, that is 0s will become 1s and 1s will become 0s. So, in this case 00101101 2 will become 11010010 2. Ms. P. Sullivan Page 2 of 7
In two s complement apart from inverting the bits, the result is increased by one. So by using the previous example to represent -45 10, first forty-five (in binary) 00101101 2 is changed using one s complement, i.e. 11010010 2. Then a one is added, which makes the number 11010011 2. This makes it easier to subtract numbers in binary. It is imperative that both numbers have the same amount of bits. One cannot subtract two numbers if one is made up of 6-bits, and the other of 8-bits. In which case the number with 6-bits, must have two zeros added in front before any complementation to become an 8-bit number. Example 1 30 10 17 10 in binary using 8 bits can be worked out as follows. First both decimal numbers are converted to binary. 30 10 00011110 2 17 10 00010001 2 Next, two s complement is applied on the negative number which in this case is 17 10. 00010001 2 11101110 2 + 1 2 11101111 2 Now, the final step is to add the two numbers. 00011110 2 + 11101111 2 100001101 2 Now since we have started with a register of 8 bits the result must also have 8 bits. So the first number is dropped, so the answer is 00001101 2. Ms. P. Sullivan Page 3 of 7
Ranges Ranges are used to determine the lowest and the highest values which can be represented using a fixed amount of bits. This is commonly used in memory addressing, in which by having a register size, the maximum and minimum number of locations can be derived. In this case, we will discuss two types of ranges, one using normal binary, and the other using two s complement. Range using Normal Representation What is the range of numbers which can be represented using 8-bits? Since we have 8 Bits, the maximum value represented in binary is 11111111 2 and the smallest is 00000000 2. If we convert these numbers to decimal we end up with: 128 64 32 16 8 4 2 1 1 1 1 1 1 1 1 1 So, in this case the biggest number is 128+64+32+16+8+4+2+1 which is equal to 255 10. The smallest number is 0 10. Therefore in this case, the range is from 0 to 255. Range using Two s Complement What is the range of numbers which can be represented using 8-bits in two s complement? Since, the first number in two s complement identifies the number as a negative number the highest possible positive number is 01111111 2, which when converted to decimal is 127 10 as shown in the working below. 128 64 32 16 8 4 2 1 0 1 1 1 1 1 1 1 Now, since we have two s complement, we ll need to find the smallest number as well. This can be easily done, by simply converting the positive number to negative so in this case is -127 10. Now the range is from -127 to 126. Notice that for the highest range, one has been subtracted from the value; this is done to compensate for the zero value. Ms. P. Sullivan Page 4 of 7
Left and Right Shift Bit shifting lets us move all of the bits in a binary number type either to the left or to the right for a specified number of times. This will either multiply (left shift) or divide (right shift) the number by 2. If we shift the byte 00110111 2 left once, we get 01101110 2. If we shift 10110011 2, right by three places, we get 00010110 2. Notice in the right-shift example that bits that are shifted out of the byte are lost. This also occurs with left-shifting: if any bits are shifted outside of the "boundaries" of the type in use (eight bits, for a byte), they are lost. Notice that these shifting operations may cause problems with binary numbers that you consider signed (two's complement). Let's try shifting some bytes. If we shift 00000001 (1 10 ) left, we get 00000010 (2 10 ). If we shift that number left, we get 00000100 (4 10 ). If we keep shifting these results to the left, we get the pattern 8, 16, 32 ; each number is double the previous number. So if we want to multiply a number by two, we can shift it left once. This works for any number you might try: 01100101 2 (101 10 ) shifted left is 11001010 2 (202 10 ). However, be aware that if any bits fall outside the range of the type, the resulting byte will no longer be correct. Conversely, shifting a number once to the right, halves the number. But notice that if any bits are shifted outside the range of the type, accuracy is lost. Notice how 00010101 2 (21 10 ), shifted once to the right, gives 00001010 2 (10 10 decimal). The answer should, of course, be 10.5 10, but we are dealing with whole numbers, so the desired 10.5 10 is truncated to 10 10. Why does this work? Well, if we draw our byte diagram, with the values associated with each bit written above... 128 64 32 16 8 4 2 1 0 0 0 1 0 0 0 0...we can see that a 1 in, say, the 16 position that gets shifted left becomes worth 32 instead of 16. Similarly, a bit that is shifted right is worth only half as much. We can multiply (or divide) numbers by powers of two (2, 4, 8, 16, etc) using bit shifting. Obviously, shifting a number left and then shifting the result left again is equivalent to shifting a number left Ms. P. Sullivan Page 5 of 7
by two. The two shift lefts result in multiplying the original number by two and then by two again, with a net result of multiplying by four. Numerical Overflow Numeric overflow happens when the storage allocated for a calculation becomes too small to hold the result of the calculation. So if for example we re using 8 bits (1 byte) to work out binary addition, and the result of the addition is made up of 9 bits, a numeric overflow error occurs. Example 11111111+ 10101010 110101001 So, as you can see from the above addition, the result is made up of 9 bits instead of 8, in that case an error will occur. Ranges When having a limited number of bits for a register, there are three methods which one can use to store a number in the register. These are using sign and magnitude (where the first digit if 0 represents positive and 1 negative) representation, using one s complement and using two s complement. So, assume that we have a register of 8-Bits and we wish to store the number 14 in it. Since in this case the number is positive, it does not pose any difficulty because all we must do is simply convert it to binary and store it (since it is positive, it will be the same in either of the three methods), therefore: 14 10 = 1110 2 Since we re using 8 bits we have to make sure that the number is stored using 8 bits, hence: Ms. P. Sullivan Page 6 of 7
Sign and Magnitude 00001110 One s Complement 00001110 Two s Complement 00001110 Now, if the number we re going to store is -14, some adjustments need to be done. Sign and Magnitude 10001110 Note that the fist digit is 1 because in this case 14 is negative. One s Complement 11110001 In this case we simply applied NOT on the binary value of 14. Two s Complement 11110010 We simply added 1 to the one s complement of 14. *** Ms. P. Sullivan Page 7 of 7