CIS3355:
Business Data Structures |
How do we store negative numbers ? In decimal we represent negative and non-negative with the use of a sign: + 45
is the value non-negative 45 Notice that there are only two 'values' that the sign can take: + or - So ??? The sign is used to represent a binary condition. So ??? We already know that a binary condition can be represented by a single bit (remember our light switches). Therefore, no matter how many bits we have (e.g., 8, 16, 32, 64), we need only to designate one of them as the sign bit (for example, the left-most bit): Notice, however, that while we still have the same total number of bits, since one bit is designated as the sign, we have one fewer bit to represent the integer value: That will change the range of our integers:
HOWEVER, the total number of combinations remains the same.
Why do you subtract 1 from the
largest non-negative integer ??? Don't forget, we need to be able to represent the integer 0 (zero). What values do we put into the sign-bit ??? We only have two possible values: '0' or '1'. Let's say that if the number is negative, we will store the binary number '1' in the sign bit; If the number is non-negative, we will store the binary number '0' in the sign-bit. Following our logic, that means that we would store the the value +56 (as a short integer) as: Further following our logic, that means that we would store the value -56 as: Now, of course, if we add +56 and -56, we should come-up with the value 0 (zero): Which evaluates to the negative value (based on our sign-bit):
WHAT !!! 56 + -56 = -112 ??? What's going on ??? Well some of our ideas were good (e.g., the use of the sign bit), but we need to refine our procedures a little bit. How ??? We first need to investigate the concept of complementing numbers. You need to say 'Nice number' ??? No, not that kind of complementing. We will take a look at the concept in the next tutorial. Some questions you should be able to answer:
|