CS302 - Digital Logic and Design Handouts (1-45 Lectures) PDF

Summary

These handouts cover 45 lectures on Digital Logic and Design, including topics like number systems, logic gates, and digital circuits.

Full Transcript

Table of Contents Lesson No. 01 1 An Overview & Number Systems 1 Programmable Logic Devices (PLDs) 8 Fractions in Binary Number Sys...

Table of Contents Lesson No. 01 1 An Overview & Number Systems 1 Programmable Logic Devices (PLDs) 8 Fractions in Binary Number System 13 Binary Number System 12 Caveman number system 11 Decimal Number System 10 Number Systems and Codes 10 Analogue to Digital and Digital to Analogue conversion and Interfacing 9 Sequential logic and implementation 8 Combinational Logic Circuits and Functional Devices 7 Binary Number System 4 Digital Systems and Digital Values 4 Electronic Processing of Continuous and Digital Quantities 3 Digital representing of quantities 1 Analogue versus Digital 1 Lesson No. 02 14 Number Systems 14 Binary to Decimal conversion 14 Decimal to Binary conversion 15 Converting Decimal fractions to Binary 16 Binary Arithmetic 17 Signed and Unsigned Binary Numbers 19 1’s & 2’s complement 20 Lesson No. 03 23 Floating-Point Numbers 24 Hexadecimal Numbers 27 Lesson No. 04 31 Octal Numbers 31 The Excess Code 34 The BCD Code 34 The Gray Code 36 Alphanumeric Codes 37 ASCII Code 38 Extended ASCII Code 38 Parity Method 38 Lesson No. 05 40 Logic Gates 40 AND Gate 40 OR Gate 42 NOT Gate 43 NAND Gate 45 NOR Gate 47 Lesson No. 06 50 Logic Gates & Operational Characteristics 50 Exclusive-OR and Exclusive-NOR Gates 53 Digital Circuits and Operational Characteristics 56 TTL/CMOS NOT Gate Operation 57 Integrated Circuit Technologies 57 Lesson No. 07 61 Digital Circuits & Operational Characteristics 61 Lesson No. 08 71 Boolean Algebra & Logic Simplification 71 Laws of Boolean Algebra 72 Rules of Boolean Algebra 73 Demorgan’s Theorems 74 Simplification using Boolean Algebra 76 Standard Form of Boolean Expressions 77 Lesson No. 09 79 Standard SOP form 84 Standard POS form 85 Converting to Standard SOP and POS forms 85 Minterms and Maxterms 85 Lesson No. 10 89 Karnaugh Map & Boolean Expression Simplification 89 The 3-variable Karnaugh Map 89 The 4-variable Karnaugh Map 89 Grouping and Adjacent Cells 90 Simplification of SOP expressions using the Karnaugh Map 93 Don’t care Conditions 96 Lesson No. 11 99 Five-Variable Karnaugh Map 102 7-Segment Display 103 Lesson No. 12 109 Comparators 109 Quine-McCluskey Simplification Method 110 Comparator Circuit 116 Lesson No. 13 118 Odd-Prime Number Detector 118 Lesson No. 14 131 Operation of Odd-Parity Generator Circuit 132 XOR and XNOR Gates 133 Half Adder and Full Adder 134 Parallel Binary Adders 137 Carry Propagation 137 Lesson No. 15 141 BCD ADDER 141 Arithmetic and Logic Unit (ALU) 147 Lesson No. 16 150 16-BIT ALU 150 Comparators 152 Decoders 157 Binary Decoder 158 MSI Decoder 159 Lesson No. 17 160 THE 74XX138 3-TO-8 DECODER 160 BCD to 7-Segment Decoder 162 MSI Seven-Segment Decoder 163 BCD-to-Decimal Decoder 163 Encoder 163 Binary Encoder 163 Priority Encoders 164 Decimal-to-BCD Encoder 166 Multiplexer 167 Lesson No. 18 169 Applications of Multiplexers 172 Lesson No. 19 178 Demultiplexer 178 Applications of Demultiplexer 178 Programmable Logic Devices 179 Programmable Arrays of AND Gates and OR Gates 179 Programmable Read-Only Memory (PROM) 182 Programmable Logic Array (PLA) 182 Programmable Array Logic (PAL) 182 Generic Array Logic (GAL) 183 Lesson No. 20 190 The GAL22V10 194 OLMC Combinational Mode 196 Tri-State Buffers 196 Lesson No. 21 200 ABEL 203 Boolean Operations and Boolean Notations 203 Test Vectors 206 Lesson No. 22 209 Latches and Flip-Flops 211 The NAND gate based S-R (Set-Reset) Latch 211 The NOR gate based S-R (Set-Reset) Latch 213 Lesson No. 23 217 The Gated S-R Latch 218 The Gated D Latch 219 Edge-Triggered Flip-Flop 221 Edge-Triggered S-R Flip-flop 222 Edge-Triggered D Flip-flop 224 Edge-Triggered J-K Flip-flop 225 Master-Slave Flip-Flops 230 Lesson No. 24 237 Edge-Triggered J-K Flip-flop 240 J-K flip-flop used as a counter 246 Lesson No. 25 248 One-Shot Mono-stable multi-vibrator 255 Lesson No. 26 258 THE 555 TIMER 258 Counters 261 Asynchronous Counters (Ripple Counters) 262 Mod-n Counters 265 Integrated Circuit Asynchronous Counters 268 Lesson No. 27 270 Down Counters 270 Synchronous Counters 272 Lesson No. 28 277 Mod-n Synchronous Counter 277 Integrated Circuit Synchronous Counters 278 Cascading Counters 279 Lesson No. 29 286 Up/Down Counters 286 Design Procedure 298 Lesson No. 30 305 Digital Clock 305 Lesson No. 31 314 S-R flip-flop based Implementation 316 Lesson No. 32 320 State Reduction 325 Lesson No. 33 328 State Assignment 328 Moore Machine State Diagram 330 Mealy Machine 333 Lesson No. 34 339 Shift Registers 339 Serial In/Shift Right/Serial Out Operation 339 Serial In/Shift Left/Serial Out Operation 339 Serial In/Parallel Out Operation 341 Parallel In/Serial Out Operation 343 Parallel In/Parallel Out Operation 345 Rotate Right Operation 347 Rotate Left Operation 347 Shift Register Counters 347 Johnson Counter 347 Ring Counter 348 Lesson No. 35 349 Serial-to-Parallel Converter 349 Keyboard Encoder 350 Lesson No. 36 360 3-Bit Up/Down Counter 360 Elevator State Diagram 366 Lesson No. 37 370 Traffic Signal Control System 374 Lesson No. 38 378 Equation Definition 378 Lesson No. 39 387 Memory 387 Memory Organization 387 Random Access Memory (RAM) 391 Static RAM 392 Lesson No. 40 396 Dynamic RAM 400 Lesson No. 41 405 Types of DRAMs 407 ROM Read-Only Memory 407 PROM (Programmable ROMs) 410 EPROM Erasable PROM 411 Programming EPROM 412 EEPROM Electrically Erasable PROM 412 FLASH Memory 412 Lesson No. 42 416 Flash Memory Array 416 First In-First Out (FIFO) Memory 418 Lesson No. 43 422 Last IN - First OUT (LIFO) Memory 422 Memory Map 424 Address Decoders 428 Introduction to FPGAs 430 Lesson No. 44 432 Analogue to Digital Conversion 434 Operational Amplifier (Op-Amp) 439 Lesson No. 45 446 Digital to Analogue Conversion 449 CS302 - Digital Logic & Design Lesson No. 01 AN OVERVIEW & NUMBER SYSTEMS Analogue versus Digital Most of the quantities in nature that can be measured are continuous. Examples include Intensity of light during the da y: The intensity of light gradually increases as the sun rises in the morning; it remains constant throughout the day and then gradually decreases as the sun sets until it becomes completely dark. The change in the light throughout the day is gradual and continuous. Even with a sudden change in weather when the sun is obscured by a cloud the fall in the light intensity although very sharp however is still continuous and is not abrupt. Rise and fall in temper ature during a 24-hour period: The temperature also rises and falls with the passage of time during the day and in the night. The change in temperature is never abrupt but gradual and continuous. Velocity of a car travelling from A to B: The velocity of a car travelling from one city to another varies in a continuous manner. Even if it abruptly accelerates or stops suddenly, the change in velocity seemingly very sudden and abrupt is never abrupt in reality. This can be confirmed by measuring the velocity in short time intervals of few milliseconds. The measurable values generally change over a continuous range having a minimum and maximum value. The temperature values in a summer month change between 23 0C to 45 0C. A car can travel at any velocity between 0 to 120 mph. Digital representing of quantities Digital quantities unlike Analogue quantities are not continuous but represent quantities measured at discrete intervals. Consider the continuous signal as shown in the figure 1.1. To represent this signal digitally the signal is sampled at fixed and equal intervals. The continuous signal is sampled at 15 fixed and equal intervals. Figure 1.2. The set of values (1, 2, 4, 7, 18, 34, 25, 23, 35, 37, 29, 42, 41, 25 and 22) measured at the sampling points represent the continuous signal. The 15 samples do not exactly represent the original signal but only approximate the original continuous signal. This can be confirmed by plotting the 15 sample points. Figure 1.3. The reconstructed signal from the 15 samples has sharp corners and edges in contrast to the original signal that has smooth curves. If the number of samples that are collected is reduced by half, the reconstructed signal will be very different from the original. The reconstructed signal using 7 samples have missing peak and dip at 34 0C and 23 0C respectively. Figure 1.4. The reason for the difference between the original and the reconstructed signal is due to under-sampling. A more accurate representation of the continuous signal is possible if the number of samples and sampling intervals are increased. If the sampling is increased to infinity the number of values would still be discrete but they would be very close and closely match the actual signal. © Copyright Virtual University of Pakistan 1 CS302 - Digital Logic & Design 45 40 35 30 temperature 0C 25 20 15 10 5 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 time Figure 1.1 Continuous signal showing temperature varying with time 45 42 41 40 37 35 34 35 30 temperature 0C 29 25 25 25 23 22 20 18 15 10 7 5 4 1 2 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 time Figure 1.2 Sampling the Continuous Signal at 15 equal intervals © Copyright Virtual University of Pakistan 2 CS302 - Digital Logic & Design 45 42 41 40 37 35 34 35 30 temperature 0C 29 25 25 25 23 22 20 18 15 10 7 5 4 1 2 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 samples Figure 1.3 Reconstructed Signal by plotting 15 sampled values 45 40 35 30 temperature 0C 25 20 15 10 5 0 1 3 5 7 9 11 13 15 samples Figure 1.4 Reconstructed Signal by plotting 7 sampled values Electronic Processing of Continuous and Digital Quantities Electronic Processing of the continuous quantities or their Digital representation requires that the continuous signals or the discrete values be converted and represented in terms of voltages. There are basically two types of Electronic Processing Systems. Analogue Electronic Sy stems: These systems accept and process continuous signals represented in the form continuous voltage or current signals. The continuous quantities are converted into continuous voltage or current signals by transducers. The block diagram describes the processing by an Analogue Electronic System. Figure 1.5. © Copyright Virtual University of Pakistan 3 CS302 - Digital Logic & Design Digital Electronic Sy stems: These systems accept and process discrete samples representing the actual continuous signal. Analogue to Digital Converters are used to sample the continuous voltage signals representing the original signal. Do the Digital Electronic Systems use voltages to represent the discrete samples of the continuous signal? This question can be answered by considering a very simple example of a calculator which is a Digital Electronic System. Assume that a calculator is calibrated to represents the number 1 by 1 millivolt (mV). Thus the number 39 is represented by the calculator in terms of voltage as 39 mV. Calculators can also represent large numbers such as 6.25 x 1018 (as in 1 Coulomb = 6.25 x 1018 electrons). The value in terms of volts is 6.25 x 1015 volts! This voltage value can not be practically represented by any electronic circuit. Thus Digital Systems do not use discrete samples represented as voltage values. Figure 1.5 Analogue Electronic System processing continuous quantities Digital Systems and Digital Values Digital systems are designed to work with two voltage values. A +5 volts represents a logic high state or logic 1 state and 0 volts represents a logic low state or logic 0 state. The Digital Systems which are based on two voltage values or two states can easily represent any two values. For example, The numbers ‘0’ and ‘1’ The state of a switch ‘on’ or ‘off’ The colour ‘black’ and ‘white’ The temperature ‘hot’ and ‘cold’ An object ‘moving’ or ‘stationary’ Representing two values or two states is not very practical, as many naturally occurring phenomenons have values or state that are more than two. For example, numbers have widely varying ranges, a colour palette might have 64 different shades of the colour red, the temperature of boiling water at room temperature varies from 30 0C to 100 0C. Digital Systems are based on the Binary Number system which allows more than two or multiple values to be represented very conveniently. Binary Number System © Copyright Virtual University of Pakistan 4 CS302 - Digital Logic & Design The Binary Number System unlike the Decimal number system is based on two values. Each digit or bit in Binary Number system can represent only two values, a ‘0’ and a ‘1’. A single digit of the Decimal Number system represents 10 values, 0, 1, 2 to 9. The Binary Number System can be used to represent more than two values by combining binary digits or bits. In a Decimal Number System a single digit can represent 10 different values (0 to 9), representing more than 10 values requires a combination of two digits which allows up to 100 values to be represented (0 to 99). A Combination of Binary Numbers is used to represent different quantities. Represent Colours: A palette of four colours red, blue, green and yellow can be represented by a combination of two digital values 00, 01, 10 and 11 respectively. Representing Temperature: An analogue value such as 39oC can be represented in a digital format by a combination of 0s and 1s. Thus 39 is 100111 in digital form. Any quantity such as the intensity of light, temperature, velocity, colour etc. can be represented through digital values. The number of digits (0s and 1s) that represents a quantity is proportional to the range of values that are to be represented. For example, to represent a palette of eight colours a combination of three digits is used. Representing a temperature range of 00 C to 1000 C requires a combination of up to seven digits. Digital Systems uses the Binary Number System to represent two or multiple values, stores and processes the binary values in terms of 5 volts and 0 volts. Thus the number 39 represented in binary as 100111 is stored electronically in as +5 v, 0v, 0v, +5 v, +5 v and +5 v. Advantages of working in the Digital Domain Handling information digitally offers several advantages. Some of the merits of a digital system are spelled out. Details of some these aspects will be discussed and studied in the Digital Logic Design course. Other aspects will be covered in several other courses. Storing and processing data in the digital domain is more efficient: Computers are very efficient in processing massive amounts of information and data. Computers process information that is represented digitally in the form of Binary Numbers. A Digital CD stores large number of video and audio clips. Sam number of audio and video clips if stored in analogue form will require a number of video and audio cassettes. Transmission of data in the digital form is more efficient and reliable: Modern information transmission techniques are relying more on digital transmission due to its reliability as it is less prone to errors. Even if errors occur during the transmission methods exist which allow for quick detection and correction of errors. Detecting and Correcting errors in digital data is easier: Coding Theory is an area which deals with implementing digital codes that allow for detection and correction of multi- bit errors. In the Digital Logic Design course a simple method to detect single bit errors using the Parity bit will be considered. Data can be easily and precisely reproduced: The picture quality and the sound quality of digital videos are far more superior to those of analogue videos. The reason being that the digital video stored as digital numbers can be exactly reproduced where as analogue video is stored as a continuous signal can not be reproduced with exact precision. Digital systems are easy to design and implement: Digital Systems are based on two- state Binary Number System. Consequently the Digital Circuitry is based on the two- voltage states, performing very simple operations. Complex Microprocessors are implemented using simple digital circuits. Several simple Digital Systems will be discussed in the Digital Logic course. © Copyright Virtual University of Pakistan 5 CS302 - Digital Logic & Design Digital circuits occupy small sp ace: Digital circuits are based on two logical states. Electronic circuitry that implements the two states is very simple. Due to the simplicity of the circuitry it can be easily implemented in a very small area. The PC motherboard having an area of approximately 1 sq.ft has most of the circuitry of a powerful computer. A memory chip small enough to be held in the palm of a hand is able to store an entire collection of books. Information Processing by a Digital System A Digital system such as a computer not only handles numbers but all kinds of information. Numbers: A computer is able to store and process all types of numbers, integers, fractions etc. and is able to perform different kinds of arithmetic operations on the numbers. Text: A computer in a news reporting room is used to write and edit news reports. A Mathematician uses a computer to write mathematical equations explaining the dissipation of heat by a heat sink. The computer is able to store and process text and symbols. Drawings, Diagrams and Pictures: A computer can store very conveniently complex engineering drawings and diagrams. It allows real life still pictures or videos to be processed and edited. Music and Sound: Musicians and Composers uses\ a computer to work on a new compositions. Computers understand spoken commands. A Digital System (computer) is capable of handling different types of information, which is represented in the form of Binary Numbers. The different types of information use different standards and binary formats. For example, computers use the Binary number system to represent numbers. Characters used in writing text are represented through yet another standard known as ASCII which allows alphabets, punctuation marks and numbers to be represented through a combination of 0s and 1s. Digital Components and their internal working Digital system process binary information electronically through specialized circuits designed for handling digital information. These circuits as mentioned earlier operate with two voltage values of +5 volts and 0 volts. These specialized electronic circuits are known as Logic Gates and are considered to be the Basic Building Blocks of any Digital circuit. The commonly used Logic Gates are the AND gate, the OR gate and the Inverter or NOT Gate. Other gates that are frequently used include NOR, NAND, XOR and XNOR. Each of these gates is designed to perform a unique operation on the input information which is known as a logical or Boolean operation. Large and complex digital system such as a computer is built using combinations of these basic Logic Gates. These basic building blocks are available in the form of Integrated Circuit or ICs. These gates are implemented using standard CMOS and TTL technologies that determine the operational characteristics of the gates such as the power dissipation, operational voltages (3.3v or 5 v), frequency response etc. © Copyright Virtual University of Pakistan 6 CS302 - Digital Logic & Design Figure 1.6 Symbolic representations of logic gates. Combinational Logic Circuits and Functional Devices The logic gates which form the basic building blocks of a digital system are designed to perform simple logic operations. A single logic gate is not of much use unless it is connected with other gates to collectively act upon the input data. Different gates are combined to build a circuit that is capable of performing some useful operation like adding three numbers. Such circuits are known as Combinational Logic Circuits or Combinational Circuits. An Adder Combinational Circuit that is able to add two single bit binary numbers and give a single bit Sum and Carry output is shown. Figure 1.7. Implementing large digital system by connecting together logic gates is very tedious and time consuming; the circuit implemented occupies large space, are power hungry, slow and are difficult to troubleshoot. A P B ∑ Cin Cout G Figure 1.7 1-bit Full-Adder Combinational Circuit Digital circuits to perform specific functions are available as Integrated Circuits for use. Implementing a Digital system in terms of these dedicated functional units makes the system more economical and reliable. Thus an adder circuit does not have to be implemented by connecting various gates, a standard Adder IC is available that can be readily used. Other © Copyright Virtual University of Pakistan 7 CS302 - Digital Logic & Design commonly used combinational functional devices are Comparators, Decoders, Encoders, Multiplexers and Demultiplexers. Sequential logic and implementation Digital systems are used in vast variety of industrial applications and house hold electronic gadgets. Many of these digital circuits generate an output that is not only dependent on the current input but also some previously saved information which is used by the digital circuit. Consider the example of a digital counter which is used by many digital applications where a count value or the time of the day has to be displayed. The digital counter which counts downwards from 10 to 0 is initialized to the value 10. When the counter receives an external signal in the form of a pulse the counter decrements the count value to 9. On receiving successive pulses the counter decrements the currently stored count value by one, until the counter has been decremented to 0. On reaching the count value zero, the counter could switch off a washing machine, a microwave oven or switch on an air-conditioning system. The counter stores or remembers the previous count value. The new count value is determined by the previously stored count value and the new input which it receives in the form of a pulse (a binary 1). The diagram of the counter circuit is shown in the figure. Figure 1.8. Digital circuits that generate a new output on the basis of some previously stored information and the new input are known as Sequential circuits. Sequential circuits are a combination of Combinational circuits and a memory element which is able to store some previous information. Sequential circuits are a very important part of digital systems. Most digital systems have sequential logic in addition to the combinational logic. An example of sequential circuits is counters such as the down-counter which generates a new decremented output value based on the previous stored value and an external input. The storage element or the memory element which is an essential part of a sequential circuit is implemented a flip-flop using a very simple digital circuit known as a flip-flop. Figure 1.8 A Counter Sequential Circuit Programmable Logic Devices (PLDs) The modern trend in implementing specialized dedicated digital systems is through configurable hardware; hardware which can be programmed by the end user. A digital © Copyright Virtual University of Pakistan 8 CS302 - Digital Logic & Design controller for a washing machine can be implemented by connecting together pieces of combinational and sequential functional units. These implementations are reliable however they occupy considerable space. The implementation time also increases. A general purpose circuit that can be programmed to perform a certain task like controlling a washing machine reduces the implementation cost and time. Cost is incurred on implementing a digital controller for a washing machine which requires that an inventory of all its components such as its logic circuits, functional devices and the counter circuits be maintained. The implementation time is significantly high as all the circuit components have to be placed on a circuit board and connected together. If there is a change in the controller circuit the entire circuit board has to be redesigned. A PLD based washing machine controller does not require a large inventory of components to be maintained. Most of the functionality of the controller circuit is implemented within a single PLD integrated circuit thereby considerably reducing the circuit size. Changes in the controller design can be readily implemented by programming the PLD. Programmable Logic Devices can be used to implement Combinational and Sequential Digital Circuits. Memory Memory plays a very important role in Digital systems. A research article being edited by a scientist on a computer is stored electronically in the digital memory whilst changes are being made to the document. Once the document has be finalized and stored on some media for subsequent printing the memory can be reused to work on some other document. Memory also needs to store information permanently even when the electrical power is turned off. Permanent memories usually contain essential information required for operating the digital system. This important information is provided by the manufacturer of a digital system. Memory is organized to allow large amounts of data storage and quick access. Memory (ROM) which permanently stores data allows data to be read only. The Memory does not allow writing of data. Volatile memory (RAM) does not store information permanently. If the power supplied to the RAM circuitry is turned off, the contents of the RAM are permanently lost and can not be recovered when power is restored. RAM allows reading and writing of data. Both RAM and ROM are an essential part of a digital system. Analogue to Digital and Digital to Analogue conversion and Interfacing Real-world quantities as mention earlier are continuous in nature and have widely varying ranges. Processing of real-world information can be efficiently and reliably done in the digital domain. This requires real-world quantities to be read and converted into equivalent digital values which can be processed by a digital system. In most cases the processed output needs to be converted back into real-world quantities. Thus two conversions are required, one from the real-world to the digital domain and then back from the digital domain to the real-world. Modern digitally controlled industrial units extensively use Analogue to Digital (A/D) and Digital to Analogue (D/A) converters to covert quantities represented as an analogue voltage into an equivalent digital representation and vice versa. Consider the example of an industrial controller that controls a chemical reaction vessel which is being heated to expedite the chemical reaction. Figure 1.9. Temperature of the vessel is monitored to control the chemical reaction. As the temperature of the vessel rises the heat has to be reduced by a proportional © Copyright Virtual University of Pakistan 9 CS302 - Digital Logic & Design level. An electronic temperature sensor (transducer) converts the temperature into an equivalent voltage value. This voltage value is continuous and proportion to the temperature. The voltage representing the temperature is converted into a digital representation which is fed to a digital controller that generates a digital value corresponding to the desired amount of heat. The digitized output representing the heat is converted back to a voltage value which is continuous and is used to control a valve that regulates the heat. An A/D converter converts the analogue voltage value representing the temperature into a corresponding digital value for processing. A D/A converter converts back the digital heat value to its corresponding continuous value for regulating the heater. Digital Controller A/D D/A Converter Converter Transducer Vessel Heater Figure 1.9 Digitally Controlled Industrial Heater Unit A/D and D/A converters are an important aspect of digital systems. These devices serve as a bridge between the real and digital world allow the two to communicate and interact together. Number Systems and Codes Decimal Number System The decimal number system has ten unique digits 0, 1, 2, 3… 9. Using these single digits, ten different values can be represented. Values greater than ten can be represented by using the same digits in different combinations. Thus ten is represented by the number 10, two hundred seventy five is represented by 275 etc. Thus same set of numbers 0,1 2… 9 are repeated in a specific order to represent larger numbers. The decimal number system is a positional number system as the position of a digit represents its true magnitude. For example, 2 is less than 7, however 2 in 275 represents 200, whereas 7 represents 70. The left most digit has the highest weight and the right most digit has the lowest weight. 275 can be written in the form of an expression in terms of the base value of the number system and weights. 2 x 102 + 7 x 101 + 5 x 100 = 200 + 70 + 5 = 275 where, 10 represents the base or radix 102, 101, 100 represent the weights 100, 10 and 1 of the numbers 2, 7 and 5 © Copyright Virtual University of Pakistan 10 CS302 - Digital Logic & Design Fractions in Decimal Number System In a Decimal Number System the fraction part is separated from the Integer part by a decimal point. The Integer part of a number is written on the left hand side of the decimal point. The Fraction part is written on the right side of the decimal point. The digits of the Integer part on the left hand side of the decimal point have weights 100, 101, 102 etc. respectively starting from the digit to the immediate left of the decimal point and moving away from the decimal point towards the most significant digit on the left hand side. Fractions in decimal number system are also represented in terms of the base value of the number system and weights. The weights of the fraction part are represented by 10-1, 10-2, 10-3 etc. The weights decrease by a factor of 10 moving right of the decimal point. The number 382.91 in terms of the base number and weights is represented as 3 x 102 + 8 x 101 + 2 x 100 + 9 x 10-1 + 1 x 10-2 = 300 + 80 + 2 + 0.9 + 0.01 = 382.91 Caveman number system A number system discovered by archaeologists in a prehistoric cave indicates that the caveman used a number system that has 5 distinct shapes ∑, ∆, >, Ω and ↑. Furthermore it has been determined that the symbols ∑ to ↑ represents the decimal equivalents 0 to 5 respectively. Centuries ago a caveman returning after a successful hunting expedition records his successful hunt on the cave wall by carving out the numbers ∆↑. What does the number ∆↑ represent? The table 1.1 indicates that the Caveman numbers ∆↑ represents decimal number 9. Decimal Number Caveman Number Decimal Number Caveman Number 0 ∑ 10 >∑ 1 ∆ 11 >∆ 2 > 12 >> 3 Ω 13 >Ω 4 ↑ 14 >↑ 5 ∆∑ 15 Ω∑ 6 ∆∆ 16 Ω∆ 7 ∆> 17 Ω> 8 ∆Ω 18 ΩΩ 9 ∆↑ 19 Ω↑ 20 ↑∑ Table 1.1 Decimal equivalents of the Caveman Numbers The Caveman is using a Base-5 number system. A Base-5 number system has five unique symbols representing numbers 0 to 4. To represent numbers larger than 4, a combination of 2, 3, 4 or more combinations of Caveman numbers are used. Therefore, to represent the decimal number 5, a two number combination of the Caveman number system is used. The most significant digit is ∆ which is equivalent to decimal 1. The least significant digit is ∑ which is equivalent to decimal 0. The five combinations of Caveman numbers having the most significant digit ∆, represent decimal values 5 to 9 respectively. This is similar to the Decimal Number system, where a 2-digit combination of numbers is used to represent values © Copyright Virtual University of Pakistan 11 CS302 - Digital Logic & Design greater than 9. The most significant digit is set to 1 and the least significant digit varies from 0 to 9 to represent the next 10 values after the largest single decimal number digit 9. The Caveman number ∆↑ can be written in expression form based on the Base value 5 and weights 50, 51, 52 etc. = ∆ x 51 + ↑ x 50 = ∆ x 5 + ↑ x 1 Replacing the Caveman numbers ∆ and ↑ with equivalent decimal values in the expression yields = ∆ x 51 + ↑ x 50 = 1 x 5 + 4 x 1 = 9 The number ∆Ω↑∑ in decimal is represented in expression form as ∆ x 53 + Ω x 52 + ↑ x 51 + ∑ x 50 = ∆ x 125 + Ω x 25 + ↑ x 5 + ∑ x 1 Replacing the Caveman numbers with equivalent decimal values in the expression yields = (1) x 125 + (3) x 25 + (4) x 5 + (0) x 1 = 125 + 75 + 20 + 0 = 220 Binary Number System The Caveman Number system is a hypothetical number system introduced to explain that number system other than the Decimal Number system can exist and can be used to represent and count numbers. Digital systems use a Binary number system. Binary as the name indicates is a Base-2 number system having only two numbers 0 and 1. The Binary digit 0 or 1 is known as a ‘Bit’. Table 1.2 Decimal Number Binary Number Decimal Number Binary Number 0 0 10 1010 1 1 11 1011 2 10 12 1100 3 11 13 1101 4 100 14 1110 5 101 15 1111 6 110 16 10000 7 111 17 10001 8 1000 18 10010 9 1001 19 10011 20 10100 Table 1.2 Decimal equivalents of Binary Number System Counting in Binary Number system is similar to counting in Decimal or Caveman Number systems. In a decimal Number system a value larger than 9 has to be represented by 2, 3, 4 or more digits. In the Caveman Number System a value larger than 4 has to be represented by 2, 3, 4 or more digits of the Caveman Number System. Similarly, in the Binary Number System a Binary number larger than 1 has to be represented by 2, 3, 4 or more binary digits. © Copyright Virtual University of Pakistan 12 CS302 - Digital Logic & Design Any binary number comprising of Binary 0 and 1 can be easily represented in terms of its decimal equivalent by writing the Binary Number in the form of an expression using the Base value 2 and weights 20, 21, 22 etc. The number 100112 (the subscript 2 indicates that the number is a binary number and not a decimal number ten thousand and eleven) can be rewritten in terms of the expression 100112 = (1 x 24) + (0 x 23) + (0 x 22) + (1 x 21) + (1 x 20) = (1 x 16) + (0 x 8) + (0 x 4) + (1 x 2) + (1 x 1) = 16 + 0 + 0 + 2 + 1 = 19 Fractions in Binary Number System In a Decimal number system the Integer part and the Fraction part of a number are separated by a decimal point. In a Binary Number System the Integer part and the Fraction part of a Binary Number can be similarly represented separated by a decimal point. The Binary number 1011.1012 has an Integer part represented by 1011 and a fraction part 101 separated by a decimal point. The subscript 2 indicates that the number is a binary number and not a decimal number. The Binary number 1011.1012 can be written in terms of an expression using the Base value 2 and weights 23, 22, 21, 20, 2-1, 2-2 and 2-3. 1011.1012 = (1 x 23) + (0 x 22) + (1 x 21) + (1 x 20) + (1 x 2-1) + (0 x 2-2) + (1 x 2-3) = (1 x 8) + (0 x 4) + (1 x 2) + (1 x 1) + (1 x 1/2) + (0 x 1/4) + (1 x 1/8) = 8 + 0 + 2 + 1 + 0.5 + 0 + 0.125 = 11.625 Computers do handle numbers such as 11.625 that have an integer part and a fraction part. However, it does not use the binary representation 1011.101. Such numbers are represented and used in Floating-Point Numbers notation which will be discussed latter. © Copyright Virtual University of Pakistan 13 CS302 - Digital Logic & Design Lesson No. 02 NUMBER SYSTEMS Binary to Decimal conversion Most real world quantities are represented in Decimal Number System. Digital Systems on the other hand are based on the Binary Number System. Therefore, when converting from the Digital Domain to the real-world, Binary numbers have to be represented in terms of their Decimal equivalents. The method used to convert from Binary to Decimal is the Sum-of-Weights method. The Sum-of-Weights method has been used to represent the Caveman numbers ∆↑, ∆Ω↑∑ and the Binary numbers 10011 and 1011.101 in the first lecture. 1. Sum-of-Weights Method Sum-of-weights as the name indicates sums the weights of the Binary Digits (bits) of a Binary Number which is to be represented in Decimal. The Sum-of-Weights method can be used to convert a Binary number of any magnitude to its equivalent Decimal representation. In the Sum-of-Weights method an extended expression is written in terms of the Binary Base Number 2 and the weights of the Binary number to be converted. The weights correspond to each of the binary bits which are multiplied by the corresponding binary value. Binary bits having the value 0 do not contribute any value towards the final sum expression. The Binary number 101102 is therefore written in the form of an expression having weights 20, 21, 22, 23 and 24 corresponding to the bits 0, 1, 1, 0 and 1 respectively. Weights 20 and 23 do not contribute in the final sum as the binary bits corresponding to these weights have the value 0. 101102 = 1 x 2 4 + 0 x 23 + 1 x 22 + 1 x 21 + 0 x 20 = 16 + 0 + 4 + 2 + 0 = 22 2. Sum-of-non-zero terms In the Sum-of-Weights method, the Binary bits 0 do not contribute towards the final sum representing the decimal equivalent. Secondly, the weight of each binary bit increases by a factor of 2 starting with a weight of 1 for the least significant bit. For example, the Binary number 101102 has weights 20=1, 21=2, 22=4, 23=8 and 24=16 corresponding to the bits 0, 1, 1, 0 and 1 respectively. The Sum-of-non-zero terms method is a quicker method to determine decimal equivalents of binary numbers without resorting to writing an expression. In the Sum-of-non- zero terms method the weights of non-zero binary bits are summed, as the weights of zero binary bits do not contribute towards the final sum representing the decimal equivalent. The weights of binary bits starting from the right most least significant bit is 1, The next significant bit on the left has the weight 2, followed by 4, 8, 16, 32 etc. corresponding to higher significant bits. In binary number system the weights of successive bits increase by an order of 2 towards the left side and decrease by an order of 2 towards the right side. Thus a binary number can be quickly converted into its decimal equivalent by adding weights of non-zero terms which increase by a factor of 2. Binary numbers having an integer and a fraction part can similarly be converted into their decimal equivalents by applying the same method. © Copyright Virtual University of Pakistan 14 CS302 - Digital Logic & Design A quicker method is to add the weights of non-zero terms. Thus for the numbers o 100112 = 16 + 2 + 1 = 19 o 1011.1012 = 8 + 2 + 1 + ½ + 1/8 = 11 + 5/8 = 11.625 Decimal to Binary conversion Conversion from Decimal to Binary number system is also essential to represent real-world quantities in terms of Binary values. The Sum-of-weights and repeated division by 2 methods are used to convert a Decimal number to equivalent Binary. 1. Sum-of-Weights The Sum-of-weights method used to convert Binary numbers into their Decimal equivalent is based on adding binary weights of the binary number bits. Converting back from the decimal number to the original Binary number requires finding the highest weight included in the sum representing the decimal equivalent. A Binary 1 is marked to represent the bit which contributed its weight in the Sum representing the decimal equivalent. The weight is subtracted from the sum decimal equivalent. The next highest weight included in the sum term is found. A binary 1 is marked to represent the bit which contributed its weight in the sum term and the weight is subtracted from the sum term. This process is repeated until the sum term becomes equal to zero. The binary 1s and 0s represent the binary bits that contributed their weight and bits that did not contribute any weight respectively. The process of determining Binary equivalent of a Decimal number 392 and 411 is illustrated in a tabular form. Table 2.1. Sum Term Highest Binary Sum Term Weight Number = Sum Term – Highest Weight 411 256 100000000 155 155 128 110000000 27 27 16 110010000 11 11 8 110011000 3 3 2 110011010 1 1 1 110011011 0 Table 2.1a Converting Decimal to Binary using Sum-of-Weights Method Sum Term Highest Binary Sum Term Weight Number = Sum Term – Highest Weight 392 256 100000000 136 136 128 110000000 8 8 8 110001000 0 Table 2.1b Converting Decimal to Binary using Sum-of-Weights Method The Sum of weights method requires mental arithmetic and is a quick way of converting small decimal numbers into binary. With practice large Decimal numbers can be converted into Binary equivalents. 2. Repeated Division-by-2 Repeated Division-by-2 method allows decimal numbers of any magnitude to be converted into binary. In this method the Decimal number to be converted into its Binary © Copyright Virtual University of Pakistan 15 CS302 - Digital Logic & Design equivalent is repeatedly divided by 2. The divisor is selected as 2 because the decimal number is being converted into Binary a Base-2 Number system. Repeated division method can be used to convert decimal number into any Number system by repeated division by the Base- Number. For example, the decimal number can be converted into the Caveman Number system by repeatedly dividing by 5, the Base number of the Caveman Number System. The Repeated Division method will be used in latter lectures to convert decimal into Hexadecimal and Octal Number Systems. In the Repeated-Division method the Decimal number to be converted is divided by the Base Number, in this particular case 2. A quotient value and a remainder value is generated, both values are noted done. The remainder value in all subsequent divisions would be either a 0 or a 1. The quotient value obtained as a result of division by 2 is divided again by 2. The new quotient and remainder values are again noted down. In each step of the repeated division method the remainder values are noted down and the quotient values are repeatedly divided by the base number. The process of repeated division stops when the quotient value becomes zero. The remainders that have been noted in consecutive steps are written out to indicate the Binary equivalent of the Original Decimal Number. Number Quotient after division Remainder after division 392 196 0 196 98 0 98 49 0 49 24 1 24 12 0 12 6 0 6 3 0 3 1 1 1 0 1 Table 2.2 Converting Decimal to Binary using Repeated Division by 2 Method The process of determining the Binary equivalent of a Decimal number 392 is illustrated in a tabular form. Table 2.2. Reading the numbers in the Remainder column from bottom to top 110001000 gives the binary equivalent of the decimal number 392 Converting Decimal fractions to Binary Two methods are used to Convert Decimal fractions to Binary. The Sum-of-Weights method, which has been described and used to convert Decimal Integers into Binary Equivalents is applied to convert Decimal fractions into Binary fractions. This method requires mental arithmetic and is suitable for small numbers. The conversion of Decimal fraction 0.625 into Binary equivalent is illustrated in a tabular form. Table 2.3 Sum Term Highest Binary Sum Term Weight Number = Sum Term – Highest Weight 0.625 0.500 0.100 0.125 0.125 0.125 0.101 0 Table 2.3 Converting Decimal to Binary using Sum-of-Weights Method Repeated Multiplication-by-2 Method An alternate to the Sum-of-Weights method used to convert Decimal fractions to equivalent Binary fractions is the repeated multiplication by 2 method. In this method the © Copyright Virtual University of Pakistan 16 CS302 - Digital Logic & Design number to be converted is repeatedly multiplied by the Base Number to which the number is being converted to, in this case 2. A new number having an Integer part and a Fraction part is generated after each multiplication. The Integer part is noted down and the fraction part is again multiplied with the Base number 2. The process is repeated until the fraction term becomes equal to zero. Repeated Multiplication-by-2 method allows decimal fractions of any magnitude to be easily converted into binary. The conversion of Decimal fraction 0.625 into Binary equivalent using the Repeated Multiplication-by-2 method is illustrated in a tabular form. Table 2.4. Reading the Integer column from top to bottom and placing a decimal point in the left most position gives 0.101 the binary equivalent of decimal fraction 0.625 Number Integer part after Fraction part after multiplication multiplication 0.625 1 0.25 0.25 0 0.5 0.5 1 0.0 Table 2.4 Converting Decimal to Binary using repeated Multiplication-by-2 Method Binary Arithmetic Digital systems use the Binary number system to represent numbers. Therefore these systems should be capable of performing standard arithmetic operations on binary numbers. 1. Binary Addition Binary Addition is identical to Decimal Addition. By adding two binary bits a Sum bit and a Carry bit are generated. The only difference between the two additions is the range of numbers used. In Binary Addition, four possibilities exist when two single bits are added together. The four possible input combinations of two single bit binary numbers and their corresponding Sum and Carry Outputs are specified in table 2.5. First Number Second Number Sum Carry 0 0 0 0 0 1 1 0 1 0 1 0 1 1 0 1 Table 2.5 Addition of two Single Bit Binary Numbers The first three additions give a result 0, 1 and 1 respectively which can be represented by a single binary digit (bit). The fourth addition results in the number 2, which can be represented in binary as 102. Thus two digits (bits) are required. This is similar to the addition of 9 + 3 in decimal. The answer is 12 which can not be represented by a single digit, thus two digits are required. The number 2 is the sum part and 1 is the carry part. Any number of binary numbers having any number of digits can be added together. Thus the number 1011, 110, 1000 and 11 can be added together. Most significant digits (bits) of second and fourth numbers are assumed to be zero. © Copyright Virtual University of Pakistan 17 CS302 - Digital Logic & Design Carry 1 10 1 Decimal Equivalent 1st Number 1 0 1 1 (11) 2nd Number 1 1 0 (06) 3rd Number 1 0 0 0 (08) 4th Number 1 1 (03) Result 1 1 1 0 0 (28) Table 2.6 Adding multiple binary numbers of different sizes 2. Binary Subtraction Binary Subtraction is identical to Decimal Subtraction. The only difference between the two is the range of numbers. Subtracting two single bit binary numbers results in a difference bit and a borrow bit. The four possible input combinations of two single bit binary numbers and their corresponding Difference and Borrow Outputs are specified in table 2.7. It is assumed that the second number is subtracted from the first number. First Number Second Number Difference Borrow 0 0 0 0 0 1 1 1 1 0 1 0 1 1 0 0 Table 2.7 Subtraction of two Single Bit Binary Numbers The second subtraction subtracts 1 from 0 for which a Borrow is required to make the first digit equal to 2. The Difference is 1. This is similar to decimal subtraction when 17 is subtracted from 21. The first digit 7 can not be subtracted from 1, therefore 10 is borrowed from the next significant digit. Borrowing a 10 allows subtraction of 7 from 11 resulting in a Difference of 4. 3. Binary Multiplication Binary Multiplication is similar to the Decimal multiplication except for the range of numbers. Four possible combinations of two single bit binary numbers and their products are listed in table 2.8. First Number Second Product Number 0 0 0 0 1 0 1 0 0 1 1 1 Table 2.8 Multiplication of two Single Bit Binary Numbers Multiplying two binary numbers such as 1101 x 101 is performed by a shift and add operation. The binary multiplication shifts and adds partial product terms. © Copyright Virtual University of Pakistan 18 CS302 - Digital Logic & Design 1101 x 101 1101 1st product term 0000 2nd product term 1101 3rd product term 1000001 4. Binary Multiplication by shifting left Binary Multiplication can be performed by shifting the binary number towards left. A left shift by a single bit is equivalent to multiplication by 2. A left shift by two bits is equivalent to multiplication by 4. Generally, the multiplication factor is determined by 2n where n is the number of bit shifts. 00011 (3) original binary number 00110 (6) binary number shifted left by 1 bit 01100 (12) binary number shifted left by 2 bits 11000 (24) binary number shifted left by 3 bits 5. Binary Division Division in binary follows the same procedure as in the division of decimal numbers. An example illustrates the division of binary numbers. Figure 2.1. 10 101 | 1101 101 011 000 11 Figure 2.1 Binary Division 6. Binary Division by Shifting right Binary Division can be performed by shifting the binary number towards right. A right shift by a single bit is equivalent to division by 2. A right shift by two bits is equivalent to division by 4. Generally, the division factor is determined by 2n where n is the number of bit shifts. 10100 (20) original binary number 01010 (10) binary number shifted right by 1 bit 00101 (5) binary number shifted right by 2 bits Signed and Unsigned Binary Numbers Digital systems not only handle positive numbers but both positive and negative numbers. In the decimal number system positive numbers are identified by the + sign and negative numbers are represented by the – sign. In a digital system which uses the Binary number system, the positive and negative signs can not be represented as + and -. As mentioned in the Overview all forms of numbers, text, punctuation marks etc. are represented in the form of 1s and 0s. Thus the positive and negative signs are also presented in terms of binary 0 and 1. © Copyright Virtual University of Pakistan 19 CS302 - Digital Logic & Design To handle positive and negative binary numbers, the digital system sets aside the most significant digit (bit) to represent the sign MSB set to 1 indicates a negative number MSB set to 0 indicates a positive number Thus +13 and -13 are represented as 01101and 11101 respectively. The bits 1101 represent the number 13 and the MSBs 0 and 1 represent positive and negative signs respectively. Thus binary numbers having the MSB signifying the Sign bit are treated as Signed Binary Numbers. This representation is known as the Signed-Magnitude representation. Digital systems also handle binary numbers which are assumed to be positive and therefore do not have the most significant sign bit. Such numbers are known as Unsigned numbers. Digital system thus have to handle two different types of binary numbers, signed and unsigned. Thus 111012 represents -13 in signed binary and 29 in unsigned binary. How should a Digital System treat a binary number? Should it consider it as a signed or unsigned number? A digital system on its own can not decide how to handle a binary number. The digital system has to be notified beforehand to deal with a certain binary representation as signed or unsigned. 1’s & 2’s complement Informing the digital system how to treat a binary number is not very efficient. A better way is to represent negative signed numbers in their 2’s complement form. Using 2’s Complement form to represent signed numbers, allows direct manipulation of positive as well as negative numbers without having to worry about setting the most significant sign bit to indicate positive and negative numbers. A 2’s complement of a number is obtained by first taking the 1’s complement of a number and then adding a 1 to change the 1’s complement to 2’s complement. 1’s complement of a number is obtained by simply inverting all its bits. Obtaining the 2’s complement of 13 is described in the example below. 01101 The number 13 10010 1’s complement of 13 is obtained by inverting all the five bits. + 1 10011 2’s complement of 13 is obtained by adding a 1 to its 1’s complement. In a 2’s complement number system all negative numbers are represented in their 2’s complement form and all positive numbers are represented in their actual form. Negative numbers can be readily identified by their MSBs which are set to 1. Thus in a 2’s complement representation +13 is represented as 01101 and -13 is represented as 10011. By having numbers represented in their 2’s complement form addition and subtraction operations can easily be performed without having to worry about the sign bits. Thus +13 added to -13 should result in a zero value. This can be verified by directly adding the +13 and - 13 in their 2’s complement forms. 01101 10011 100000 © Copyright Virtual University of Pakistan 20 CS302 - Digital Logic & Design The most significant carry bit is discarded; retaining only the first 5 bits proves that adding +13 and -13 results in a zero value. Similarly it can be shown that adding the numbers +7 and -13 results in -6. 10011 (-13) 00111 (+7) 11010 (-6) The binary 2’s complement number 11010 has its most significant bit set to 1 indicating that the number is negative. The actual magnitude of the negative number is determined by taking the 2’s complement of 11010. 11010 Original number 00101 1’s complement of Original number +1 00110 2’s complement of Original number is equal to 6. Addition and Subtraction Operations with Signed Binary An additional benefit of using 2’s complement representation for signed numbers is that both add and subtract operations can be performed by addition. In the above example 13 was subtracted from 7 by adding 2’s complement of -13 to 2’s complement of +7. Four cases of adding and subtracting numbers using the 2’s complement representation are shown below. Both numbers are positive 0101 +5 0010 +2 0111 +7 Both numbers are negative 1011 -5 1110 -2 11001 -7 the carry generated from the msb is discarded One number is positive and its magnitude is larger than the negative number 0101 +5 1110 -2 10011 +3 the carry generated from the msb is discarded One number is positive and its magnitude is smaller than the negative number 1011 -5 0010 +2 1101 -3 The four examples show that add and subtract operations can be carried out by an adder circuit if numbers are represented in their 2’s complement form. A separate circuit to perform subtractions is not required. Range of Signed and Unsigned Binary numbers Three different types of Binary representations have been discussed. The Unsigned Binary representation can only represent positive binary numbers. The Sign-Magnitude can © Copyright Virtual University of Pakistan 21 CS302 - Digital Logic & Design represent both positive and negative numbers. The 2’s complement signed representation also allows positive and negative numbers to be handled. Each of the three binary number representations can represent certain range of binary numbers determined by the total number of bits used. The maximum range of values that can be represented in any number system depends upon the number of digits assigned to represent the value. A 5-digit car odometer can only count up to 99,999 and then it rolls back to 00000. Similarly an 8-digit calculator can only handle integer numbers of the magnitude 99,999,999. A calculator that reserves the most significant digit to write + or – can only handle a maximum range of integer numbers from - 9,999,999 to +9,999,999. A 3-bit unsigned binary number can have values ranging between 000 and 111. Adding 100 and 111 unsigned numbers results in 1011, this result is considered to be out of range as 4 bits are required. Similarly a 4-bit sign magnitude number can handle a number range between -7 and +7. -8 can not be represented as 5-bits are required 11000. A 4-bit 2’s complement based signed number range is between -8 to +7. The table 2.9 shows the range of values that can be represented by the three Binary representations using 4-bits. Decimal Sign-Magnitude 2’s complement Unsigned form Number form form -8 1000 -7 1111 1001 -6 1110 1010 -5 1101 1011 -4 1100 1100 -3 1011 1101 -2 1010 1110 -1 1001 1111 0 0000 0000 000 1 0001 0001 001 2 0010 0010 010 3 0011 0011 011 4 0100 0100 100 5 0101 0101 101 6 0110 0110 110 7 0111 0111 111 Table 2.9 Range of values represented by 4-bit Binary representations Signed Magnitude representation can represent positive and negative numbers in the range (2n-1-1) and -(2n-1-1) where n represents the number of bits. 2’s complement signed representation can represent positive and negative numbers in the range (2n-1-1) and -(2n-1) where n represents the number of bits. The unsigned representation can represent positive numbers in the range 0 to 2n-1, where n represents the number of bits. © Copyright Virtual University of Pakistan 22 CS302 - Digital Logic & Design Lesson No. 03 NUMBER SYSTEMS Range of Numbers and Overflow When arithmetic operation such as Addition, Subtraction, Multiplication and Division are performed on numbers the results generated may exceed the range of values specified by the Binary representations. The values that exceed the specified range can not be correctly represented and are considered as Overflow values. For example, a 3-bit Unsigned representation can correctly represent Unsigned Binary values in the range 0 to 23-1 (0 to 7). Adding 3-bit Unsigned 010 (2) to another 3-bit Unsigned 111 (7) results in 1001 (9) which exceeds the 3-bit unsigned range and is considered to be an Overflow. Similarly, 1011 (-5) and 1100 (-4) values represented in 4-bit 2’s complement form when added together result in 10111 (-9) which exceeds the 4-bit 2’s complement range of values (24-1-1) and -(24-1) (7 to -8) and is considered as an Overflow. Determining Overflow Conditions for 2’s Complement Numbers The Overflow condition can be easily determined when two numbers represented in 2’s Complement form are added together. Consider the four examples described below. All numbers are represented in 4-bit 2’s Complemented form. Both numbers are positive 0101 +5 0100 +4 1001 -7 The result indicates a negative number as the most significant bit is a 1. The answer is incorrect as the result should be positive. The result indicates -7. The correct answer +9 can not be represented using 4-bit 2’s complemented form, thus an Overflow has occurred. Both numbers are negative 1011 -5 1100 -4 10111 +7 The carry generated is discarded. The result indicates a positive number as the most significant bit is a 0. The answer is incorrect as the result should be negative. The result indicates +7. The correct answer -9 can not be represented using 4-bit 2’s complement form, thus an Overflow has occurred. One number is positive and its magnitude is larger than the negative number 0101 +5 1100 -4 10001 +1 The carry generated is discarded. The result is correct. One number is positive and its magnitude is smaller than the negative number 1011 -5 0100 +4 1111 -1 © Copyright Virtual University of Pakistan 23 CS302 - Digital Logic & Design The result is correct. As 1111 represents -1. Analysis of the four addition operation indicates that Overflow conditions can be determined by looking at the most significant sign bits of the two numbers to be added together and the most significant sign bits of the sum result. In the first two examples where an Overflow has occurred the sign bits of both the numbers are the same indicating both numbers to be positive or negative respectively. The sign bit of the sum term in both cases is opposite to the signs of the two numbers being added together which can never be. Thus the erroneous sign bits indicate the Overflow conditions. Floating-Point Numbers Modern computers can handle large binary numbers such as 64-bit unsigned number, the maximum decimal number that can be represented using the 64-bit unsigned representation is 264-1 which is nearly equal to1.84 x 1019. How does a computer handle numbers larger than 264-1 or 1.84 x 1019 decimal? Secondly, numbers used routinely are not only integer numbers but numbers such as 3.14 which have an integer part and a fraction part. Thirdly, how can very small numbers such as 1.84 x 10-19 can be represented in Digital Systems? The floating-point number system, based on scientific notation is capable of representing very large and very small numbers without having to increase the number of bits. Numbers having an integer part and a fraction part are also easily represented using the Floating-Point representation. Floating point numbers are defined using certain standards. The ANSI/IEEE Standard 754 defines a 32-bit Single-Precision Floating Point format for binary numbers. The 32-bit Single-Precision F.P. format is shown in Figure 3.1. S Exponent Mantissa The single Sign (S) bit represents the sign of the number (0=positive 1=negative) The Exponent (E) 8 bits represent the exponent The Mantissa 23 bits represent the magnitude of the number Figure 3.1 Single-Precision 32-bit Floating Point Number Format Decimal Number Floating-Point Format To help understand how numbers are represented in the 32-bit Single Precision Floating Point format. Consider a similar 15 digit Decimal Number format to represent very large and very small decimal numbers. The 15-digit floating point format to represent decimal numbers is shown in Figure 3.2. S E E M M M M M M M M M M M M The Sign (S) 1 digit represents the sign of the number (+/–) The Exponent (E) 2 digits represent the exponent The Mantissa 12 digits represent the magnitude of the number © Copyright Virtual University of Pakistan 24 CS302 - Digital Logic & Design Figure 3.2 15-digit Decimal Floating Point Number Format The number 6918.3125 can be written as 6.9183125 x 103. 69183125 represents the magnitude of the number (mantissa) 3 represents the exponent The decimal point is moved to the extreme left of the number (normalized) so that the magnitude is represented by a fraction part. The number 0.69183125 x 104 is represented in decimal f.p. notation as + 0 4 6 9 1 8 3 1 2 5 0 0 0 0 Using this 15 digit (including the sign digit) notation the largest number that can be represented is 0.999,999,999,999 x 1099 Representing Negative Exponent Values The 15-digit decimal floating-point format does not allow negative exponents to be represented. There are two options available Increase the Exponent field by one digit to allow for the sign to represent positive and negative exponents. The total number of digits increases to 16. Used a Biased Exponent scheme. Instead of writing the exponent value directly add the value 50 to the exponent and write the result in the exponent field. Using this biased scheme the maximum positive exponent value that can be represented is 49 (49 + 50 = 99). The smallest exponent that can be represented is -50 (-50 + 50 = 0). After allowing positive and negative exponent values to be represented, the range of positive and negative decimal numbers that can be represented using the decimal f.p. notation is 0.999,999,999,999 x 1049 to 0.999,999,999,999 x 10-50 Representing Zero and Infinity Values How should the number Zero and the value Infinity be represented using the 15-digit decimal floating point format? The number zero can be represented by setting al the Mantissa digits to 0. The Biased exponent field can be set to any number and the sign field can be set to + or – The number infinity can not be represented. The solution to represent infinity is to set aside a biased exponent value to represent infinity. There are two options available Allow numbers having the maximum and minimum exponent values to be 48 and -49 instead of 49 and -50. Thus the Biased exponent values would range between 98 (50 + 48 = 98) and 01 (-49 + 50 = 1). The biased exponent value 00 can be used to represent the number zero whatever the value of the mantissa. The biased exponent value 99 can be used to represent the number infinity what ever the value of mantissa. Allow numbers having the maximum and minimum exponent values to be 49 and -48 instead of 49 and -50 and selecting 49 as the biased number. Thus the Biased exponent values would range between 98 (49 + 49 = 98) and 01 (-48 + 49 = 1). The biased exponent value 00 can be used to represent the number zero whatever the value of the mantissa. © Copyright Virtual University of Pakistan 25 CS302 - Digital Logic & Design The biased exponent value 99 can be used to represent the number infinity what ever the value of mantissa. This approach is perhaps better as the range of maximum positive exponent remains 49 and the range of values having a negative exponent have been reduced to -48. Representing a Decimal fraction number in 32-bit Single-Precision Floating Point format The 32-bit Single Precision Floating Point format represents the Exponent value as a Biased Number, reserving the exponent values 0 and 255 to represent the value zero and infinity respectively. The range of exponent value is from +127 to -126. The step wise representation of a decimal number 6918.3125 in 32-bit Floating Point format Convert Decimal number into equivalent Binary representation: Binary equivalent of Decimal number 6918.3125 is 1101100000110.0101 Normalizing the binary number: 1.1011000001100101 x 212 Representing the exponent in Biased 127: exponent is 12 + 127 =139 = 10001011 0 10001011 10110000011001010000000 The Mantissa is 10110000011001010000000 instead of 110110000011001010000000 as all binary numbers that are normalized always have a leading 1. In the f.p. format the leading 1 is not written, however it is taken into account in all calculations. The leading 1 which is not written is known as a hidden 1. Arithmetic Operations on Floating Point Numbers Arithmetic operations can be directly performed on floating point numbers by manipulating the mantissa and exponent parts of the floating point numbers. Two floating point numbers can be added by adding together their mantissas ensuring that the exponent parts of both the numbers are the same. If the exponents of the two floating point numbers that are to be added together are not the same than decimal point has to be adjusted for one of the floating point number to make both the exponents equal. Similarly, two floating point numbers having the same exponents can be subtracted by subtracting their corresponding mantissas. If the exponents of the two numbers to be subtracted are not equal, then decimal point is adjusted to make the two exponents equal. Multiplication is performed by multiplying the mantissas together and adding their corresponding exponents. Division is performed by dividing the mantissa parts and subtracting the corresponding exponents. The examples illustrate arithmetic operations on floating point numbers. 723 represented in f.p. as exponent 2 mantissa 7.23 + 134 represented in f.p. as exponent 2 mantissa 1.34 857 Adding together the mantissa part results in exponent 2 mantissa 8.57 723 represented in f.p. as exponent 2 mantissa 7.23 + 2015 represented in f.p. as exponent 3 mantissa 2.015 2738 Adjusting the decimal point of the first number exponent 3 mantissa 0.723 Adding together the mantissa pert results in exponent 3 mantissa 2.738 © Copyright Virtual University of Pakistan 26 CS302 - Digital Logic & Design 723 represented in f.p. as exponent 2 mantissa 7.23 - 134 represented in f.p. as exponent 2 mantissa 1.34 589 Subtracting together the mantissa part results in exponent 2 mantissa 5.89 2015 represented in f.p. as exponent 3 mantissa 2.015 - 723 represented in f.p. as exponent 2 mantissa 7.23 1292 Adjusting the decimal point of the second number exponent 3 mantissa 0.723 Subtracting the mantissa pert results in exponent 3 mantissa 1.292 723 represented in f.p. as exponent 2 mantissa 7.23 x 34 represented in f.p. as exponent 1 mantissa 3.4 24582 Multiplying the mantissa parts and adding the exponents results in exponent 4 mantissa 24.582 697 represented in f.p. as exponent 2 mantissa 6.97 ÷ 41 represented in f.p. as exponent 1 mantissa 4.1 17 Dividing the mantissa part and subtracting the exponents results in exponent 1 mantissa 1.7 64-bit Double-Precision Floating Point format The 32-bit Single precision floating point representation can represent largest positive or negative number of the order of 2127 and the smallest positive or negative number of the order of 2-126. To represent numbers larger than 2127 and numbers smaller than 2-126, 64- bit Double Precision floating point format is used. The 64-bit Double-Precision format sets aside 11 bits to represent the exponent as Biased-1023 and a mantissa of 52 bits. A single bit, the most significant bit, is set aside for the sign. Hexadecimal Numbers Representing even small number such as 6918 requires a long binary string (1101100000110) of 0s and 1s. Larger decimal numbers would require lengthier binary strings. Writing such long string is tedious and prone to errors. The Hexadecimal number system is a base 16 number system and therefore has 16 digits and is used primarily to represent binary strings in a compact manner. Hexadecimal number system is not used by a Digital System. The Hexadecimal number system is for our convenience to long binary strings in a short and concise form. Each Hexadecimal Number digit can represent a 4-bit Binary Number. The Binary Numbers and the Hexadecimal equivalents are listed in Table 3.1 Decimal Binary Hexadecimal Decimal Binary Hexadecimal 0 0000 0 8 1000 8 1 0001 1 9 1001 9 2 0010 2 10 1010 A 3 0011 3 11 1011 B 4 0100 4 12 1100 C 5 0101 5 13 1101 D 6 0110 6 14 1110 E 7 0111 7 15 1111 F © Copyright Virtual University of Pakistan 27 CS302 - Digital Logic & Design Table 3.1 Hexadecimal Equivalents of Decimal and Binary Numbers Counting in Hexadecimal Counting in Hexadecimal is similar to the other number systems already discussed. The maximum value represented by a single Hexadecimal digit is F which is equivalent to decimal 15. The next higher value decimal 16 is represented by a combination of two Hexadecimal digits 1016 or 10 H. The subscript 16 indicates that the number is Hexadecimal 10 and not decimal 10. Hexadecimal Numbers are also identified by appending the character H after the number. The Hexadecimal Numbers for Decimal numbers 16 to 39 are listed in Table 3.2. Decimal Hexadecimal Decimal Hexadecimal Decimal Hexadecimal 16 10 24 18 32 20 17 11 25 19 33 21 18 12 26 1A 34 22 19 13 27 1B 35 23 20 14 28 1C 36 24 21 15 29 1D 37 25 22 16 30 1E 38 26 23 17 31 1F 39 27 Table 3.2 Counting using Hexadecimal Numbers Binary to Hexadecimal Conversion Converting Binary to Hexadecimal is a very simple operation. The Binary string is divided into small groups of 4-bits starting from the least significant bit. Each 4-bit binary group is replaced by its Hexadecimal equivalent. 11010110101110010110 Binary Number 1101 0110 1011 1001 0110 Dividing into groups of 4-bits D 6 B 9 6 Replacing each group by its Hexadecimal equivalent Thus 11010110101110010110 is represented in Hexadecimal by D6B96 Binary strings which can not be exactly divided into a whole number of 4-bit groups are assumed to have 0’s appended in the most significant bits to complete a group. 1101100000110 Binary Number 1 1011 0000 0110 Dividing into groups of 4-bits 0001 1011 0000 0110 Appending three 0s to complete the group 1 B 0 6 Replacing each group by its Hexadecimal equivalent Hexadecimal to Binary Conversion Converting from Hexadecimal back to binary is also very simple. Each digit of the Hexadecimal number is replaced by an equivalent binary string of 4-bits. FD13 Hexadecimal Number 1111 1101 0001 0011 Replacing each Hexadecimal digit by its 4-bit binary equivalent Decimal to Hexadecimal Conversion © Copyright Virtual University of Pakistan 28 CS302 - Digital Logic & Design There are two methods to convert from Decimal to Hexadecimal. The first method is the Indirect Method and the second method is the Repeated Division Method. 1. Indirect Method A decimal number can be converted into its Hexadecimal equivalent indirectly by first converting the decimal number into its binary equivalent and then converting the binary to Hexadecimal. 2. Repeated Division-by-16 Method The Repeated Division Method has been discussed earlier and used to convert Decimal Numbers to Binary by repeatedly dividing the Decimal Number by 2. A decimal number can be directly converted into Hexadecimal by using repeated division. The decimal number is continuously divided by 16 (base value of the Hexadecimal number system). The conversion of Decimal 2096 to Hexadecimal using the Repeated Division-by-16 Method is illustrated in Table 3.3. The hexadecimal equivalent of 209610 is 83016. Number Quotient after division Remainder after division 2096 131 0 131 8 3 8 0 8 Table 3.3 Hexadecimal Equivalent of Decimal Numbers using Repeated Division Hexadecimal to Decimal Conversion Converting Hexadecimal Numbers to Decimal is done using two Methods. The first Method is the Indirect Method and the second method is the Sum-of-Weights method. 1. Indirect Method The indirect method of converting Hexadecimal number to decimal number is to first convert Hexadecimal number to Binary and then Binary to Decimal. 2. Sum-of-Weights Method A Hexadecimal number can be directly converted into Decimal by using the sum of weights method. The conversion steps using the Sum-of-Weights method are shown. CA02 Hexadecimal number C x 163 + A x 162 + 0 x 161 + 2 x 160 Writing the number in an expression (C x 4096) + (A x 256) + (0 x 16) + (2 x 1) (12 x 4096) + (10 x 256) + (0 x 16) + (2 x 1) Replacing Hexadecimal values with Decimal equivalents 49152 + 2560 + 0 + 2 Summing the Weights 51714 Decimal equivalent Hexadecimal Addition and Subtraction Numbers represented in Hexadecimal can be added and subtracted directly without having to convert them into decimal or binary equivalents. The rules of Addition and Subtraction that are used to add and subtract numbers in Decimal or Binary number systems apply to Hexadecimal Addition and Subtraction. Hexadecimal Addition and Subtractions allows large Binary numbers to be quickly added and subtracted. © Copyright Virtual University of Pakistan 29 CS302 - Digital Logic & Design 1. Hexadecimal Addition Carry 1 Number 1 2 A C 6 Number 2 9 2 B 5 Sum B D 7 B 2. Hexadecimal Subtraction Borrow 1 1 1 Number 1 9 2 B 5 Number 2 2 A C 6 Difference 6 7 E F © Copyright Virtual University of Pakistan 30 CS302 - Digital Logic & Design Lesson No. 04 NUMBER SYSTEMS & CODES Octal Numbers Octal Number system also provides a convenient way to represent long string of binary numbers. The Octal number is a base 8 number system with digits ranging from 0 to 7. Octal number system was prevalent in earlier digital systems and is not used in modern digital systems especially when the Hexadecimal number is available. Each Octal Number digit can represent a 3-bit Binary Number. The Binary Numbers and the Octal equivalents are listed in Table 4.1 Decimal Binary Octal 0 000 0 1 001 1 2 010 2 3 011 3 4 100 4 5 101 5 6 110 6 7 111 7

Use Quizgecko on...
Browser
Browser