Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
92%
(13)
92% found this document useful (13 votes)
16K views
Isc Sumita Arora Class Xi Java
SUMITA ARORA JAVA CLASS XI ISC
Uploaded by
Ritaban Ghosh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save Isc Sumita Arora Class Xi Java For Later
Download
Save
Save Isc Sumita Arora Class Xi Java For Later
92%
92% found this document useful, undefined
8%
, undefined
Embed
Share
Print
Report
92%
(13)
92% found this document useful (13 votes)
16K views
Isc Sumita Arora Class Xi Java
SUMITA ARORA JAVA CLASS XI ISC
Uploaded by
Ritaban Ghosh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save Isc Sumita Arora Class Xi Java For Later
Carousel Previous
Carousel Next
Save
Save Isc Sumita Arora Class Xi Java For Later
92%
92% found this document useful, undefined
8%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 487
Search
Fullscreen
~ COMPUTER SCIENCE with [A Textbook for Class Xl] SUMITA ARORA DHANPAT RAI & CO. (Pvt.) Ltd. EDUCATIONAL & TECHNICAL PUBLISHERSPublished by: GAGAN KAPUR Dhanpat Rai & Co. (P) Ltd., Delhi Regd. Office : 4576/15, Agarwal Road Darya Ganj, New Delhi-110002 Phone : 2324 7736, 37, 38,
[email protected]
© Sumita Arora All Trademarks Acknowledged Disclaimer Every effort has been made to avoid errors or omissions inthis publication. In spite of this, some errors might have crept in. Any mistake, error or discrepancy noted may be brought ro our notice which shall be taken care of inthe next edition. lis notified that neither the publisher nor the author or seller will be responsible for any damage or loss of action to any one, of any kind, in any manner, therefrom. EDITIONS : 2002, 2003, 2007, 2008, 2015, 2016 REPRINT : 2010, 2011, 2012, 2013, 2014, 2017, 2019, 2020, 2021, 2022, 2023, SEVENTH EDITION : 2018 REPRINT : 2024 price : & 595/- ‘Typeselting by : North Delhi Computer Convention, Delhi-110009, dce.inégmail.com Printed at : Natraj Offset, Delhi“DATA REPRESENTATION 1.1 Introduction 1.2. Numeral Systems 1.2.1 Categories of Number Systems 2 13 Digital Number Systems 1.3.1 Decimal Number System 5 1.3.2 Binary Number System 5 1.3.3 Octal Number System 7 1.3.4 Hexadecimal Number System 7 1.4 Number Conversions 1.4.1 Decimal-to-Binary Conversion 9 1.4.2 Binary-to-Decimal Conversion 13 1.4.3, Decimal-to-Octal Conversion 15 1.44 Octal-to-Decimal Conversion 15 1.4.5 Octal-to-Binary Conversion 15 1.4.6 Binary-to-Octal Conversion 16 1.4.7 Decimal-to Hex Conversion 17 1.4.8 Hex-to-Decimal Conversion 17 1.4.9 Binary-to-Hex Conversion 18 1.4.10 Hex to Binary Conversion 18 1.5. Binary Representation of Numbers 1.5.1 Binary Representation of Integers 21 1.6 Floating-Point Representation (For Real Numbers) 1.6.1 Fixed-Point Representation 27 1.6.2 Scientific Notation (Exponential Notation) 28 41.6.3 Normalized Scientific Notation 28 1.6.4 Mantissa-Exponent Notation 29 1.6.5. Single and Double Precision Representation Schemes 32 17 Binary Arithmetic 1.7.1 Binary Addition 33 1.7.2 Binary Subtraction 35 1.7.3. Binary Addition and Subtraction using One's Complement Notation 37 1.7.4 Binary Addition and Subtraction using 2's complement Notation 39 1.7.5 Binary Multiplication 41 1.7.6 Binary Division 42 Contents ssn Y Sathatsacerae SR 61] 20 718 19 1.10 4 112 Chapter 2 24 22 23 24 25 Chapter 3 31 32 33 om xiv ‘Adding and Subtracting Octal Numbers 1.8.1. Octal Addition 43 1.8.2. Octal Subtraction 44 ‘Adding and Subtracting Hexadecimal Numbers 1.9.1 Adding two Hexadecimal Numbers 45 1.9.2 Hexadecimal Subtraction 47 Representing Characters in Memory Isc Unicode | PROPOSITIONAL LoGiC & HARDWARE Introduction Propositional Logic 2.2.1 Terms and Symbols 64 2.2.2 Truth values and Wh 65 2.2.3 Some Equivalence Propositional Laws 68 2.2.4 Drawing Conclusions - Syllogism 71 Basic Logic Gates 2.3.1 Inverter (NOT Gate) 75 2.3.2 OR Gate 75 2.3.3 AND gate 76 More About Logic Gates 24.1 NOR Gate 76 NAND Gate 77 XOR Gate (Exclusive OR Gate) 78 AOR Gate (Exclusive NOR gate) 79 2.4.5. NAND to NAND and NOR to NOR Design 80 Applications of Logic Gates 2.5.1 Adders 84 ENERAL OOP CONCEPTS Introduction Evolution of Software 3.2.1 Programming Paradigms. 98 Basic Concepts of OOP 3.3.1 Data Abstraction 101 Encapsulation 101 Modularity 102 Inheritance 103 Polymorphism 104 68 6 m 6 101Chapter 5 JAVAFUNDAMENTALS 4.1 Introduction 4.2 About Java . 4.2.1 History of Java 110 . 4.2.2 Byte Code 110 4.2.3 Java Virtual Machine (VM) 112 4.24 Characteristics of Java 112 43. Simple Java Program 13 43.1 Types of Java Programs 114 44, Creating and Running a Java Program 5 44.1 General Method of Creating and Running Java Programs 115 44.2 Creating and Running Java Program Using JCreator LE 117 5.1 Introduction 5.2. Java Character Set 53 Tokens 5.3.1 Keywords 124 5.3.2 Identifiers 125 5.3.3. Literals 126 5.3.6 Separators 129 5.3.5 Operators 129 54 Concept of Data Types 129 5.4.1 Primitive Datatypes 130 5.4.2. Reference Datatypes 133 55 Variables Bs 5.5.1 Declaration of a Variable 135 5.5.2. Initialization of Variables 136 6 Constants 139 5.7 Operators in Java Mi 5.7.1 Arithmetic Operators 141 Increment/Decrement Operators (+*, ~~) 142 Relational Operators 144 Logical Operators 145 Shift Operators 147 Bitwise Operators 149 Assignment Operators 151 Other Operators 153 Operator Precedence 155 58 Expressions ad 5.8.1 Arithmetic Expressions 1585.9 Java Statements 163 5.10. Significance of Classes 16 5.11 Objects as Instances of Class wm hapier 6 | FLlowor CONTROL 185-247 6.1 Introduction 185 6.2 Programming Constructs 185 63 Selection Statements e 6.3.1 The if Statement of Java 187 6.3.2 The switch Statement 199 208 65 Elements that Control a Loop (Parts of a Loop) 208 6.6 The for Loop - Fixed Number of Iterations 209 6.6.1 The for Loop Variations 212 67 The while Loop 4 6.7.1 Variations in a while Loop 205 68 The do-while Loop 26 6.9 Nested Loops 27 6.10 Comparison of Loops 28 6.11 Jump Statements 8 6.11.1 The break Statement 218 6.11.2 The continue Statement 220 6.11.3 Labels and Branching Statements 221 C hapier 7 | CLASSES IN JAVA 243 - 264 7.1 Introduction m3 7.2. Class as Composite Type 2a 7.2.1 Class as User-defined Datatype 245 7.3. Creating and Using Objects 246 7.3.1. Using Objects 248 7.3.2 Controlling Access to Members of a Class ~ Access Specifiers 250 74 Encapsulation A 75. Visibility Modifiers = 7.6 Scope and Visibility Rules a CS hapters [FUNCTIONS (METHODS) _ ; 265-312 8.1 Introduction 8.2. Why Functions ? ae 267 83 Function/Method Definition 8.3.1 Function Prototype and Signature 269 enn? om84 85 86 87 88 89 8.10 sil 812 813 814 8.15 8.16 817 hapter 9 O41 92 93 94 95 96 Accessing a Function 8.4.1 Actual and Formal Parameters 273 8.4.2 Arguments to Functions 273 Pass By Value (Call By Value) Call By Reference Returning from a Function 8.7.1 The return statement 283 8.7.2 Returning Values 283 Pure and Impure Functions Function Overloading 8.9.1 Need For Function Overloading 285 8.9.2. Declaration and Definition 287 Calling Overloaded Functions Constructors 8.11.1 Need for Constructors 290 8.11.2 Declaration and Definition 290 Types of Constructors 8.12.1 Non-Parameterized Constructors 292 8.12.2 Parameterized Constructors 292 The this Keyword ‘Temporary Instances Constructor Overloading Recursive Functions Recursion in Java PROGRAM ERROR TYPES AND BASIC EXCEPTION HANDLING Introduction ‘Type of Program Errors 9.2.1, Compile-Time Errors 314 9.2.2 Run-Time Errors 316 9.2.3 Logical Errors 317 Exception and Exception Handling, 9.3.1 What happens when an Exception occurs ? 321 9.3.2 Concept of Exception Handling 322 9.3.3 Exception Handling in Java 323 9.3.4 The Exception Class 327 9.3.5 Common Exceptions 328 Exception Hierarchy Forcing an Exception Benefits of Exception Handling a 274, 275 282 285 BB 291 296 297 298 299 313 - 338 313, 314 319Chapter 10 | UsiN LiBRARY CLASSES AND PACKAGES 10.1 Introduction 10.2 Simple Input/Output 10.3. Exception Handling, 10.3.1 Concept of Exception Handling 348 10.3.2 Exception Handling in Java 349 104 Wrapper Classes 10.5 Working with Strings 10.5.1 Greating Strings 356 10.5.2 Creating StringBuffers 356 10.5.3 Accessor Methods 355 10.6 Packages in Java 10.6.1 Importing Packages and their Classes 364 10.6.2 Using Dates and Times (Date and Calendar Objects) 364 10.6.3 Packages in Java 366 10.6.4 User Defined Packages 367 © hapter 11 “ARRays 11.1 Introduction 112 Need for Arrays 113 Types of Arrays 11.3.1. Single Dimensional Arrays 376 11.3.2 Some Facts about Arrays 381 11.3.3 Two-Dimensional Arrays. 382 114 Searching in 1-D Arrays 115 Sorting 116 Arrays vs. Objects 11.7 Advantages and Disadvantages of Arrays 11.8 Solution of Simultaneous Linear Equations 11.8.1 Gauss Elimination Method 399 11.8.2 Generalised Solution by Gauss Elimination Method and Corresponding Computer Program 403 Chapter 12 OPERATIONS ON FILES 12.1 Introduction 12.2 Files 12.3 Java Streams 124 Operations on Files 12.4.1 Buffering 419 339 340 348 352 354 363 375-416. 375 376 376 387 392 397 398 39912.4.2 Output to Text Files 420 12.4.3 Input from Text Files 423 12.44 Output to Binary Files 424 12.4.5 Input from Binary Files 426 125 String Tokenizer 428 12.5.1 Processing a Number Sequence with StringTokenizer 430 12.5.2 String Tokenizing and File Handling 431 12.6 Stream Tokenizer 432 127 Obtaining Input using Scanner Class 435 128 Printing in Java 439 12.8.1 Java Printing API 439 hapter 13 TRENDS IN COMPUTING AND ETHICAL ISSUES é 4 -475 13.1 Introduction 453 13.2. Trends in Computing 454 13.2.1 Artificial Inteligence 454 13.2.2 Internet of Things (IoT) 456 13.2.3 Virtual Reality (VR) 456 13.2.4 Augmented Reality (AR) 457 13.3. Cyber Security 458 13.3.1 Threats to Computer Security 458 13.3.2 Oyber Security Measures 460 13.3.3 Online Privacy 462 13.4 Major Ethical Issues 464 13.41 Individual’s Right to Privacy 464 13.4.2 Intellectual Property Rights 465 13.43 Accuracy of Information 465 13.5 Open Source and Its Philosophy 465 13.5.1 Terminology 466 13.5.2 Philosophy of Open Source 466 13.5.3 Definitions 467 13.5.4 Licenses and Domains of Open Source Technology 469 47 13.6 Netiquette an 137 Email Etiquette Appendices 476-480el 12 c HA PLT ER Data Representation Introduction Digital techniques have found their way into innumerable areas of technology, but the area of automatic digital computers is by far the most notable and most extensive. As you know, a computer is a system of hardware that performs arithmetic operations, manipulates data, and makes decisions. In science, technology, business, and, in fact, most other fields of endeavour, we are constantly dealing with quantities; so are computers. Quantities are measured, monitored, recorded, manipulated arithmetically, observed, or in some other way utilized in most physical systems. In digital systems like computers, the quantities are represented by symbols called digits. Many number systems are in use in digital technology that represent the digits in various forms. The most common are the decimal, binary, octal, and hexadecimal systems. This chapter discusses these number systems and the physical representation of digits in computers. Numeral Systems Numeral systems or Numbering systems or simply Number Systems are a way of representing numbers by a finite set of distinct graphic symbols or signs. Same number can be represented differently in different numeral systems. For instance, number 42 of decimal system can also be represented as 52 in octal number system or as 2A in hexadecimal 1a 12 13 14 15 16 uz 18 19 1.10 Ce 112 113 aaa In This Chapter Intsoduction Numeral Systems Digital Number Systems Number Conversions Binary Representation of Numbers Floating — Point Representation Binary Arithmetic Adding and Subtracting Octal Numbers ‘Addting and Subtracting Hexadecimal Numbers ‘Adding and Subtracting Floating-Point Numbers Limitations of Finite Representation Representing Characters in Memory Isc Unicode number system, SPEC: . Ni are =iCOMPUTER SIE} 2 NCE (asc) 1.2.1. Categories of Number Systems , rrently also multiple number systems a used in past. Cu Several number systems have been P rit into po categories: in use. These number systems can be broadly divid' © Non-positional number systems ® Positional number systems Let us briefly discuss about their difference and then we shall continue with Positiona number systems as per the syllabus. 1.2.1A Non-Positional Numeral/Number Systems In early days, people counted using fingers, stones, pebbles or sticks etc. Non-positionay number systems are based on this method of counting and additive approach, jy non-positional number system, a symbol carries the same value always irrespective of it, position. Two such number systems are : Roman Number System and Greek Number System, man Numeral System One popular non-positional number system is the Roman numeral system, which uses sequences of the following symbols to represent the numbers : Roman Digit _| Decimal Value | Roman Digit | Decimal Value I 1 Cc 100 v 5 D 500 : x 10 M 1000 L 50 | As this is non-positional number system, the position of the digits has no significance for the value of the number depicted in this number system. For determining the value of a Roman number, the following rules are applie: (i) If two consecutively represented Roman digits are in such order that the value of the first one is bigger or equal to the value of the second one, their values are added. For example : qr [values of three I’s added} MO = 2500 [values of M, M and D added) (ii) If two consecutively represented roman digits are in increasing order of their values, they are subtracted, For example : IxX=9 [value of I subtracted from value of X] MXL= 1048 [value of X subtracted from value of L, giving 4@ which is added to M} C= 980 [value of C subtracted from value of M]DATA REPRESENTATION Greek Numeral System Another non-positional number system is Greek numeral system. In this number system, grouping of fives is done, Wuses the following digits : Greek Digit | Decimal Value | Greek Digit | Decimal Value 1 1 H 100 r 5 x 1,000 A 10 M 10,000 As we can see in the table, one is represented with a vertical line, five with the letter[, and the powers of 10 with the first letter of the corresponding Greek word. Here are some examples of numbers in this system : Ta =50=5x 10 fh = 500 =5 x 100 Ix = seee = 5 x 1,000 Im = 50,000 = 5 x 10,000 In a non-positional number system, the placement of a numeral di value. does not change its 1.2.1B Positional Numeral/Number Systems Positional number systems (also called place-value numeral systems) are systems in which the placement of a digit in connection with its intrinsic value determines its actual weight or value in the number being presented For instance, In 135, Shas value 5x1=5 153, 5 has value 5x10=50 531, 5 has value 5x 100= 500 As you can see in above examples that digit ‘5’ is in all three numbers but each digit ‘5 carries different weight (actual value) depending upon its position in the number. Before we proceed, let us know about an important term related to positional number system — Base or Radix. Base or Radix This is an important term associated with positional number systems. A Base or Radix tells, how many distinct graphic symbols are used to represent number in a number system. For example, in decimal number system, the base is 10 as it uses ten (10) different symbols {0, 1, 2, 3,4, 5, 6, 7, 8, 9} to represent numbers. Whatever mathematics you have stuclied so fat, has been based on Decimal number system. So you can safely say that you have been writing and using numbers based on Decimal number system. ‘This base or radix plays an important role in determining positional value of a digit,COMPUTER SCIENCE. ; Ogg Number Representation in Positional Numeral Systems Perea D icts om In Positional number systems , a number is represented’ For example conic value ofeach digit using base or radix of the number system. For AP, Sonbider deci number system, you are familiar with Decimal number system on Working with since you started schooling. The decimal number system repre: sents numbers where each digit has a place value, Htuncroda 4110 (Tents) = 1/100 (Hundredtns) naths) im 111000 (Thousat ! [- 437.591 — 10x >bigger 10 x smaller | ; | If you just consider the non-fractional part 437 of above number, you can represent it as 4x10? 43x10! +7 x 10° . As you can make out the positional value of 4 becomes 4x 100= 400 ; positional value of 3 becomes 3x 10= 30 and positional value of 1 is 1x1=1. : -2 -3 Similarly, you can represent its fractional part 591 as : 5x10 +9x10™ +1%10 1 2 a So the total number is represented as :4 x 10? +310! +7%10° + 5x10 +9%10™* +1109 You must have noticed that each digit’s positional value depends upon the position of a digit multiplied with a power of base (radix) of the number system (base in this case is 10). Following figure also illustrates the same : Decimal Number Sar Position Number Base or 5/3 /1/9 Redixis 10 | Note In a_ positic Qx10°=9x1 = 9 number system, A position a digit occuy 1x10. =1x10 = 10 multiplied by a po 3x10°=3% 100 = 300 the radix of the nu 3 system determines 5x 10 =5 x 1000=5000 | seualvatue, Final sum (he number) — 531949 ‘The base (radix ofthe number system. Figure 1.1 Decimal Number System - a Positional Number System. In the coming lines, we shall be covering four positional number systems — decimal, binary, octal and hexadecimal which are commonly used in digital calculations. | In fixed-radix positional number systems only there can be mixed-radix positional number systems also. But he we shall stick to discussion of fixed-radix number systems as discussion, of other types of positional number systems is beyond the scope of this book.DATA REPRESENTATION 13 1.3.1 1.3.2 Digital Number Systems In digital representation, various number systems are used. The most common number systems used are decimal, binary, octal, and hexadecimal systems. Let us discuss these number systems briefly. Decimal Number System The decimal system is composed of 10 numerals or symbols (Deca means 10, that is why this system is called decimal system). These 10 symbols are 0, 1, 2, 3, 4,5, 6, 7, 8,9 ; using these symbols as digits of a number, we can express any quantity. The decimal system, also called the base-10 system because it has 10 digits, has evolved naturally as a result of the fact that man has 10 fingers. The decimal system is a positional-value system in which the value of a digit depends on its position. For example, consider the decimal number 729. We know that the digit 7 actually represents 7 hundreds the 2 represents 2 tens and the 9 represents 9 units In essence, the 7 carries the most weight of three digits; itis referred to as the most significant digit (MSD). The 9 carries the least weight and is called the least significant digit (LSD). Consider another example, 25.12. This number is actually equal to (2 fens plus 5 units plus 1 tenths plus 2 hundredths) ie, 2x10+5x1+ 1x s +2x a The decimal point is used to separate the integer and fractional parts of the number. More rigorously, the various positions relative to the decimal point carry weights that can be expressed as powers of 10. This is illustrated in Fig. 1.2 where the number 2512.1971 is represented. The decimal point separates the positive powers of 10 from the negative powers. The number 2512.1971 is thus equal to 2x 10° +5x 10? +1x10' +2x10° +1x10-1 +9x10* +7x10 9 +110 In general, any number is simply the sum of the products of each digit value and its positional value 10% 3/102 \% 10%» 110°"; 107 10s, ONT LOE a + + ‘ + + + + 1 2 5 1 2 fi 9 7 1 + t + MsD Sd LsD point Figure 1.2 Positional values in decimal numbers. The squence of decimal numbers goes as 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21.... See after 9, each successive number is a combination of two (or more) (unique) symbols of this system. Binary Number System Unfortunately, the decimal number system does not lend itself to convenient implemen- tation in digital systems. For example, itis very difficult to design electronic equipment so thatCOMPUTER SCIENCE-X! (Isc) one decimal character, 9 mn simple, accurate electronic circuits thay my, almost every digital system uses the tem of its operations, although other it can work with 10 different voltage levels (each one representing through 9). On the other hand, it is very easy to desigi operate with only two voltage levels. For this reaso binary number system (base 2) as the basic number sys a used in conjunction with binary. mrnctiy ont ossible digit values, 0 and 1. Even so, this In the binary system there are only two symbols or ane tienen base-2 pene be used to represent any quantity that can be represe! on other number systems. ; nan Ss ‘The binary system is also a positional-value system, vwhergin o80 rahe digit own value or weight expressed as a power of 2. This is illustrated in ig a , . Here, places to the left of the binary point (counterpart of the decimal CO es powers of 2 and places to the right are negative powers of 2. The number 1018" wn represented in the figure Paneer ort ork 28, Rat values t v + L L + Y + E 9 1 0 i 0 1 0 i iu fs t (Most Sontcan SB phen LSB (Least Significant ba point Bi) Figure 1.3 Positional values in binary numbers. To find the decimal equivalent of above shown binary number, we simply take the sum of the products of each digit value (0 or 1) and its positional value : 1010.0101, = (1 x 23) + (0x 2?) + (1x 2!) +(0x 2°) +(0x2~) +(1x2 72) +(0x2 9) +(1x2 4) =8+042+0+0+0.25+0+ 0.0625 =10.3125;9 Notice in the preceding operation that subscripts (2 and 10) were used to indicate the base in which the particular number is expressed. This convention is used to avoid confusion whenever more than one number system is being employed In the binary system, the term Binary digit is often abbreviated to the term bit, which we'll use henceforth. As you see in Fig. 1.3, there are 4 bits to the left of the binary point, representing the integer part of the number, and 4 bits to the right of the binary point, representing the fractional part. The leftmost bit carries the largest weight and hence, is called the most significant bit (MSB). The rightmost bit carries the smallest weight, and hence called least significant bit (LSB). The sequence of binary numbers goes as 00, 01, 10, 11, 100, 101, 110, 111, 1000, - --- - -. The binary counting sequence has an important characteristic. The units bit (LSB) changes either from 0 to 1 or 1 to 0 with each count. The second bit (two's (2') position) stays at 0 for two counts, then at 1 for two counts, then at 0 for two counts, and so on. The third bit (four’s (2) position) stays at 0 for four counts, then at 1 for four counts, and so on. The fourth bitTA REPRESENTATION : 13.3 1.3.4 eo ce oa Ailes $ a for eight counts, then at 1 for eight counts If we wanted f0 a : : alternating in groups of 24-1 "OT? Pl8ces, and this patter would continue with Os and 1s Octal Number System tal i j Gee rae on 3S very important in digital computer work. The octal number syst h dig of eight, meaning that it has eight unique symbols : 0, 1, 2, 3, 4, 5, 6, and 7. Thus, each digit of an octal number can have any value from 0 to 7. The octal system is also a positional value system, or weight expressed as a power of 8 (See Fig, 1 a decimal point and binary point) are positive powers of 8 and places to the right a ‘ative Powers of 8. The number 3721.2406 is shown Tepresented in the figure. wherein each octal digit has its own value .4). The places to the left of the octal point 8 8 a positional see D.C 0 values > 4 4 (weights) NS eS ad . 7 2 1 r 2 | 4 0 6 1 Octal point Figure 1.4 Positional values in Octal numbers. To find the decimal equivalent of above shown octal number, simply take the sum of products of each digit value and its positional value : 372.2406, = (3x 8°) + (7 x 87) +(2x 8!) + (1x 8%) +(2x 8-1) +(4x 82) + (0x2) + (6x84) =3x 512 +7 x 64 +2x 8+1x1+2x 0.12544 x 0.015625 + 0 +6 x 0.000244 = 1536 + 448 +16 +1 + 0.25 + 0.0625 + 0+ 0.001464 = 2001.313964 yo The sequence of octal numbers goes as 0, 1, 2, 3, 4,5, 6, 7, 10, 11, 12, 13, 14, 15, 16, 17, 20, 21, 22.4. See each successive number after 7 is a combination of 2 or more unique symbols of octal system. Hexadecimal Number System ‘The hexadecimal system uses base 16. Thus, it has 16 possible digit symbols. It uses the digits 0 through 9 plus the letters A, B, C, D, E, and Fas the 16 digit symbols. Just like above discussed systems, hexadecimal system is also a positional-value system, wherein each hexadecimal digit has its own value or weight expressed as a power of 16. (See Fig. 1.5). The digit positions in a hexadecimal number have weights as follows : 1, e167 | 167 | 16 | 16 Hexadecimal point Weights 16° | 162 | 16 | 16° Figure 1.5 Positional values in hexadecimal number.14 COMPUTER SCIENCE. (5) Following table 1.1 shows the relationships between hexadecimal, octal, decimal and binary, Table 1.1 Relationship between various number systems Hexadectmal Octal Dectmal Binary ] 0 0 0 ‘0000 1 1 1 0001 2 2 2 010 a 3 3 0011 4 4 4 0100 d 5 5 o101 o 6 6 ono u 7 7 out . 10 8 1000 a u 9 1001 A a 10 1010 5 u) u 1011 c a 12 1100 a 15 1B 1101 . 16 14 1110 : ud 15, mu Note that each hexadecimal digit represents a group of four binary digits. It is important to remember that /iex (abbreviation for hexadecimal) digits A through F are equivalent to the decimal values 10 through 15. Number Conversions The binary number system is the most important one in digital systems as itis very easy to implement in circuitry. The decimal system is important because it is universally used to represent quantities outside a digital system. In addition to binary and decimal, octal and hexadecimal number systems find widespread application in digital systems. These number systems (octal and hexadecimal) provide an efficient means for representing large binary numbers, As we shall see, both these number systems have the advantage that they can be easily converted to and from binary. Ina digital system, three or four of these number systems may be in use at the same time, so that an understanding of the system operation requires the ability to convert from one number system to another. This section discusses how to perform these conversions, So, let us discuss them one by one,DATA REPRESENTATION 1.4.1. Decimal-to-Binary Conversion There are two procedures for converting (integers) from decimal to binary, The first method requires a table of powers of 2 (Table 1.2). Because of this restrictio. 4 more useful for small numbers where these powers have been Semcon ae decimal number to be evaluated, obtain the largest power of 2 from the table withe : exceeding the original number. Record this, Then subtract the table obtained number from the original number. Repeat the process for the remainder, and continue until the remainder cg zero. Finally, add the binary numbers obtained from the table. The result is the anewes Table 1.2 Powers of 2 Be a Ea ro oStG 2 105: 4 2 025 8 = 3 0.125 16 4 00625 32 5 0085 64 6 (0015625 18 7 007 8125 256 © 8 (0.003 905 25 5129 0001 953125 024 = 10 0.000 976 562 048 «11 0.000488 28125 096 = 120.000 244 140625 192 13 0000 122 070 312 16 384 = 14 0.000 06103515625 32 768 «= 15 0.000 030 «517-578 125, 65 536 © 16 (0.000 O15 258.789 0625 11 072-17 (0.000 007629394 Sa1 5 262 144 «18 0.000 003 814. 697-265 625 524 288 19 0.000 001 907 348 632-812 1 048 576-20 0.000 000-953 67431640625 2 097 152 21 0.000 000 476-837 «158203125 4 194 304 22 0.000 000 238 «418-579 101s Sez 8 388 608 23 0.000 000 119 209 289 55078125 16 777 216 © 24 0.000 000 059-604 644-775-390 625 33 554 432 «250.000 000 029-802-322 387 69S 312 O14 901 161 193 84765625 007 450 580 59692382825 003-725 «290 «298 «461M 082 001 862 645149 230957 OBB. 000 931 322 574 61S 478515 625 000 465 661387 307739257 000-232 830 643653869628 000 116 «415321826 984 BIL 000 058 «= 207660913. 487407 000 029 «103 830 456 -733«-703.«613. 281-25 000 000-014 «551 «915-228-366 8S1 806 40625, 67 108 «864 ~=— 260.000 134-217-728 =—27_:0.000 268 435 456 «(28 (0.000 536 870 912 29 0.000 073 741 «824 «300.000 147 483 64831. (0.000 294 967 296 320.000 589 934 592 -33:0.000 179 869 «184 34. (0,000 gese288828 8838 g 3 8 3 g 719 476 736 36 0.00010 A COMPUTER SCIENCE-XI (Isc) ing 43. Example 1.1. Convert 43,9 to binary. Solution. From Table 1.2, 32 is the large: 32 = 100000 at all other posi! .st number without exceed! i sions. Thus 329 = 1000002) (2° =32ie, put 1 at (5 +1) th position and 0, 43-32=11 From the table, 8 (2°) is the largest number without exceeding 11. 8=1000 (2? =8ie., put1 at (3+1)" position and 0, at all other positions. Thus 80 =10002) 11-8=3 From the table, 2 is the largest number without exceeding 3. 2=10 (2' =2, thus 2y9 =102) 3-2=1 1 is the largest number without exceeding 1 1s1 Add all the binary numbers obtained ic, 100000 1000 10 1 701011 Thus 43,9 = 101011, Example 1.2. Convert 2009 to binary. Solution. First largest number is 128. Longest number 128 = 10000000 200-128 =72 64= 1000000 2-64=8 = 8 1000 200= 11001000 Therefore, 200 =11001000, The second method of converting decimal to binary is repeated-division method. In this oa ine number is successively divided by 2 and its remainders recorded. The final ‘ary result is obtained by assembling all the remainders, with i fea caer ee is s, with the last remainder being the11 DATA REPRESENTATION Example 1.3. Convert 439 to binary using repeated division method, Solution. Repeated Division Remainders 2 43 7 21 a LSB - 10 1 edd 5. 0 Write in this 2 2 1 order 2 1 0 e 1 MSB Reading the remainders from the bottom to the top, 4319 =101011, (compare with result of example 1.1) Example 1.4. Convert 2009 to binary using repeated division method. Solution. Remainders 200 100 50 LSB fee o | 23. HHocHsoe olale MSB Reading the remainders from the bottom to the top, the result is : 200j5 =11001000, On paper you may even compute 85/2 the conversion as depicted through aN following example. (As you can see 422 that this is just another way of Tepresenting the repeated division.) 11 Least significant bit (LSB) Example 1.5. Convert 854 to binary using repeated division method. Solution. (See on the right) Q- Quotient and oe R~Remainder 42 a/"Ke 8549 = 1010101, v \ Most significant bit (MSB)COMPUTER SCIENCE 12 anees, i i i ny is to successively my, 1.4.1A Converting Decimal Fractions to Bino ocedure is ulti i inary, the PF ¢ all numbers to the jen 2” 2 ipa a ead os ea is case) and ae left of lecimal fraction by the radix i, bas lowing decimal point, reading down. Consider the foll it Integer Part Example 1.6. Convert 0.3759 to binary. Integer Solution. Original number 0.375 Cyto mold ‘Multiply (fractional part) 0.375 by2 50 Be is Multiply (fractional part) 0.75 by : 7 1 by fl ‘Multiply (fractional part) 0.50 Reading down the integer par! Example 1.7, Convert 0.545459 to binary equivalent. Integer-Part Solution, Original number 0.54545 tes —————»1 ts, 0.375=0.0112 054545 x2 eI (00909 x 2 01818 x2 03636 x 2 07272 x2 0.4544 x2 09088 x 2 081762 06376 x 2 02704 x2 0.5408 This sequence keeps on continuing, thus, this particular number can never be ex} exactly in binary. Therefore, reading down the numbers, 0.54545 = 0.1000101110, to 10 pl As the previous example illustrates, many decimal fractions cannot be represented as e binary numbers. To convert a mixed number such as 38.21 to binary, first convert the integer to its bis equivalent, then the fraction. Example 1.8. Convert 38.219 to its binary equivalent. Solution. Converting the integer part ie,, 38. Remainders o 0 o : writing 1) order o 1 1 1 0 a Hr sw oonnme it Reading up, 38; =100110,ry Ft sepRESENTATION 13 Converting the fractional part 0.21 Integer-part 021x2 =042 042x2 = 0.84 0 084 x2 =168 1 0682 = 136 a 0.36 x2 =072 0 072%2 21.44 1 0.44 x2 = 0.88 0 0.88 x2 =176 1 076x2 =152 1 0522 = 104 1 0.04 x2 = 0.08 0 Reading down, 0.21 9 = 0.00110101110 Adding the integer and fraction, 38.21 jy = 100110.0011010111 Conversion Rules for Radix based Number Systems 1. Separate the integeral and the fractional parts. 2, For the integeral part, divide by the target radix repeatedly, and collect the successive remainders in reverse order. 3, For the fractional part, multiply the fractional part by the target radix repeatedly, and collect the successive integeral-parts in the same order. 1.4.2. Binary-to-Decimal Conversion ‘The binary number system is a positional system where each binary digit (bit) carries a certain weight based on its position relative to the LSB. Any binary number can be converted to its decimal equivalent simply by summing together the weights of the various positions in the binary number which contain a 1. To illustrate, consider a binary number 11011, 1] 1] of 1] 12| (inary) ‘Add positional weights for all 15, 24423 40421 +29 = 16484241 7 1p (decimal) Let's try another example with a greater number of bits ie, 10110101, 7] 0] 1] 1] 0] 1) 0|% ‘Adding positional weights of all t's. - 27 40425 +24 +042? +0429 = 18139COMPUTER SCIENCE-X (isc) : th bit posit Note that the procedure is to find the weights (i., powers of 2) oa tof? te fa contains a 1, and then to add them up. Also note that the MSB lias a weis} igh its 4 5 the eighth bit; this is because the LSB isthe first bit and has a weight of 2°. / The above method will always provide the correct decimal representation of a’ binary umber, There is a second method, called the dibble-dobble method, that will also provide the solution, To use this method, start with the left-hand bit. Multiply this value by 2, - aad the next bit to the right. Multiply the result value by 2, and add the next bit to the right. Stop when the. binary point is reached. To illustrate, 11011, (binary) Copy down the leftmost bit : 1 Multiply by 2, add next bit (2x1) +1=3 Multiply by 2, add next bit (2x3) + 0 =6 Multiply by 2, add next bit (2x6) +1=13 Multiply by 2, add next bit (2x13) +1=27 » 11011, =2749 Let us try another example, 110100, The leftmost bit : 1 Multiply by 2, add next bit (2x1) +1 Multiply by 2, add next bit (23) +0 Multiply by 2, add next bit (2x6) +1 =13 Multiply by 2, add next bit (213) + 0 =26 Multiply by 2, add next bit (226) + 0 =52 110100, = 5249 1.4.24 Converting Binary Fractions to Decimal To find the decimal equivalent of binary fraction, take the sum of the products of each digi value (0 to 1) and its positional value. To illustrate : a7 27 20 2-4 ¢___ Position values o.t01 | 0 1 0 1 Binary Pont = (0x27) +(1x2)+(0x2 4) 40x24) =0+0.25+ 0+ 0.0625= 0.375 0.0101, = 0.312519. Example 1.9. Convert 1101.000101, to decimal. Solution. = 1x2? 412? 0x21 414294 0x21 40x22 40x29 412-4 0x2 41x2% =844404140+04+0+1 4041 16 ot = 13 + 0.0625 + 0.015625 = 13,078125 1101.000101, =13.078125,,DATA REPRESENTATION 15 1.4.3 Decimal-to-Octal Conversion A decimal integer can be converted to octal by we used in the decimal-to-binary conversion, example is shown below : ¥y using the same repeated-division method that 1, but with a division factor of 8 instead of 2. An Remainders 3 266 8] 33 2 [4 1 0 4 Reading up, 26649 = 412, Note that the first remainder becomes the least significant digit (LSD) of the octal number, and the last remainder becomes the most significant digit (MSD). 1.4.3A Converting Decimal Fraction to Octal 1.4.4 1.4.5 To convert a decimal fraction into octal, the procedure is to successively multiply the decimal fraction by the radix i, base (8 in this case) and collect all the numbers to the left of the decimal point, reading down. Consider the following examples. Example 1.10. Convert 0.37545 to octal Solution. Original number 0.375 Integer-part Multiply (fraction part) 0.375 by 8 =3.0 3 0.3759 = 0.35 Example 1.11. Convert 0.015625,9 to octal. Solution. 0.015625 8 =0.125 0 0.125x8=1.0 iu | Reading down, 0.015625) = 0.01, Octal-to-Decimal Conversion An octal number, then, can be easily converted to its decimal equivalent by multiplying each octal digit by its positional weight. For example, 372g = 3x (8°) +7 x(81) +2x(8" = 3x 6447x8+2x1=25019 Another example : 24.6, = 2% (8!) +4 x (8°) +6x(8 ') =20.75,5 Octal-to-Binary Conversion The primary advantage of the octal number system is the ease with which conversion can be made between binary and octal numbers. The conversion from octal to binary is performed by converting each octal digit to its 3-bit binary equivalent. The eight possible digits are converted as indicated in Table 1.3.| COMPUTER SCIENCE-X! (Isc) 16 eneenenceneeneenenenseenenentes Table 1.3 Binary equivalents of octal digits Octal Digit 0 1 2 3 4 5 6 Z Binary Equivalent | 000 001 010011100201 m0 11 Using these conversions, any octal number is converted to binary by individually converting each digit. For example, we can convert 472g to binary using 3 bits for each octal digit as follows : 4 7 2 4 4 J 100 11 010 Hence, octal 472 is equivalent to binary 100111010. As another example, consider converting 5431 to binary : 5 4 3 1 + a L L 101 100 oll Thus, 5431, =101100011001. ‘The same process applies equally on fractions. For example, G91 ho ob + O11 * 001 31, = 011.001, 1.4.6 Binary-to-Octol Conversion Converting from binary integers to octal integers is simply the reverse of the foregoing process. The bits of the binary integer are grouped into groups of three bits starting at the LSB. Then each group is converted to its octal equivalent (Table 1.3). To illustrate, consider the conversion of 100111010, to octal. 100 11 io to v + v 4 7 25 Sometimes the binary number will not have even groups of 3 bits. For those cases, we can add one or two 0s to the left of MSB of the binary number to fill out the last group. This is illustrated below for the binary number 11010110. o11 010 110 added nee tomate 1 b it group of 3 bits = 3 2 65 Note that a 0 was placed to the left of the MSB in order,to produce complete groups of 3.EPRESENTATION seneee seeneeeesaseasencesesss 17 DATA RE The same process applies on fractions. But after the binary point, zeros are added to the right, For example a— 10110.0101, = 010 110 010 160 2 6 2 4 \ 2m ulded here vy 10110.0101 = 26.24 mabe ita grasy of 3 ies Note that, after the binary point, the groups of 3 bits are made starting from left-to-right. That is why, we added two zeros to make a group of three bits as the last group had only 1. 1.4.7 Decimal-to Hex Conversion Recall that we did decimal-to-binary conversion using repeated division by 2, and decimal-to-octal using repeated division by 8. Likewise, decimal-to-hex conversion can be done using repeated division by 16. For example, to convert 4234, to hex, Remainders 16 423 “16 | 26 74 ue a S (10jg = Ars) | 0 1 Reading tip, 423) = 147 16 Similarly, to convert 2149 to hex, Remainders 16 214 “16 | 13 6 0) 2 (139 = Dis) Reading up, 214 1p = D6 5 Note that any remainders that are greater than 9 are represented by letters A through F. 1.4.7A Converting Decimal Fractions to Hex The same old procedure applies here also, that is, successively multiply the decimal fraction by the radix (base) (16 here) and collect all the numbers to the left of the decimal point, reading down. For instance, 0.031254 is converted as 0.03125 16 = 0: 05x16 0.031255 = 0.08, 1.4.8 Hex-to-Decimal Conversion A hex number can be converted to its decimal equivalent by using the fact that each hex digit position has a weight that is a power of 16. The LSD has a weight of 16° =1, the next higherCOMPUTER SCIENCE-XI (ISC) digit has a weight of 16! =16, the next higher digit has a weight of 16” = 256, and so on. The conversion process is demonstrated in the examples below : 356g = 3x16? +5 x16! +6x 16% =768+80+6 5410 2x16? +1016! +15%16° 12+ 160+15 = 68719 Note that in the second example the value 10 was substituted for A and the value 16 for F in the conversion to decimal. To convert a fractional number, 56.081, = 5x16! +6x16° +0x 167! +8x16 =80+6+0+8/ 256 = 86 +0.03125 = 86.03125 1.4.9. Binary-to-Hex Conversion Binary numbers can be easily converted to hexadecimal by grouping in groups of four starting at the binary point. Example 1.12, Convert 1010111010, to hexadecimal. Solution. Group in fours 10,1011,1010 Convert each number 2BA Thus, the solution is 2 BA. Example 1.13. Convert 10101110.010111, to hexadecimal Solution. Groups in fours (inserting zeros before MSB or in the end, wherever required) 1010, 1110 . 0101, 1100 A E.5 C 10101110.010111 = AESCy, 1.4.10 Hex to Binary Conversion Like the octal number system, the hexadecimal number system is used primarily as a “shorthand” method for representing binary numbers. It is a relatively simple matter to convert a hex number to binary. Each hex digit is converted to its 4-bit binary equivalent (Table 1.1). This is illustrated below for 9F2yg. QF2y= 9 F 2 1 + + 1001 1111-0010 = 100111110010, et alas cdeer DATA REPRESENTATION tneneees 19 Similarly, 3A6y5 = 3 A 6 Note eager | 4 t ‘ and octal codes are used, 0011 1010 110 as shorthand means of expressing large binary = 001110100110, numbers. : Consider another example SBFSCy= 3 B F 5 c + 4 L L L 0011 1011 1111 0101 1100 = 001110111111.01011100, Converting from Any Base to Any OTHER Base ‘As demonstrated in earlier examples and the table below, there is a direct correspondence between the number systems : with three binary digits corresponding to one octal digit ; four binary digits corresponding to one hexadecimal digit, which you can use to convert from one number system to another. Table 1.5 Comespondence of Binary, Octal and Table 1.4 Correspondence of binary and n octal number systems Hexadecimal number systems. BIN ocr DEC BIN nex | ocr | pec 000 0 0 0000 0 00 0 001 1 1 0001 1 o1 1 011 2 a 2 010 2 2 oe 0011 3 oa 3 on 3 3 0100 4 OF 4 val 2 ‘ 0101 5 05 3 oe e 5 ou0 6 06 6 110 6 6 om 7 7 7 peel 7 7 1000 8 10 8 | 1001 . nu 9 1010 A 12 ul For conversion from base 2to 0 base 8, we use groups of 101 B B n three bits and vice-versa. 1100 c iW o 1101 D 15 13 1110 E 16 4 ui F 7 15 For conversion from base 2 to base 16, we use groups of four bits and vice-versa.15 COMPUTER SCIENCE ge sq) 20 “ ing tne same, Let us consider some more exam system to another. . Example 1.14. Comoert 1948.B6y6 0 Binary ani Solution. a Hexadecimal 1 9 # es | eearond oe cons 30 ‘ cit binary from above table 15) (By converting each individual Hex digit to equivalent 4 digit 15) 1948.B6 yg = 0001 1100101001000.10110110 2 ove Binary equivalent hee 28 follows : ym abor les regard etal equivalent New Octal number can be generated £70 401 101 100 0 001 100 101 001 000 - Piso. Binary Tot Octal (By creating groups of 3 binary digits and co} 1948.B6 yg = 14510.554. averting them into equivalent octal no.) Example 1.15. Convert 75643.5704 5 to hexadecimal and binary numbers. Solution. We shall convert it in following way * oe al digit to 3 digit binary number, () From octal to binary — by representing each oct i yn (ii) From the complete binary number, we shall create groups of 4 binary digits a the decimal point. (iii) Convert each 4-digit-binary group to equivalent hex digit. Octal 7 5 6 a3 4 Binary 111 101 ‘110 100 O11 yo. 111 000 100 75643,5704. = 111101110100011.101111000100 » 1100 0100 Binary 0111 1011 1010 0011. 1011 a Hexadecimal 7 BA 3 Bc 4 75643,5704 = 7BA3.BC4 ys igital number systems, one must know how the data is ‘After learning about various dij represented in computers. Following sections illustrate the same. inary Representation of Numbers While working with numbers, you have mainly worked with integers (e.g., +17, -23 etc.) with real numbers (eg., +17.015, -23.214 etc.) ee Integers are whole numbers or fixed-point numbers with the radix point fixed after the Teast: significant bit. They are contrast to rea! numbers also called floating-point numbers, where position of the radix point varies. It is important to take note that integers and ‘loating;PolYr DATA REPRESENTATION ; 15.1 22 numbers are treated differently in computers, They have different representation and are processed dtforontly (@, floating-point numbers are processed in a so-called floating-point processor). In this section, wwe shall exptore how these two number types are represented in computers, Binary Representation of Integers ‘To represent an integer, computers use a fired muumber of bits. The commonly-used bit-tengths for integers are Sit, 16-bit, 32-bit oF 6L-bIF, Besides bit-lengths, there are two representation schemes far integers (Unsigned Integers can represent zerv and positive integers (a) Signed Integers can represent zery, positive and negative integers. 1.5.14 _ Representing Unsigned Integers 1.5.18 _Anunsigned integer can be either a positive integer or zero but never a negative integer. Before wwe discuss how unsigned numbers are represented, considera single digit decimal number: © Inasingle decimal digit, you can write a number between 0 and 9 (maximum number is 9). ® In two decimal digits, you can write a number between 0 and 99 (maximum number is 99). © In three decimal digits, you can write a number between 0 and 999 (maximum number is 999), and so on. Since 9 is equivalent to10! ~1 99 is equivalent to 10? - 1; 999 is equivalent to 10° Let, in n decimal digits, you can vorite a number between 0 and 10" = 1. Analogously, in the binary number tem, an unsigned integer containing n bits can have a value between 0 and 2° —1 (i.e., out of 2” different values). ‘ This fact is one of the most important and useful things to know about computers, An mbit pattern can represent 2" distinct integers, An mbit unsigned integer can represent integers from 0 to 2" 1, as tabulated below : Table 1.6 Ranges of Unsigned Integers represented through n-bits n (bits) | Minimum value Maximum Value 8 0 28-1(=255) 16 0 2-1 (= ) 32 0 294,967 295) (9+ digits) 64 0 2° 1 (518,446,744,073,709,551615) (19+ digits) Representing Signed Integers Signed integers can represent zero, positive integers, as well as negative integers. Three representation schemes are available for signed integers : (i) Sign-Magnitude representation (ii) 1's Complement representation (iii) 2's Complement representation In all the above three schemes, the mos! significant bit (MSB) is called the sign bit.22 COMPUTER SCIENCE-XI (ISC) ‘°T Sign-Magnitude Representation | ‘This is the conventional form for number representation. Integers are identified by their signs (+ or ~)and a string of digits which represent the magnitude. For example: +17 or 23 are positive integers (4 sign is frequently omitted) ~14, -18 are negative numbers To represent sign of a number (i.e, +or-), utmost bit (called as Most Significant Bit (MSB)) is used. If it holds value 0, the sign is + and if holds 1, the sign is ~. That is, in sign-magnitude representation : ® The most-significant bit (MSB) is the sign bit, with value of 0 representing positive integer and value 1 representing negative integer. ® The remaining 1-1 bits represent the magnitude (absolute value) of the integer. The absolute value of the integer is interpreted as “the magnitude of the (n~1}bit binary pattern”. For example, if in a computer, the word size is 1 byte (8 bits), then © +15 will be represented as follows : Number in binary notation ofo o | of 1 | 1 |} 1 | 1 | Binary equivatent of 15 is 1111 Most Least significant significant bit (LSB) bit (MSB) (0 for + sign} © ~ 23 will be represented as MsB (1 for sign) ‘Number in binary notation Example 1.16, Given that n=8 and the sign-magnitude binary representation is 01000001)>, determine the decimal integer represented. Solution. Sign bit is 0 => positive number (+ sign) 6 +2" = 64+1=65)19 Absolute value is 100 0001) Hence, the integer represented is +65)jo. Example 1.17. Given that n=8 and the sign-magnitude binary representation is 10000001), determine the decimal integer represented. ° Solution, Sign bit is 1 = negative number (- sign) = Vio Absolute value is 0000001), = 2” Hence, the integer is ~I)y.DATA REPRESENTATION 23 Example 1.18. Given that n=8 and the sign-magnitude binary representation is 0.000 0000), determine the decimal integer represented. Solution. Sign bit is 0 => positive number (4-sign) Absolute value is 0000000)» =0) 19 Hence, the integer is +0) Example 1.19. Given that n=8 and the sign-magnitude binary representation is 1.000 0000)», determine the decimal integer represented, Solution. Sign bit is 1 = negative number (- sign) Absolute value is 000 0000) = 0)19 Hence, the integer is -0) 9 Total numbers which can be represented by N bit word, using sign and magnitude representation, are 2" 1. In the above binary word of 8 bits, MSB is reserved for sign notation. Therefore, the maximum magnitude which can be represented is of 7 bits. Hence, maximum number which can be represented in 8 bits signed notation is 2” = 128. An 8 bits word can represent total 2° -1=255 numbers i.e, -127 to 0 and 0 to + 128. 16 bits (2 bytes) binary word can represent maximum 2" = 32768 numbers, one bit MSB is reserved for sign notation. And total (2'6 -1 = 65535) numbers it can represent are - 32767 to 0 and 0 to + 32768. Values represented Values represented increasebyt = Bi 1 + ze or +000 0000 0100 0000 (0000 0000 Binary values increase by 1 7 Figure 1.6 Sign-magnitude Representation. Drawbacks/Problems Asso ied with Sign-magnitude Representation As you must have noticed that this scheme allows you to write zero as two different numbers +0 or -0, which is misleading and hence a drawback. Major drawbacks of sign-magnitude representation are : () There are two representations 0000 0000),(+0) and 1000 0000) (-0) for the number zero, which could lead to inefficiency and confusion. (i) Positive and negative integers need to be processed separately.COMPUTER SCIENCE.x) (isc) zeseitation|| meri plement Represental a Nore aes Jement represents positive numbers by their binary To calculate 1) ae rents called true forms) and negative numbers by their 1’s Eaelement of a bina equival 7 number, just repla complements (called 1’s complement forms). every 0 with 1 and every For example 1's complement of binary number 1001 will be 0110 |with0. j and for 0011, it will be 1100. Therefore +6and~6 will be represented as 0110 (binary equivalent of 6) and 1001 (V's complement of 6) respective. Tofal numbers which can be represented using 1's complement are 2% —1 where N is the word size (number of bits in a word). Therefore, an 8 bit word can represent maximum 2 -1=255 numbers. 1’s complement can represent + 0(0000) and — 0(1111), but there is no significance of +00 or 0 therefore, only 0000 is used to represent 0 (zero), Example 1.20. Find the one’s complement form of ~ 13 Solution. * 13 = 00001101 -13=11110010 This system is called the one’s complement because the number can be subtracted from 1111 1111 to obtain the result, The answer will be the same as if all the 1’s were changed to 0's and all the 0's to 1’s. Example 1.21. Find the one's complement representation of - 13. 111 1111 0000 1101 1111 0010 Solution. Note that this is the same result as in example 1.14. Values represented Values represented increase by 1 increase by 1 ‘memmmnanectmsnsrontes [caneacnncmnemeseme HELL ULL Binary values increase by 1 Figure 1.7 1's Complement Representation, Drawbacks/Limitations of One’s Complement Notation The drawbacks/limitations of one’s complement representation scheme are : (i) There are two representations 0000 0000), and 1111 1111), for zero. (ii) The positive integers and negative integers need to be processed separately.DATA REPRESENTATION (ea eeneneneee (if two's| Complement Representation | Two's complement method represents positive numbers in their true forms ie, their binary equivalents and negative numbers in 2's complement form. For instance, number 6 (0110)'s 2’s complement will be calculated as follows : 1's complement of 0110 =1001 +1 ' lement of 0110 =—— 2’s complement o! ae (Binary addition rules are 0+0 25 }14+0=1;14+1=10;14+14+1=11) Therefore numbers 6 and ~ 6 will be represented as 0110 (binary equivalent of 6) and 1010 (2's complement of 6). Total numbers which a word of N bits can present, using 2's complement representation, are 2 . Therefore, a 4 bit word can represent 24 = 16 numbers. Example 1.22. Express ~4 in two's complement form. Solution. Positive expression of number 0000 0100 Complement 111 1011 Add 1 1111100 Therefore, ~4 equals 1111 1100. Example 1.23. Express — 17 in two's complement form. Solution. Positive expression of number 0001 0001 Complement 10 1110 Add 1 qm10 1111 Another method of converting a positive number to its two’s complement is as follows : Starting at the least significant bit and working from right to left, copy each bit down up to and including the first 1 bit encountered. Then complement the remaining bits, Example 1.24. Express -4 in two's complement form. Solution, Original number 0000 0100 Copy up to first 1 100 Complement remaining bits 111 1100 Compare this result with Example 1.22. Example 1.25. Express - 17 in two's complement form, Solution. Original number 0001 0001 Copy up to first 1 1 Complement remaining bits m0 111 Compare this result with Example 1.23.26 COMPUTER SCIENCE-XI (ISC) ; isti ider a four-bit Two's complement numbers have some very interesting Siete Consi word length. The decimal equivalents would be as shown in Table 1.7. Table 1.7 Two's Complement Numbers g lues represented Binary Decimal veut 8“ hneaseby | os ou +7 .. 1K ono +6 a oa oro *s S82: : 1: 222/888: :::: 222 ow “4 ggg. | gaz /gg¢ 323 mm | ss Be deep ea 0010 +2 => 001 +1 Rearrange 000 ° Values represented increase by 1 (from —128 to +127) ul -1 ® TE ED 1110 -2 B88: See ares BBs uo1 -3 he 1100 -4 588 eee 1011 5 388 322 1010 -6 ry bit) 1001 7 — 1000 -8 Figure 1.8 2's Complement Representation Looking at Table 1.7 and Fig. 1.8, you can observe the following : ( There is one'unique 0. (ii) The two's complement of 0 is 0. (ii) The leftmost bit cannot be used to express quantity. It is a sign bit such that if it is a ‘Y, the number is negative, and if it is a ‘0’, the number is positive, (iv) There are eight negative integers, seven positive integers, and one 0, making a total of 16 unique states. In the two's complement system, there will always be 2?! -1 Positive integers, 2°" negative integers, and one 0, for a total of 2” unique states, where P is the number of bits in the binary word. () Significant information is contained in the 1's of the Positive numbers and the 0's of the negative numbers. Think about that for a while, (Hint : Consider -2 and +2 in a 16-bit number.) (vi) To convert a negative number to a Positive number, you just need to find its two's complement. Generally, the negative numb: 3 n rs are stored in 2's complement form and positive numbers in sign and magnitude form,DATA REPRESENTATION - 27 Computers use 2's Complement Representation for Signed Integers Out of the three representations for signed integers : sign-magnitude, 1’s complement and 2’s complement, computers use 2's complement in representing signed integers. This is because : ( There is only one representation for the number zero in 2's complement, instead of two representations in sign-magnitude and 1’s complement. (i) Positive and negative integers can be treated together in addition and subtraction. Subtraction can be carried out using the “addition logic”. (This point will become clear to you in a later section where we shall discuss addition and subtraction with different representation schemes.) 16 Floating-Point Representation (For Real Numbers) Floating-point representation is used to represent real numbers, ie., numbers with fractions. A floating-point representation scheme allows you to store non-fractional and fractional parts separately. In this section, we shall explore different schemes for floating-point representation. 1.6.1 Fixed-Point Representation In fixed-point representation scheme, real numbers are represented in binary form and there are some bits reserved for the real and fractional part. For instance, 73). 625, Real pat“ Fractional part — |2000,0000 a000 0000 1001 90104 10100000 Real part” “Fractional part The fixed-point representation splits the bits of the representation between the places to the left of the decimal point and places to the right of the decimal point. For example, a 32-bit fixed-point representation might allocate 24 bits for the integer part and 8 bits for the fractional part. To represent 73.625, we would use the first 24 bits to store 73 in binary, and we'd use the remaining 8 bits to hold value 0.625 (in binary). b/}-—— 24 bits ——+ a bits +4 Integer part fraction 1+ Real part (24 bits) —4 some ereonn60 oroic5 | eo Thus, fixed-point 32-bit representation for number 73.625 would be as shown in the figure pan(oue) on the right. Ne: Fixed-point representation works reasonably well as long as you work Boerner pon epee with numbers within the supported range, For instance, the 32-bit lion approach has a fixed-point representation described above can represent any multiple of tation that very large | 1/256 from 0 up to 2" ~16.7 million. If your program uses numbers mbers or very small} within this range only, you can safely work with this scheme, but Sctlons cannot be |_ programs frequently need to work with numbers from a much broader sented, range. For this reason, fixed-point representation isn’t used very often in today’s computing world.28 1.6.2 1.6.3 COMPUTER SCIENCE-X! (Sq) Scientific Notation (Exponential Notation) Exponential Notation or Scientific notation isa conventional method for representing floating. point numbers. As you must be aware that scientific notation has the following configuration: 3505 = 3.505 x10? where 3is the non-fractional part and 0.505 is the fractional part ; 10 is the radix or base of the system and 2is the exponent to the radix. As this number is a Decimal number, the base is 10, Following figure (1.9) illustrates the components of the scientific notation in Decimal number system as well as in Binary Number System. This signifies that 1.08 a base 2 (binary) number Mantissa Exponent Mantissa Exponent ae NG a 6.02 x 10" 1.03 x 2 I i t I Decimal Radix “Binary Radix point (base) point” (base) (2) Decimal Number system (base 10) (b) Binary Number system (base 2) Figure 1.9 Components in Scientific notation Thus, you see that a floating-point number is typically expressed in the scientific notation, with a fraction (F) and an exponent (E) of a certain radix (r) in the form of Fxr®. Decimal numbers use radix of 10(F x 10*) ; while binary numbers use radix of 2( x 2%), You must have noticed that the point dividing fractional and non-fractional parts is called decimal point in decimal numbers and binary point in binary numbers. In general, you can call this point as radix point. Please remember here that the same number can be represented through various mantissa and exponents, e.g., number 350.56 10* is equivalent to all the following numbers : 35.056 10° 3.5056 x 104 3505.6 x 10" Similarly, binary number 101,001 x 2 is equivalent to all the following numbers : 101.1001 x 2° 1011001 x 2* 1.011001 x 2° Normalized Scientific Notation Representation of floating point number is not unique. For example, the number 35.66 can be represented as 5.566 x10", 0.5566 x 10", 0.05566x 10°, and so on. The fractional part can be normalized, however. In the normalized form, there is only a single non-zero digit before the radix point. For example, decimal number 123.4567 can be normalized as 1.234567 x 10? ; binary number 1010,1011 can be normalized as 1.010101 x 2°, Here carefully notice one thing that in binary number system, the normalized numbers will always have binary value 1 before the binary point — as per the condition that there should be only a single non-zero number before binary point, (As there is only single bit 1 that is non-zero in binary number system)29 ATA REPRESENTATION 1.64 Mantisso-Exponent Notation "Computers mostly represent real numbers in a form known as Mantissa and Exponent form, Similar to scientific notation, the value of a floating-point binary number is represented in the form : Mantissa x 2°" where mantissa isin normalised binary form. Let us understand it with the help of an example, suppose you have to represent decimal number 18.5 in Mantissa-Exponent Notation. step 1 Convert 18.5 into binary which is : 10010.1. step 2. Normalize this binary number (so that there is only one non-zero bit is on the left of binary-point.) For this we need to move the binary point to the left by four places, number is like ; now the > this 4s in decimal number 1.00101 x 24 Step 3 Represent everything in binary form. The power or exponent 4 is 100 in binary ie., is like : oe oo tere eas i em of Mantissa is the part of a og —? 0077 equivalent of 4) floating-point number which 1.00101 x 2 , represents the _ significant Step 4 So, we now have Mantissa as : digits of that number. 1.00101 Exponent represents the | power to base to be multiplied and exponent as 100 with mantissa to get the actual floati e Representation in Memory loating point number. IEEE standard way of representing floating-point numbers with mantissa and exponent scheme is as follows : A 32-bit representation has : ke bits Leftmost 1 bit as sign bit next 8 bits as exponent next 23 bits as Mantissa. Here you need to know about one important thing and which is that the bit before binary Point is not stored i.e., from the Mantissa computed above 1.00101, only the fractional part 00101 will be stored. The reason being since in the normalized form, the mantissa must have exactly one nonzero bit to the left of its binary point, and the only nonzero bit is 1, we know that the bit to the left of the binary point must be a 1. There's no point in wasting space in inserting this 1 into our bit pattern, so computer includes only the bits of the mantissa to the right of the binary point. So the number computed above : 1.00101 x2" will be represented in memory as ‘bit bits 23 bits Chappe ase PE PEP PPP PPP PPP EPPPPETE v Exponent Marissa (ad toma igh)tet COMPUTER SCIENCE. “30 a into decimal form + Let us convert above floating-point number into de Sign bit = 0 > positive number Exponent = 00000100), = 4)10 Mantissa = 00101 : red as it is alwa => it represents 1.00101 as bit 1 to the left of binary 1 not stor ysl, 1.00101 x 2* is the number = 10010-1 40+2=18 1 Non-fractional part = 10010 =1x 24 +1*2 Fractional Part 10010.1). = 18.5), Excess Notation ions, which Many computers also represent floating point number in Excess-notations, which helps representing numbers in a sorted order. The excess notation is ans of ropresetng oth negative and positive number iy manner in which the order of the bit patterns is maintained. Let us understand it with the ha of an example : Consider the 8 bit-patterns in 3 bits (in decreasing order) 111 110 101 100 O11 011 001 000 These bit patterns when interpreted as natural numbers represent FivigeG. 45 /ah 9 ha Dike 1 0, When interpreted as integers in 2’s complement represent : 1 2 3 -4 3 2 1 «0 Now in excess notation a fixed number called magic number is added to each number bef representing it in binary form. Say if the magic number is 4, this becomes Excess-4 notati and will represent numbers as follows : | Original number Excess 4 Binary notation 0 4(0+4) 100 1 5(1+4) 101 2 6(2+4) 110 3 7(3+4) m1 -1 3(-1+4) on 2 2(-2+4) 010 3 1(-3+4) 001 -4 O(-444) 000DATA REPRESENTATION 31 Compare it with earlier representations, we have g a g a z 5 000 0 0 -4 As you can see that excess notation is capable of representing N “ne SETS signed numbers in sorted order, hence excess notations are often ocean ed used by computers. also sometimes referred to Consider the following examples that represent numbers in | # Biased notation, Excess-7 notation. Example 1.26. Represent -96,9) in Excess-7 notation. Solution. (q) First we convert our desired number to binary : -1100000,.). (®) Then we convert this to binary scientific notation : —1.100000,.) x 2°. (0) Then we fit this into the bits. (i) We choose 1 for the sign bit since the number is negative. (ii) We add 7 to the exponent and place the result into the four exponent bits, For this example, we arrive at 6 +7 =13,49) =1101). (ii) The three mantissa bits are the first three bits following the leading Thus we end up with 1 1101 100. 00. Example 1.27. Decode the number 0 0101 100 represented in Excess-7 notation. Solution, (We observe that the number is positive, and the exponent bits represent 0101,a) = 5,10): (i) This is 7 more than the actual exponent, and so the actual exponent must be 5-7 Thus, in binary scientific notation, we have 1.100.) x2. ( (iv) We convert the binary into decimal ) We convert this to binary 1.100). x 2 = 0.011).—a 32 COMPUTER SCIENCE-XI (ISC) 1 Ropresentation Schamos 1.6.5. Single and Double Prec In computers, floating-point numbers are represented In scientific notation of fraction (F) and exponent (E) with a radi of 2, in the form of Fx 2", Both E and F can be positive as well as negative, Modem computers adopt TEBE 754 standard for representing floating-point number There are fay representation schemes : 32-bit single-precision and 64 © Single provision numbers include an 8-bil exponent field and a 23-bit fraction, for a total of 22 bits, ® Double pre of ob bi bit double-precision. ean L1-bit exponent field and a 52-bit fraction, for a total Note srantissa is also sometimes referred to as Single prveision seo mantissa significand. ( is + 3 ——+ ion numbers ha Figure 1.10 illustrates these two representation schemes. mantissa + 82 4 + 64 bits 4 Double precision Figure 1.10 Single and double-precision number representation Example 1.28. What is the decimal value of this single Precision float ? (Excess/Bias 127) SERRE eT Pleo foro fof ToTo oe [efoToTo Ts eToTs Solution. Sign = 1 is negative Exponent = (01111100), =124, Adjust bias by subtracting excess Significand = (1.0100 ... 0), =1 +27? = 1.25 (1. is implicit) Value in decimal = ~1.25x 2? = - 0.15625 24 - 12; Example 1.29. What is the decimal value of following binary number ? (Excess/Bias 127) Poe PPL) Solution, inplics Adjusing exces/bias Tele T Pole] o]o]o]o[o + Value in decimal = + (101001100 ... 0), «2'°"7 ‘ = (101001100 ... 0), «2? = (101001100 ... 0), = 10.375DATA REPRESENTATION 1.6.5A Trade-off between Siz of Mantlssa 17 1.7.1 ne nennennnnaeneeenennnnnenen 33 and Exponent As you know that the maxim y Num number represented by mantissa clepends upon the number of bits allotted for mantissa ; more the bits, more the accuracy (lt, precision), Its possible to improve the accuracy of a floating potnt number (la the range of numbers held can be increased) by increasing the number of bits devoted to the mantisa, But Increasing, the mantissa-size would reduce the exponent size, thereby reducing, the range of numbers thal can be represented, Hence, there will always be a trade-off between Preelsion/aceuracy and Range when storing real numbers using floating point notation, as there will always be a set number of bits allocated to storing real numbers with the Potential to increase or decrease the number of bits used for the mantissa against the number of bits used for the exponent. © The more digits assigned for precision and lower the range, ‘The higher the, © The mote digits assigned for the exponent the higher the |/Aumber-ol-blis for the mantissa, the higher the Nfeig pera ; mantissa, the higher the range and lower the precision, ipedalilon lan cores ave range and vice ver Binary Arithmetic After learning to represent numbers in various number s arithmetic. In the following lines, we shall be discu: arithmetic operations on binary numbers. ystems, let us now discuss binary ing various methods used to perform Binary Addition ALUs don’t directly work upon decimal numbers ; rather they proces binary numbers as a computer can understand only binary numbers, There are five basic cases for binary addition that must be understood before going on. These are : Case1: 0+0=0 ie, addition of two binary O's (zero) results into a binary 0 (zero). Case2: 0+1=1 ie, addition of a binary 0 (zero) and a binary 1 (one) results into binary 1 (one), Case: 140=1 i, addition of a binary 1 (one) and a binary 0 (zero) results into binary 1 (one). Case4: 141=10 ie,, Binary 1+ Binary 1 equals Binary 10. Or we can say that in binary numbers, one plus one equals zero (0), carry one (1). Case5: 1+1+1=11 ie, Binary 1 + Binary 1 + Binary 1 equals Binary 1, carty Binary 1. Let us summarize these rules 0+0=0 O+1=1 14021 1+1= 10 [0 with carry 1] 14141511 [1 with carry 1]>. | 34 Computer SCN “KI i Let us now perform binary addition on bigger binary numbers. The binary num “0 added column-by-column just ike decimal numbers. Also, the way results larger ay decimal digit are carred-over, in binary addition, results larger than 1 are algy =" For example, Miedo Decimal Addition ‘This is carried over as 5+7 =| 2which is more than 9, 4 hence carried over. 15 27 42 Similarly, Binary Addition will be 1111 (Equivalent of 1539) 11011 Equivalent of 2719) 101010 Equivalent of 42:9) Let us perform another addition, step-by-step for 11100 +11010 Start with the least significant column ie,, the right most column to get 11100 +11010 0 In all examples, 0 +0 gives 0. Next, add the bits of the second column (second from right) as follows : 11100 +11010 10 This time, 0 +1 gives The addition in third column gives 11100 #11010 110 In this case, 1+0 results in 11100 +11010 (carry 1) 0110 uATA REPRESE 17.2 NTATION teeeeeneeneee 35 [As you see, 1+1 produces 10 ie,, 0 with a carry of 1. inally, the last column gives Finally, ; ; 11100 +11010 110110 Here, 1+1+1 (previously generated carry) results in 11, recorded as 1 with a carry to the next higher column. Example 1.30. Add the binary numbers 01010111 and 00110101. Solution. If you add the bits column by column as earlier explained you will get 1440144 ¢———— Carries 01010111 +00110101 10001100 Example 1.31. Add the binary numbers 1011 and 110. Solution. 1 1011 + 110 10001 +. carries Example 1.32. Add binary numbers 11110 and 11. Solution. 111 11110 ll 100001 +. caries Example 1.33. Add binary numbers 11.01 and 101.11. Solution. 1444 11.01 101.11 1001.00 + carries Binary Subtraction Binary subtraction is performed in similar manner as that of decimal subtraction, Because the binary number system has only two digits, binary subtraction requires more borrowing operations than decimal number system. To subtract binary numbers, we need to discuss four cases : Case 1: 0-0=0 Case 2: 1-0=1 Case 3: 1-1=0 Case 4: 1 0-1=1 ie, 0-1=1 with a borrow of 1.36 COMPUTER SCIENCE-XI (ISq) ‘The last result is representation of 0 -ts1 conse (because +1 * 10). one needs to subtraet column by column, borrowing from . Por example, in subtracting 101 from 111, ong which mak To subtract larger binary number the next higher column whenev: should proceed like this : wt -101 010 Starting on the right, 1 -1 gives 0 ; then 1- (is 1 ; finally, 1-1is 0. Notice that nothing is borrowed in this example. But in the next example, you'll see “borrow” taking place, Example 1.34. Subtract 1 (one) from 1003. Borrows a 490 1 11 Solution. Since, in the last most column, a 1 cannot be subtracted from a 0, a1 must be borrowed from the next (j., 2" column from right) column. But it is also 0, so a 1 must be borrowed from the next (j., 3% from right) column, making 3" column 0 and the 2" column 10. A 1 can now be borrowed from the 2“ column, making it a 1 and the first column 10. 10-1 =1 in the Ist column 1-0=1 in the 2nd column 0-0=0in the 3rd column Therefore, 100) -1) =113. Example 1.35, Subtract 101 from 1001. Both of these numbers are binary numbers. Solution. 1 y001 Borrons Ataf 100 Example 1,36, Subtract 11, from 100003. Example 1.37, Subtract 1003 front 110.015. Solution, ew a Solution. toma +¥9000 110.01 11 -100.1 1101 1DpaTA REPRESENTATION 7 neeen 37 jssues with. Addition/Subtraction in Sign and Magnitude System 17.3 Recall that Sign & Magnitude representation system represents both positive and negative integers using just I's and 0's - by using, the most significant bit (msb), x1y_1, to designate the sign and the remaining (N ~1) bits to designate the magnitude (for N-bit capacity ). ‘That is: Xyer Nyce XpXg and the usual convention is sign magnitude xy1=0 > — Xis positive Xy1=l > — Xis negative +6 > 110 5 > 101 While the Sign and magnitude format is intrinsically simple, it leads to significant complications if the normal rules of arithmetic are applied. If you use this system for addition or subtraction, this might give you correct or incorrect results. It gives correct result if both numbers are of the same sign and the result very well fits in N ~1 bits. In other cases, it gives incorrect results, Have a look at examples below that highlight this problem. Examples 1.38. Add numbers +3 and +2 using sign and magnitude representation. Solution. (#3) 0011 add (+2) 0010 0101 > (+5) iscor ct Examples 1.39. Add numbers +3 and ~2 using sign and magnitude representation. Solution. (43) 0011 Note 5 | ecause of the add = (-2) 1010 ; problems arising with 1101 > (-3) is incorrect addition and subtraction, the _ sign and magnitude system is _ Because of the problems arising with addition and subtraction, | not generally used where | the sign and magnitude system is not generally used where arithmetic functions are’ arithmetic functions are required; it may, however, be used for |] teduired; it may, however, be | data storage, used for data storage, Binary Addition and Subtraction using One's Complement Notation You already have learnt about 1’s complement and 2's complement notations. Though 2's complement arithmetic is used by most computers, yet some use 1’s complement also. This Section explains to you the binary operations in 1's complement arith: As you know that a negative number can be represented in 1’s complement by changing all 0's {01'sand vice-versa ifthe positive binary number eg., + 19is represented as 0000 1101 and ~13 as 11110010, which is 1's complement of +13,38 ewenenne COMPUTER SCIENCE-XI (IS) ather binary number, say 4, then it may Wa binary number, say bis to be subtracted from ano it becomes a spectal case of addition, also be interpreted as at (=6) in place of = hey wherein, complement is added, Ww Ls complement arithmeti¢, the Qvo numbers ate various eases possible in I's complement arithmetic, added including the sign bits. Let us learn, sample, Case L Positive number added to another positive number, Fo 14 Sion btm g, A +7 +6 0,0110 13, 01101 This type of addition is straightionward. Case 2 Addition of a Positive and Negative number. Case 2.1 Positive number with a greater magnitude added to a negative number with lesser magnitude, For example, Sign bit 4 > o,0111 1.1100 1's complement of 3 representing ~ 3 10,0011 L__,1 Add overtiow bit to LSB 0,0100 — Result See an overtlow is generated, notice an extra bit beyond sign bit. This overflow bit is added to the least significant bit of the sum to get the correct result, This process is called end around carry. Case 2.2 Negative number has greater magnitude than positive number, 2. Sign bit -7 ~+1,1000 +s complement of 7 representing ~ 7 +3 0,0011 1,101 (tis is 1's complement of 4, depicting -4) -4 Notice that no overflow is generated. This answer is correct as the sign bit is 1 and the result is in 1’s complement form, Case 3 Addition of two negative numbers, &8- 1 1,1000 -7 1,1100 3 11,0100 10 Li 71,0101 (1's complement representing ~10)Fr sane 39 ATA REPRESENTATION Notice that an overflow is generated, this overflow is added to the least significant bit. If the sign bit is 1 depicting the negative result, then the result is correct otherwise incorrect e.g., -9 10,1101 -17 Lys 01110 (Incorrect sign bit, thus incorrect result) Insuch case bigger unit should be used for addition. For instance, in the previous case the unit used is 4 bit number which can represent a number with magnitude upto 15. Bigger numbers will definitely cause incorrectness. Now let us do the same example with 8 bits. 44444 Sie errEh 11110110 11,1101101 a 1,1101110 (1's complement representing - 17) [Notice the result is 1’s complement of 0,0010001, which is binary equivalent of +17] 1.7.4 Binary Addition and Subtraction using 2’s complement Notation You have leamt that to represent a negative number, its 2’s complement of a number is calculated by adding 1 to the least significant bit of its 1’s complement. Let us now discuss the addition and subtraction mechanisms using 2's complement notation. Case 1 Two positive numbers are added. This situation is identical to the same case in 1's complement notation 28. +5 S90 P > or 01 +3 0,0011 8 0,1000 Case 2 Addition of a positive and negative number (je, subtraction of a number from another number), (se 2.1 Positive Number has greater magnitude. Sign bit 47 a7 oad =3 11101 4 10,0100 The result is 0,0100 which is correc. Ignore 7» 2.0100 this overflowCOMPUTER SCIENCE-X! (ISC) ignore the overflow The rule in this case is that after adding the two numbers, lov the sign bit is if any. The result should be positive for the above example, zero, which is correct. Case 2.2 Negative number has greater magnitude. -7 Sin bt—+ 11001 +3 0,0011 an 7.1100 (2's complement representing ~ 4) ‘The result is in 2's complement form. The sign bit 1 is depicting it is negative number. Case 3. Addition of two negative numbers. -7 1,1001 oS 1,1101 1 ATID eet ping” 1 The result generated above shows sign bit as 1, which represents negative result and it is correct. However, if the result generated is outside the allowed range of numbers, the sign bit will become incorrect, which indicates an error condition, e.g., Sign bit -3 "+ 1,1000 -9 1,0111 -7 10,1111 — Error Sign bit should be 1 indicating negative, but it is 0 (zero) here and hence the result is incorrect. If the same addition is performed with 8 bits (1 bit reserved for sign bit) it’ll show the correct result eg., - 8 1,111 1000 -9 4,111 0111 -17 11,110 1111 The resuttis 1,110 1111 which is Ignore 2's complement representing ~ 17. Some of the previous results were carefully chosen to the extent that the numbers all fell within the valid range for 2’s complement representation with N= 4. What happens if the arithmetic result exceeds the valid range ? An incorrect result is produced and the arithmetic system should have the capability of flagging an overflow. Consider the following t1v0 cases : (46) 0110 (6 1010 (7) 0111 (7) 1001 The result contains an oxtra bit than capacity - OVERFLOW
You might also like
The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
From Everand
The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
Mark Manson
4/5 (6129)
Principles: Life and Work
From Everand
Principles: Life and Work
Ray Dalio
4/5 (627)
The Gifts of Imperfection: Let Go of Who You Think You're Supposed to Be and Embrace Who You Are
From Everand
The Gifts of Imperfection: Let Go of Who You Think You're Supposed to Be and Embrace Who You Are
Brene Brown
4/5 (1148)
Never Split the Difference: Negotiating As If Your Life Depended On It
From Everand
Never Split the Difference: Negotiating As If Your Life Depended On It
Chris Voss
4.5/5 (935)
The Glass Castle: A Memoir
From Everand
The Glass Castle: A Memoir
Jeannette Walls
4/5 (8215)
Grit: The Power of Passion and Perseverance
From Everand
Grit: The Power of Passion and Perseverance
Angela Duckworth
4/5 (631)
Sing, Unburied, Sing: A Novel
From Everand
Sing, Unburied, Sing: A Novel
Jesmyn Ward
4/5 (1253)
The Perks of Being a Wallflower
From Everand
The Perks of Being a Wallflower
Stephen Chbosky
4/5 (8365)
Shoe Dog: A Memoir by the Creator of Nike
From Everand
Shoe Dog: A Memoir by the Creator of Nike
Phil Knight
4.5/5 (860)
Her Body and Other Parties: Stories
From Everand
Her Body and Other Parties: Stories
Carmen Maria Machado
4/5 (877)
The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
From Everand
The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
Ben Horowitz
4.5/5 (361)
Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
From Everand
Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
Margot Lee Shetterly
4/5 (954)
Steve Jobs
From Everand
Steve Jobs
Walter Isaacson
4/5 (2923)
Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
From Everand
Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
Ashlee Vance
4.5/5 (484)
The Emperor of All Maladies: A Biography of Cancer
From Everand
The Emperor of All Maladies: A Biography of Cancer
Siddhartha Mukherjee
4.5/5 (277)
A Man Called Ove: A Novel
From Everand
A Man Called Ove: A Novel
Fredrik Backman
4.5/5 (4972)
Angela's Ashes: A Memoir
From Everand
Angela's Ashes: A Memoir
Frank McCourt
4.5/5 (444)
Brooklyn: A Novel
From Everand
Brooklyn: A Novel
Colm Toibin
3.5/5 (2061)
The Art of Racing in the Rain: A Novel
From Everand
The Art of Racing in the Rain: A Novel
Garth Stein
4/5 (4281)
The Yellow House: A Memoir (2019 National Book Award Winner)
From Everand
The Yellow House: A Memoir (2019 National Book Award Winner)
Sarah M. Broom
4/5 (100)
The Little Book of Hygge: Danish Secrets to Happy Living
From Everand
The Little Book of Hygge: Danish Secrets to Happy Living
Meik Wiking
3.5/5 (447)
Yes Please
From Everand
Yes Please
Amy Poehler
4/5 (1987)
The World Is Flat 3.0: A Brief History of the Twenty-first Century
From Everand
The World Is Flat 3.0: A Brief History of the Twenty-first Century
Thomas L. Friedman
3.5/5 (2283)
Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
From Everand
Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
Gilbert King
4.5/5 (278)
Bad Feminist: Essays
From Everand
Bad Feminist: Essays
Roxane Gay
4/5 (1068)
The Outsider: A Novel
From Everand
The Outsider: A Novel
Stephen King
4/5 (1993)
The Woman in Cabin 10
From Everand
The Woman in Cabin 10
Ruth Ware
3.5/5 (2641)
A Tree Grows in Brooklyn
From Everand
A Tree Grows in Brooklyn
Betty Smith
4.5/5 (1936)
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
From Everand
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
Viet Thanh Nguyen
4.5/5 (125)
A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
From Everand
A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
Dave Eggers
3.5/5 (692)
Team of Rivals: The Political Genius of Abraham Lincoln
From Everand
Team of Rivals: The Political Genius of Abraham Lincoln
Doris Kearns Goodwin
4.5/5 (1912)
Wolf Hall: A Novel
From Everand
Wolf Hall: A Novel
Hilary Mantel
4/5 (4074)
On Fire: The (Burning) Case for a Green New Deal
From Everand
On Fire: The (Burning) Case for a Green New Deal
Naomi Klein
4/5 (75)
Fear: Trump in the White House
From Everand
Fear: Trump in the White House
Bob Woodward
3.5/5 (830)
Manhattan Beach: A Novel
From Everand
Manhattan Beach: A Novel
Jennifer Egan
3.5/5 (901)
Rise of ISIS: A Threat We Can't Ignore
From Everand
Rise of ISIS: A Threat We Can't Ignore
Jay Sekulow
3.5/5 (143)
John Adams
From Everand
John Adams
David McCullough
4.5/5 (2544)
The Light Between Oceans: A Novel
From Everand
The Light Between Oceans: A Novel
M L Stedman
4.5/5 (790)
The Unwinding: An Inner History of the New America
From Everand
The Unwinding: An Inner History of the New America
George Packer
4/5 (45)
Little Women
From Everand
Little Women
Louisa May Alcott
4/5 (105)
The Constant Gardener: A Novel
From Everand
The Constant Gardener: A Novel
John le Carré
3.5/5 (109)