Data Storage 2s Complement Notation to Store Numbers

Data Storage 2’s Complement Notation to Store Numbers Dr. M. Tanvir Afzal

2’s Complement Notation Integer Representations ü fixed number of bits to represent each value ü Normally 32 bits are used ü We will discuss smaller examples for demonstration purpose 2 Dr. M. Tanvir Afzal

2’s Complement Notation Integer Representations 3 ü For +ve integers, Starting from zero going upward until a single zero is reached followed by all 1 s ü For -ve integers, Starting from all 1 s going downwards until a single 1 is reached followed by all 0 s. ü Left most bit = sign bit. Dr. M. Tanvir Afzal

2’s Complement Notation Integer Representations 4 Dr. M. Tanvir Afzal

2’s Complement Notation Conversion between +ve and –ve representations ü Start from right most bit start copying until 1 st 1 arrives, after that complement all numbers. 5 Dr. M. Tanvir Afzal

Addition in 2’s Complement Notation ü All rules are same except that all bit patterns, including the answer, are the same length ü Truncation is performed 6 Dr. M. Tanvir Afzal

Problem of Overflow 7 ü Using 4 bits, maximum +ve number 7 can be represented and -8 on the –ve side. ü 5+4 = 9 which can not be stored in four bits ü Result would be as -7. ü Can be deducted using sign bit Dr. M. Tanvir Afzal

Problem of Overflow ü Todays computer have longer bit pattern normally 32 bits ü Maximum positive value = 2, 147, 483, 647 ü If still overflow occurs, we can use even more bits, or changing the units like calculating answer in Km than meter. 8 Dr. M. Tanvir Afzal

Dangers of using Computer ü Previously 16 bits were used to store 2’s complement notation, maximum number represented was 32, 768 ü On September 19, 1989, Hospital system malfunctioned as 32, 768 days had been passed after January 1 1900, thus it produced negative value. 9 Dr. M. Tanvir Afzal

Summary 2’s Complement ü ü ü 10 Representation of +ve and –ve numbers Conversions Addition in 2’s complement Truncation error Overflow Dr. M. Tanvir Afzal
- Slides: 10