DAT10403 CHAPTER 4 COMPUTER ARITHMETIC By Noordiana Kassim
NUMBER SYSTEMS
NUMBER SYSTEMS A number system defines how a number can be represented using distinct symbols. A number can be represented differently in different systems. For example, the two numbers (2A)16 and (52)8 both refer to the same quantity, (42)10, but their representations are different.
HOW COMPUTER STORES INFORMATION???
Binary Numbers are at the heart of how a computer stores all information Computers Store ALL information using Binary Numbers Computers use binary numbers in different ways to store different types of information. Common types of information that are stored by computers are : Whole numbers (i.e. Integers). Examples: 8 97 -732 0 -5 etc Numbers with decimal points. Examples: 3.5 -1.234 0.765 999.001 etc Textual information (including letters, symbols and digits) Keep reading …
All information on computers is stored as numbers All information that is processed by computers is converted in one way or another into a sequence of numbers. This includes numeric information textual information and Pictures Therefore, if we can derive a way to store and retrieve numbers electronically this method can be used by computers to store and retrieve any type of information.
CASE STUDY EXAMPLE Bitmap images are widely used on digital cameras, smartphones and online. Common bitmap image file types include JPEG, GIF and PNG. Bitmaps are also known as pixel maps or raster graphics.
CASE STUDY EXAMPLE (cont…) Bitmap images are organized as a grid of colored squares called pixels (short for 'picture elements'). When zooming in or enlarging a bitmap image, the pixels are stretched and made into larger blocks. This is why bitmap images appear as poor quality when enlarged too much.
CASE STUDY EXAMPLE (cont…) Each color of an image is stored as a binary number. In the black-and-white image below, each pixel is either black or white. You need a binary value for each different color. As each pixel is either black or white, this image can be encoded with a value of 0 for white and 1 for black.
Common Number Systems System Base Symbols Used by humans? Used in computers? Decimal 10 0, 1, … 9 Yes No Binary 2 0, 1 Octal 8 0, 1, … 7 Hexa- decimal 16 0, 1, … 9, A, B, … F
What is a binary number? A binary number is a number that includes only ones and zeroes. The number could be of any length The following are all examples of binary numbers 0 10101 1 0101010 10 1011110101 01 0110101110 111000 000111 Another name for binary is base-2 (pronounced "base two")
What is a decimal number? The numbers that we are used to seeing are called decimal numbers. decimal numbers consist of the digits from 0 (zero) through 9. The following are examples of decimal numbers 3 76 15 32423234 890 53 Another name for decimal numbers are base-10 (pronounced "base ten") numbers.
Quantities/Counting (1 of 3) Decimal Binary Octal Hexa- decimal 1 2 10 3 11 4 100 5 101 6 110 7 111
Quantities/Counting (2 of 3) Decimal Binary Octal Hexa- decimal 8 1000 10 9 1001 11 1010 12 A 1011 13 B 1100 14 C 1101 15 D 1110 16 E 1111 17 F
Quantities/Counting (3 of 3) Decimal Binary Octal Hexa- decimal 16 10000 20 10 17 10001 21 11 18 10010 22 12 19 10011 23 13 10100 24 14 10101 25 15 10110 26 10111 27 Etc.
Conversion Among Bases The possibilities: Decimal Octal Binary Hexadecimal
Quick Example 2510 = 110012 = 318 = 1916 Base
Weight 12510 => 5 x 100 = 5 2 x 101 = 20 1 x 102 = 100 125 Base
Binary to Decimal Decimal Octal Binary Hexadecimal
Binary to Decimal Technique Multiply each bit by 2n, where n is the “weight” of the bit The weight is the position of the bit, starting from 0 on the right Add the results
BINARY TO DECIMAL Example Bit “0” 1010112 => 1 x 20 = 1 1 x 21 = 2 0 x 22 = 0 1 x 23 = 8 0 x 24 = 0 1 x 25 = 32 4310
Hexadecimal to Decimal Octal Binary Hexadecimal
Hexadecimal to Decimal Technique Multiply each bit by 16n, where n is the “weight” of the bit The weight is the position of the bit, starting from 0 on the right Add the results
HEXADECIMAL TO DECIMAL Example ABC16 => C x 160 = 12 x 1 = 12 B x 161 = 11 x 16 = 176 A x 162 = 10 x 256 = 2560 274810
Decimal to Binary Decimal Octal Binary Hexadecimal
Decimal to Binary Technique Divide by two, keep track of the remainder First remainder is bit 0 (LSB, least-significant bit) Second remainder is bit 1 Etc.
DECIMAL TO BINARY Example 12510 = ?2 2 125 62 1 2 31 0 2 15 1 2 7 1 2 3 1 2 1 1 2 0 1 12510 = 11111012
Hexadecimal to Binary Decimal Octal Binary Hexadecimal
Hexadecimal to Binary Technique Convert each hexadecimal digit to a 4-bit equivalent binary representation
HEXADECIMAL TO BINARY Example 10AF16 = ?2 1 0 A F 0001 0000 1010 1111 10AF16 = 00010000101011112
Decimal to Hexadecimal Octal Binary Hexadecimal
Decimal to Hexadecimal Technique Divide by 16 Keep track of the remainder
DECIMAL TO HEXADECIMAL Example 123410 = ?16 123410 = 4D216 16 1234 77 2 16 4 13 = D 0 4
Binary to Hexadecimal Decimal Octal Binary Hexadecimal
Binary to Hexadecimal Technique Group bits in fours, starting on right Convert to hexadecimal digits
BINARY TO HEXADECIMAL Example 10101110112 = ?16 10 1011 1011 B B 10101110112 = 2BB16
Exercise – Convert ... Decimal Binary Octal Hexa- decimal 33 1110101 703 1AF Don’t use a calculator! Skip answer Answer
Exercise – Convert … Decimal Binary Octal Hexa- decimal 33 100001 41 Answer Decimal Binary Octal Hexa- decimal 33 100001 41 21 117 1110101 165 75 451 111000011 703 1C3 431 110101111 657 1AF
Common Powers (1 of 2) Base 10 Power Preface Symbol pico p nano n 10-12 pico p 10-9 nano n 10-6 micro 10-3 milli m 103 kilo k 106 mega M 109 giga G 1012 tera T Value .000000000001 .000000001 .000001 .001 1000 1000000 1000000000 1000000000000
Common Powers (2 of 2) Base 2 What is the value of “k”, “M”, and “G”? Preface Symbol 210 kilo k 220 mega M 230 Giga G Value 1024 1048576 1073741824 What is the value of “k”, “M”, and “G”? In computing, particularly w.r.t. memory, the base-2 interpretation generally applies
Example In the lab… 1. Double click on My Computer 2. Right click on C: 3. Click on Properties / 230 =
Fractions Decimal to decimal (just for fun) 3.14 => 4 x 10-2 = 0.04 1 x 10-1 = 0.1 3 x 100 = 3 3.14
Fractions Binary to decimal 10.1011 => 1 x 2-4 = 0.0625 1 x 2-3 = 0.125 0 x 2-2 = 0.0 1 x 2-1 = 0.5 0 x 20 = 0.0 1 x 21 = 2.0 2.6875
Fractions 0.2510 = 0.012 (exactly)
Fractions 0.8110 = 0.1100112 (approximately)
Fractions Decimal to binary .14579 x 2 0.29158 x 2 0.58316 x 2 1.16632 x 2 0.33264 x 2 0.66528 x 2 1.33056 etc. 3.14579 11.001001...
Exercise – Convert ... Decimal Binary Octal Hexa- decimal 29.8 101.1101 3.07 C.82 Don’t use a calculator! Skip answer Answer
Exercise – Convert … Decimal Binary Octal Hexa- decimal 29.8 Answer Decimal Binary Octal Hexa- decimal 29.8 11101.110011… 35.63… 1D.CC… 5.8125 101.1101 5.64 5.D 3.109375 11.000111 3.07 3.1C 12.5078125 1100.10000010 14.404 C.82
ARITHMETIC LOGIC UNIT (ALU)
ARITHMETIC & LOGIC UNIT Does the calculations Everything else in the computer is there to service this unit Handles integers May handle floating point (real) numbers May be separate FPU (Floating-Point Unit : maths co-processor) May be on chip separate FPU (486DX +)
ALU INPUTS AND OUTPUTS Operand Result
INTEGER REPRESENTATION
Number representation Representing whole numbers Representing fractional numbers
Integer Representations Signed magnitude notion Two’s complement notation.
Representation of negative numbers Is a representation of negative numbers possible? Unfortunately: you can not just stick a negative sign in front of a binary number. (it does not work like that) There are three methods used to represent negative numbers. Signed magnitude notation Excess notation notation Two’s complement notation
Signed Magnitude Representation Unsigned: - and + are the same. In signed magnitude the left-most bit represents the sign of the integer. 0 for positive numbers. 1 for negative numbers. The remaining bits represent to magnitude of the numbers.
Example - Suppose 10011101 is a signed magnitude representation. The sign bit is 1, then the number represented is negative The magnitude is 0011101 with a value 24+23+22+20= 29 Then the number represented by 10011101 is –29. position 7 6 5 4 3 2 1 Bit pattern contribution - 24 23 22 20
Exercise 1 3710 has 0010 0101 in signed magnitude notation. Find the signed magnitude of –3710 ? Using the signed magnitude notation find the 8-bit binary representation of the decimal value 2410 and -2410. Find the signed magnitude of –63 using 8-bit binary sequence?
Disadvantage of Signed Magnitude Addition and subtractions are difficult. Signs and magnitude, both have to carry out the required operation. They are two representations of 0 00000000 = + 010 10000000 = - 010 To test if a number is 0 or not, the CPU will need to see whether it is 00000000 or 10000000. 0 is always performed in programs. Therefore, having two representations of 0 is inconvenient.
Signed-Summary In signed magnitude notation, Advantages: The most significant bit is used to represent the sign. 1 represents negative numbers 0 represents positive numbers. The unsigned value of the remaining bits represent The magnitude. Advantages: Represents positive and negative numbers Disadvantages: two representations of zero, Arithmetic operations are difficult.
Two’s Complement Notation The most used representation for integers. All positive numbers begin with 0. All negative numbers begin with 1. One representation of zero i.e. 0 is represented as 0000 using 4-bit binary sequence.
Two’s Complement Notation In twos complement notation, the negation of an integer can be formed with the following rules: Take the Boolean complement of each bit of the integer (including the sign bit). That is, set each 1 to 0 and each 0 to 1. Treating the result as an unsigned binary integer, add 1. These two-step process is referred to as the twos complement operation.
Two’s Complement Notation +18 = 00010010 (twos complement) bitwise complement = 11101101 + 1 ___________ 11101110 = -18
Alternative Representation for 4-Bit Integers Decimal Representation Sign-Magnitude Representation Twos Complement Representation +7 0111 +6 0110 +5 0101 +4 0100 +3 0011 +2 0010 +1 0001 +0 0000 -1 1001 1111 -2 1010 1110 -3 1011 1101 -4 1100 -5 -6 -7
DIGITAL LOGIC
BOOLEAN ALGEBRA Mathematical foundation of digital logic. In this case, it makes use of logical variables and operations. Variable may take the value :- 1 (TRUE) 0 (FALSE) Basic logical operations are :- AND ( . dot symbol) OR ( + plus symbol) NOT ( ¯ overbar or ‘ apostrophe symbol)
BOOLEAN ALGEBRA Examples : A AND B = A · B A OR B = A + B NOT A = Ā Other useful operators : XOR (exclusive OR) two logical operands is 1, if and only if exactly one of the operands has the value 1. NAND (not AND) NOR (not OR)
BOOLEAN ALGEBRA Boolean Operators of Two (2) Input Variables 1 P Q NOT P (P) P AND Q (P · Q) P OR Q (P + Q) P NAND Q P NOR Q (P+Q) P XOR Q (P O Q) 1 +
GATES Gate is an electronic circuit that produces an output signal that is a simple Boolean operation on its input signal. A device that performs a basic operation on electrical signals Fundamental building block of all digital logic circuit. Logical functions are implemented by the interconnection of gates. Typically, logic diagrams are black and white, and the gates are distinguished only by their shape
GATES Basic gates used in digital logic are : AND OR NOT NAND NOR XOR Each gate is defined in three (3) ways: Graphical symbol or logic diagram Algebraic notation/function or Boolean Expression Truth table A table showing all possible input value and the associated output values)
BASIC LOGIC GATES
BINARY SYSTEM Computers are made of a series of switches Each switch has two states: ON or OFF Each state can be represented by a number – 1 for “ON” and 0 for “OFF”
Switching Circuits AND OR
NOT Gate A NOT gate accepts one input value and produces one output value
NOT Gate By definition, if the input value for a NOT gate is 0, the output value is 1, and if the input value is 1, the output is 0 A NOT gate is sometimes referred to as an inverter because it inverts the input value
AND Gate An AND gate accepts two input signals If the two input values for an AND gate are both 1, the output is 1; otherwise, the output is 0
OR Gate If the two input values are both 0, the output value is 0; otherwise, the output is 1
XOR Gate XOR, or exclusive OR, gate An XOR gate produces 0 if its two inputs are the same, and a 1 otherwise Note the difference between the XOR gate and the OR gate; they differ only in one input situation When both input signals are 1, the OR gate produces a 1 and the XOR produces a 0
XOR Gate
NAND and NOR Gates The NAND and NOR gates are essentially the opposite of the AND and OR gates, respectively 4–15
Review of Gate Processing A NOT gate inverts its single input value An AND gate produces 1 if both input values are 1 An OR gate produces 1 if one or the other or both input values are 1
Review of Gate Processing An XOR gate produces 1 if one or the other (but not both) input values are 1 A NAND gate produces the opposite results of an AND gate A NOR gate produces the opposite results of an OR gate
Converting between circuits and equations Find the boolean expression/output of the following circuit Answer: (x+y) y x+y (x+y)y y __
Converting between circuits and equations Find the output/algebraic function of the following circuit Answer: x x y x y y ___ _ _
EXERCISE Find the value (output) at point Q for the following logic gates circuit if A = 0 and B = 1
EXERCISE Find the value (output) at point Q for the following logic gates circuit if A = 0 and B = 1
EXERCISE Construct/Produce a truth table based on the following logic gates circuit
ANSWER
EXERCISE Construct/Produce a truth table based on the following logic gates circuit
ANSWER
EXERCISE