Download presentation
Presentation is loading. Please wait.
1
Courtesy of Dr Radwan E Abdel-Aal
COE 202: Digital Logic Design Courtesy of Dr Radwan E Abdel-Aal Unit 1 Number Systems & Codes
2
Unit 1: Number Systems and Codes Contents
Introduction: Information representation & processing and 1.1 Number Systems: Binary, Octal and Hexadecimal Number Base Conversion: Dec, Bin, Oct, and Hex Digital Codes for Numbers and Symbols , 1.5, 1.6 Numbers: Binary, BCD, Excess-3, Gray, Parity , 1.5 Alphanumeric (symbols): ASCII, Unicode Arithmetic on Unsigned Numbers: Basic Operations in binary: Add, Subtract, Multiply Arithmetic in Octal and Hexadecimal, BCD addition Binary Signed Number Representation and Arithmetic: Subtraction, complement notation Signed representation, Addition/Subtraction, Overflow
3
We live in a predominantly Analog world
1. Introduction to Digital Systems Continuous Vs Discrete (Analog Vs Digital) We live in a predominantly Analog world Analog means Continuous (both in time and amplitude) Analog information exhibit smooth, gradual changes over time and assume a continuous (infinite) range of amplitudes Examples: Earth’s movement Body temperature Our speech Analog Signal
4
Digital Information Digital Discrete, Not continuous
Digital information assume a limited (finite) set of “Discrete” values, not a continuous range of values Values change abruptly (not smoothly) by “Jumping” between values Examples: The Alphabet Position of a switch DNA sequences (TAGC) Energy levels of electrons in atoms Digital Signal Only 4 allowed Signal levels If we have only two signal levels Binary signal … So binary is a special case of digital
5
Analog Vs Digital It is a lot easier to design digital systems than analog ones, Why? Much simpler to deal with a limited set of values as inputs and outputs for the circuits Greater tolerance to drift, noise low error rates Digital circuits are more available, simpler and cheaper (VLSI) It is also more advantageous to store and communicate information digitally Dilemma here: Our natural world is mainly analog… but it is easier to process it digitally! Also
6
Digital Processing in the Real World
Analog-to-digital-converters (ADC) are used to digitize raw analog inputs. Digital-to-analog-converters (DAC) are used to regenerate analog signals from their digitized form
7
Digitization of Analog Signals
Nowadays most processing, computing, and communication are done digitally Solution: Convert analog signals to digital parameters and process them digitally This requires two steps: 1. Sampling in time (impossible to handle the number of values existing on the time axis!). Ignore signal between samples 2. Quantization in amplitude (impossible to handle the number of values existing on the amplitude axis!). Approximate sample value to the nearest quantization level 2. Quantization to discrete levels in amplitude Ignore Ignore 1. Sampling at discrete points in time
8
Amplitude Quantization: 4 discrete levels
Example: Amplitude Quantization: 4 discrete levels Using a larger number of discrete levels We can reduce the quantization errors (noise) we introduced! Quantization Errors Analog Signal levels are mapped to the nearest value among the set of discrete voltages {V1, V2, V3, V4} allowed for the digital signal
9
Information Representation How to represent 10 discrete levels (0 to 9)?
Assume a 0 to 5 V range to represent the discrete quantization levels Direct 10-level Representation Using Binary (2-level) Representation Our circuits deal with: Ten Signal levels Two Signal levels (ON/OFF) Simpler, reliable Circuits Noise Margin: (5/9)/2 V (5/1)/2 = 2.5 V Larger (better) Number of steps 1 variable takes 1 of 10 values Use n variables, each takes 1 of 2 values {0,1} n binary digits (bits) e.g. with n = 4 bits 6 is represented as 0110 Noise Margin
10
Binary Values: Abstract and Physical Representations
Abstract Representations Digit Values: 0, 1 Logic Levels: True, False Signal Levels: Low, High States: ON, OFF Physical Representations In an IC (e.g. a microprocessor): Voltage or Current On a Hard Disk: Magnetic Field Direction On a CD: Surface pits/Light interference In a Dynamic RAM: Electrical Charge
11
Signal Voltage Levels: Noise Considerations
1 Logic Level Min O/P representing 1 Max allowed noise pickup on Level 1 Forbidden Region Min I/P Accepted as 1 0 Logic Level The HIGH range typically corresponds to binary 1 and LOW range to binary 0. The threshold region is a range of voltages for which the input voltage value cannot be interpreted reliably as either a 0 or a 1. Output Input Circuit Circuit Noise pickup on connecting medium
12
Types of Digital Systems
No State Present Response determined only by present inputs i.e. Output = Function (Inputs only) Combinational Logic Circuits, e.g. adder A State Present Response determined by present inputs and the present state of the system i.e. Output = Function (Present State, Inputs) and State (new) = Function (Present State, Inputs) Sequential Logic Circuits, e.g. Counter Two Types of state-based systems: State updated at discrete times (e.g. at clock pulses) Synchronous Sequential System State updated at any arbitrary time Asynchronous Sequential System
13
Digital Processing System
Takes a set of discrete information inputs and discrete internal system information (system state) and generates a set of discrete information outputs. Data Path System State Discrete Information Processing System Inputs Outputs Processed Digital Data Row Digital Data Control Outputs Results can affect next Processing Sequence of system states determines the processing performed Same hardware can do different processing by simply changing the stored program! Control Path Stored Program
14
A Generic Digital Computer
CPU: Central Processing Unit FPU: Floating Point Unit MMU: Memory Management Unit Program Data Processor FPU MMU Cache Input Devices: Keyboard, Mouse, Scanner, Microphone Output Devices: Monitor, Printer, Speaker Answer: Part of specification for a PC is in MHz. What does that imply? A clock which defines the discrete times for update of state for a synchronous system. Not all of the computer may be synchronous, however. Input/Output Hard disk, Modem, Network Card
15
2. Number Systems Representation
Positive radix, positional number systems A number with radix (base) r (or b) is represented by a string of digits (n in integer and m in fraction): An - 1An - 2 … A1A0 . A- 1 A- 2 … A- m in which 0 £ Ai < r and “.” is the radix point. The string of digits represents the power series: Integer Part Fraction Part ( ) ( ) i = n - 1 j = - m å å (Number)r = A r i + A r j i j i = 0 j = - 1 + ive exponents - ive exponents (Integer Portion) + (Fraction Portion)
16
Examples Decimal (base 10):
(724.56)10= 7 x x x x x 10-2 = = (base 5): (312.4)5= 3 x x x x 5-1 = = 82.8 Radix Powers Radix Powers
17
Number Systems – Examples
For these 3 systems, Radix is a Power of 2 Number Systems – Examples System General Decimal Binary Octal Hexadecimal Radix (Base) r 10 2 8 16 Digits: 0 to (r-1) 0, 1, …, (r – 1) 0, 1, …, 9 0, 1 0,…, 7 0, …, 15 1 3 Power of 4 Radix -1 -2 -3 -4 -5 r0 r1 r2 r3 r4 r5 r -1 r -2 r -3 r -4 r -5 100 1000 10,000 100,000 0.1 0.01 0.001 0.0001 4 32 0.5 0.25 0.125 0.0625 Integer Fraction
18
Commonly Used Bases Name Radix Digit takes values: Binary (b) 2 0,1*
Octal 8 0,1,2,3,4,5,6,7 10 0,1,2,3,4,5,6,7,8,9 Hexa- Decimal (H) 16 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F 10,11,12,13,14,15 Example 57AC.F4D Decimal (d) Answer: The six letters A, B, C, D, E, and F represent the digits for values 10, 11, 12, 13, 14, 15 (given in decimal), respectively, in hexadecimal. Alternatively, a, b, c, d, e, f are used. *The Binary digit is called a bit Bits take the values 0,1 only (OFF/ON) can be manipulated with Simple, Reliable Circuits
19
Integers (0 – 16) Represented in Various Bases
More Concise 0-15 : digits The larger the Radix the fewer the digits needed to represent a given number LS digit goes through all possible Combinations, then the more significant digit, etc. Octal & Hexa offer a more concise way of representing long bit (binary) sequences in computer systems
20
Binary Numbers: Special Powers of 2
210 (1024) is Kilo, denoted "K" 220 (1,048,576) is Mega, denoted "M" 230 (1,073, 741,824) is Giga, denoted "G" 240 (1,099,511,628) is Tera, denoted “T"
21
Positive Powers of 2 Powers of 2 Exponent Value Exponent Value 1 11
1 11 2,048 2 K 1 2 12 4,096 4 K 2 4 13 8,192 8 K 3 8 14 16,384 16 K 4 16 15 32,768 = 215 = 25 x 210 = 32 K 5 32 16 65,536 64 K 6 64 17 131,072 128 K 7 128 18 262,144 256 K 8 256 19 524,288 512 K 9 512 20 1,048,576 1 M 10 1024 1 K 21 2,097,152 2 M
22
Conversion Between Number Systems
We will consider: (N may contain a fraction part) Conversion from base b to decimal: (N)b (?)10 Conversion from decimal to base b: (N)10 (?)b Conversion between 2 non-decimal bases b1 and b2: (N)b1 (?)b2: In general: Go through decimal. i.e. (N)b1 (?)10 (?) b2 Special case when b1 and b2 are powers of the same number: e.g. between binary (b = 21), octal (b = 8 = 23), and hexadecimal (b = 16 = 24): Here direct conversion is easy… Powers of 2: 43210 => 1 X = 16 + 1 X = 8 + 0 X = 0 + 1 X 21 = 2 + 0 X 20 = 0 2610
23
Conversion from base b to decimal: (N)b (?)10
Simply substitute in the power series expansion Examples: compare lengths b = 2: ( )2 (D)10: D = 1 x x x x x x 2-2 = = 53.75 Notice: smaller bases give longer Number representation Binary is the longest representation! Powers of 2: 43210 => 1 X = 16 + 1 X = 8 + 0 X = 0 + 1 X 21 = 2 + 0 X 20 = 0 2610 b = 16: (E9C4.B6)16 (D)10: D =14 x x x x x x = =
24
Conversion from decimal to base b: (N)10 (?)b
Procedure: Convert the Integer Part Convert the Fraction Part Join the two results with the new radix point 625 – 512 = 113 => 9 113 – 64 = 49 => 6 49 – 32 = 17 => 5 17 – 16 = => 4 1 – = => 0 Placing 1’s in the result for the positions recorded and 0’s elsewhere,
25
Converting integer from decimal to base b:
Repeatedly divide the integer by the new base b and save the remainders, until you get a 0 quotient. The digits for the new base are the remainders with the LS digit being the first remainder (41)10 = (??)2 +0/2 radix point +0/2 Integer in new base Continue until you get a 0 quotient +0/2 Verify result!
26
Converting fraction from decimal to base b:
Repeatedly multiply the integer by the new base b and save the integer part of the result (will never exceed b) until 0 fraction or enough digits The digits for the new base are those integers with the MS digit being that computed first (0.6875)10 = (??)2 radix point Fraction in new base Continue until you get either: Zero fraction, or Enough # of Significant fraction digits Verify Result
27
Comment on the Fractional Part
In the above example the fractional part became 0 as a result of the repeated multiplications and exact conversion was achieved In general it may take many bits to get this to happen or it may never happen Example: Convert to N2 = …2 The fractional part begins repeating every 4 steps, thus repeating 1001 forever! Solution: Specify number of bits to right of radix point and truncate or round to this number of bits, e.g = or = (to 3 bits)
28
1/2 = 0 remainder = 1 Reading off in the forward direction: 0.10112
Example: Convert to Base 8, rounding the resulting fraction to 3 octal digits 1. Convert Integer Part: 153 to Base 8 2. Convert Fraction Part: to Base 8: Answer 1: Converting 46 as integral part: Answer 2: Converting as fractional part: 46/2 = 23 rem = * 2 = int = 1 23/2 = 11 rem = * 2 = int = 0 11/2 = 5 remainder = * 2 = int = 1 5/2 = 2 remainder = * 2 = int = 1 2/2 = 1 remainder = 1/2 = 0 remainder = Reading off in the forward direction: Reading off in the reverse direction: Answer 3: Combining Integral and Fractional Parts: Get 4 octal digits in the Fraction for rounding to 3 significant digits (0.513)10 = (0.4065)8 (0.407)8 after rounding, since 5 is >4 we add 1 to fraction So: =
29
Contd. Example: Convert 153.51310 to Base 8
3. Join the results together with the new radix point: 4. Verify the result: ( )8 (D)10 = 2 x x x x x x 8-3 = 2 x x x 8-3 = ( )10 ( )10 (Difference is due to rounding error) ( )10 = ( )8
30
Conversion between 2 non-decimal bases b1 and b2: (N)b1 (?)b2:
General Case: bases are not powers of the same number: Go through the intermediate step of decimal e.g. (231)5 (??)8 (231)5 (??)10 (??)8 Special (and more important case with computers): Bases are powers of the same number (2), e.g. System Base Digit Range # of Binary bits/digit Binary = Octal = Hexadecimal = Exact number of binary bits needed to store a digit! Range of Digit values Number of binary bits needed to represent a digit Here binary is a better intermediate step!
31
Binary, Octal, & Hexadecimal Conversions
Octal (or Hexadecimal) to Binary: Express each octal (hexadecimal) digit as three (four) binary bits starting at the radix point and going both ways. Binary to Octal (or Hexadecimal): Group the binary bits into three (four) bit groups starting at the radix point and going both ways, padding LH zeros in the integer part and RH zeros in the fractional part as needed Replace each group of three (four) bits with the equivalent octal (hexadecimal) digit Octal Hexadecimal Go through binary as an intermediate step (use above) e.g. Octal Binary Hexadecimal
32
Examples: Octal (Hexadecimal) Binary
Example: Hexadecimal to Binary (F C . B D)16 = = ( )2 Example: Binary to Octal ( )2 = ( )2 = ( )2 = ( )8 Always start at the radix point Working your way away from it in both directions Answer 1: 110|011| |111|111 2 Regroup: 1|1001| |1111|1(000)2 Convert: D F Answer 2: Marking off in groups of three (four) bits corresponds to dividing or multiplying by 23 = 8 (24 = 16) in the binary system. Appended 0’s
33
Example: Octal to Hexadecimal via Binary
1. Convert octal to binary 2. Use groups of four binary bits and express them as hexadecimal digits Example: Octal Binary Hexadecimal ( )8 = ( D E 8)16 Represent Octal in binary Group into 4 bit groups for both the integer and fraction parts, starting at the radix point Append leading 0’s to the left of integer part and trailing 0’s to the right of the fraction part as needed Express each group of 4 bits in hex Appended 0’s Answer 1: 110|011| |111|111 2 Regroup: 1|1001| |1111|1(000)2 Convert: D F Answer 2: Marking off in groups of three (four) bits corresponds to dividing or multiplying by 23 = 8 (24 = 16) in the binary system.
34
Using Binary Bits to Represent Information: Numbers and Codes
We can assign any combination of binary bits (called a code word) to represent any information item as long as data is uniquely represented Two Basic Types of Information: Numeric (e.g. signed or unsigned integers) Code tied to binary numbers Better use codes that allow simple implementation of common arithmetic operations Non-numeric (e.g. symbols, colors, the alphabet) Code not tied to binary numbers Greater flexibility: No arithmetic operations involved
35
If you represent only a subset of m items, m < rn
Q 1 Given n digits of radix r, what is the maximum number of different information items that can be represented? With n digits in radix r, there are rn distinct combinations (codes), i.e. a maximum of rn items can be uniquely represented If you represent only a subset of m items, m < rn , this leaves (rn – m) codes unused Examples: 2 decimal digits (r = 10) can represent up to 102 = 100 elements: (00, 01, 02, …, 98, 99) 3 binary bits (r = 2) can represent up to 23 = 8 elements: (000, 001, 010, 011, 100, 101, 110, 111) If only the blue 6 codes are used, the remaining 2 codes are unused and maybe we don’t care what the output would be for them- this simplifies the circuits needed, see Unit 2
36
Q 2 Given M different information items to represent, what is the minimum number (n) of radix r digits do we need? rn M > r(n – 1) i.e. n = the smallest integer that is ≥ [logr M] Example: What is the minimum # of binary bits are required to represent the 10 decimal digits with a binary code? n = smallest integer that is ≥ log2 (10) ≥ 3.33 n = 4 need at least 4 bits: 24 > 10 > 23 M = 10 Therefore n = 4 since: 24 =16 is 10 > 23 = 8 and the ceiling function for log2 10 is 4. How many spare codes do we have here?
37
Using Binary to represent Symbols: 1. ASCII Character Codes
American Standard Code for Information Interchange (ASCII) Popular for representing information as character-based textual data Uses 7-bits (i.e. 27 = 128 combinations) to represent: 94 Graphic printing characters (alphanumeric, symbols,…) 34 Non-printable characters Non-printable characters are used for: Text formatting (e.g. BS = Backspace, CR = carriage return) Record marking and flow control (e.g. STX and ETX start and end text areas) Communication handshaking, e.g. ACK
38
ASCII Character Codes A = 100 0001 = 41 Hex a = 110 0001 = 61 Hex
Non-Printable Characters MSB 8 Columns LSB 16 Rows 16 x 8 = 128
39
Extends ASCII to 65,536 universal characters codes
2. UNICODE Extends ASCII to 65,536 universal characters codes 2 byte (16-bit) code words For encoding characters in world languages Used in many modern applications Arabic codes: from 0600 to 06FF (hex) How many codes are used for Arabic? Answer after we cover hexadecimal arithmetic
40
Using Binary to represent Numbers: e.g. 0-9 using 4 bits (16 codes)
10 16 Using Binary to represent Numbers: e.g. 0-9 using 4 bits (16 codes) 0000 0001 1 9 1111 There are different ways to map the 10 decimal elements to the 16 binary codes of 4 bits. 4 of those are shown below: Straight Binary 4 coding schemes 1’s Complementing G2 G1 G0 Gray: Always only 1-bit changes between adjacent codes All 4 bits Change
41
Gray Code: Advantages for Optical Encoding of Angular Shaft Position
The 3 optical sources/sensors (or sector marks) may not be perfectly aligned Some may change before the others at sector crossings For example, at the crossing between positions 3 and 4: Gray: Only bit G0 changes code can change only from 111 to (i.e. correct new code is the only possibility) Binary: All 3 bits change several wrong intermediate codes can be generated (e.g. 110). If read, they give wrong information on shaft position 111 000 100 000 B White = 1 B 1 110 001 101 001 B 1 2 Yes, as illustrated by the example that followed. 3 2 111 101 4 3 010 4 011 G2 G1 G0 Shaft Rotation 3 sensors 100 3 sensors 011 110 010 (a) Binary Code for Positions 0 through 7 (b) Gray Code for Positions 0 through 7
42
Binary-Coded Decimal (BCD)
The BCD code consists of the first ten values (0 to 9) of the bit (8,4,2,1) binary codes Can be used to encode the digits of a decimal number separately in in binary, e.g. (863)10 = ( )BCD Note this is different from converting the decimal number to binary: (863)10 = ( )2 Which is a more concise representation? BCD is Basis for BCD computer arithmetic: e.g. BCD addition, subtraction, etc. See later 10 BCD Codes Answer 1: 6 Answer 2: 1010, 1011, 1100, 1101, 1110, 1111 6 Unused Binary Codes (don’t care conditions)
43
Error-Detection Codes
Redundancy (additional information), in the form of extra bits, can be appended to the binary code for the data to allow error detection, and possibly error correction A simple form of redundancy is the parity bit, an extra bit appended to the code word to make the total number of 1’s odd or even. Parity can detect all single-bit errors and some multiple-bit errors Even parity bit: Makes total number of 1’s even Odd parity: Makes total number of 1’s odd
44
Example: 4-Bit Parity Code (3 bits of data + 1 parity bit)
Even Parity Odd Parity Message Parity Message Parity - - 0000 0001 0011 0010 0101 0100 0110 0111 1001 1000 1010 1011 1100 1101 1111 1110 Even Parity Bits: 0, 1, 1, 0, 1, 0, 0, 1 Odd Parity Bits: 1, 0, 0, 1, 0, 1, 1, 0 Note: Odd parity is the complement (inversion) of even parity Question: can the parity scheme detect 1, 2, or 3 bit errors? We will design circuits that generate parity bits in Unit 3
45
e.g. for n = 8 bits (a byte): 0 to [28-1]/28 = 0 to 0.9961
Range of values (smallest to largest) of unsigned integers and fractions represented using n bits Unsigned integer values range: 0 to 2n-1 00……0 = 0 to 11……1 = 1 x x 21 + …. + 1 x 2(n-1) = 2n-1 e.g. for n = 8 bits (a byte): Range: 0 to 28-1 = 0 to 255 Unsigned fraction values range: 0.0 to [2n-1]/2n 0.00….0 = 0 to …1 = 1 x x …. + 1 x 2-n = 2n [1 x x …. + 1 x 2-n]/2n = [1 x 2n x 2(n-2) + …. + 1 x 20]/2n = [2n-1]/2n (an n-bit integer shifted right 8 places!) e.g. for n = 8 bits (a byte): 0 to [28-1]/28 = 0 to (n-1) -1 ……. -n M = 10 Therefore n = 4 since: 24 =16 is 10 > 23 = 8 and the ceiling function for log2 10 is 4.
46
3. Basic Computer Arithmetic
1. Single Bit Arithmetic: Addition with Carry, Subtraction with Borrow, and Multiplication 2. Multiple Bits Arithmetic: On Unsigned Binary Integers Binary Addition, Subtraction, and Multiplication BCD Addition b. On Signed Binary Integers Signed-magnitude 1’s and 2’s complement c. Floating Point Binary Arithmetic: e.g x 106 Not covered in this course 3. Octal and Hex arithmetic: Unsigned addition, subtraction and multiplication
47
1. Single Bit Binary Addition with Carry
X 1-bit Adder Z C S
48
Single Bit Binary Subtraction with Borrow
X 1-bit Subtractor Z B D
49
Single Bit Binary Multiplication
50
2. Unsigned Numbers (Multiple Bit) Binary Addition
Extending bit operation to multiple bit: Generated carries to the bit Augend Addend Sum Notes: Assume 0 default Carry-In to the least significant bit The red carry is end-carry from the most significant bit In unsigned addition, an end carry represents an overflow- Result is larger than can be represented in the used number of bits (here 5 bits) Always 0 for 1st bit (1st stage) End Carry Verify results by doing the operation on the equivalent decimal values = 29 < 33, No overflow = 45 > 33, overflow Result too large for 5 bits! Carries Augend Addend Sum
51
Unsigned Numbers Binary Subtraction with borrow
M – N First ensure that M N (by swapping numbers if necessary) Extending bit operation to multiple bits: (here 5 bits) Borrows from bit Minuend Subtrahend Difference Notes: If N is larger than M, swap the two, do the operation, and consider the result negative (we do not have a sign bit yet!) Assume 0 default Borrow-In to the least significant bit The red borrow is Borrow-out from the most significant bit (End Borrow) Borrow and subtraction mechanisms are cumbersome- Will seek better solutions for subtraction (through addition) Always 0 for 1st bit in 1st stage Verify results by doing the operation on the equivalent decimal values = 4 Carries Augend Addend Sum
52
Unsigned Numbers Binary Subtraction with borrow
M – N No restrictions, M >=< N No Prior Comparison of M. N Algorithm: Subtract N from M without prior comparison or swapping If no end borrow occurs, then M ³ N, and the result obtained is non-negative and correct. If an end borrow occurs, then N > M. the result is (M + 2n) - N. To get N-M, we subtract the result from 2n , and consider it negative. Examples: (-) End borrow caused by last stage It amounts to adding 2n to M 9 M 4 7 -N 7 What is the problem on the RHS? How we discover it? How we handle (correct) it? 2 13 This is (M + 2n ) – N (Wrong) To get N-M from this: do 2n – (M+2n-N) i.e. subtract the result from 2n and call it - ive 16 Correction Step 13 2n – number = its 2’s complement So, 2’s complementing can help! 3
53
The Complement of an integer
Depends not only on the Integer N but also on the number of digits (bits) n used to represent it The Complement of an integer For a number N of n digits in radix r we define: The Radix (r’s) Complement Defined as rn - N The Diminished Radix [(r - 1)’s] Complement Defined as (rn - 1) - N For binary number, r = 2, we have: The 2’s Complement Defined as 2n - N The 1’s Complement Defined as (2n -1) - N Decimal number system: r = 10, 2 digits # e.g. 57 10’s comp. = = 43 9’s comp. = (102-1)-57 = = 42 For n = 4 bits 2n = 24 = 16 2n-1 = 16-1 = 15 = (n 1’s) Let N = 1001, 1’s comp of N - 1001 0110 1’s comp. is easier to generate! Then 2’s comp = 1’s comp. + 1 1 0 , 0 1 (NOT gates)
54
Binary 1's Complement For r = 2, N = 011100112, n = 8 (8 digits):
(rn – 1) = = or The 1's complement of is then: (8 1’s) – Since the 2n – 1 factor consists of n 1's and since 1 – 0 = 1 and 1 – 1 = 0, the one's complement is obtained by complementing each individual bit (bitwise NOT).
55
Binary 2's Complement For r = 2, N = , n = 8 (8 digits), we have: (2n ) = or (1 in the (n+1)th position) The 2's complement of is then: – Note this is the 1's complement plus 1 Can design simple hardware for generating the 2’s complement from the easier-to-generate 1’s complement Eight 0’s 2n-N = (2n-1-N) +1 2’s Complement 1’s Complement
56
Alternative 2’s Complement Method
Given: an n-bit binary number, beginning at the least significant bit and proceeding to higher bits: Starting at the LS bit, Copy all least significant 0’s Copy also the first 1 Complement all remaining bits thereafter. 2’s Complement Example: Copy underlined bits: and complement all remaining bits to the left:
57
Important Complement Remarks
Taking the complement of a complement returns you back to the original number, Example: 2’s complement for an n-bit number N 2’s comp. of N = 2n-N 2’s comp. of the 2’s comp. of N = 2n- (2n-N) = N Complements of 0 (represented as n bits): 1’s comp of 0 = n 1’s 2’s comp of 0 = 1’s comp of = Carry + n 0’s = 1’s Complementing 0000 1111 Ignored- Outside the n bits +1 2’s Complementing (also by the rule on previous slide!) 0000
58
Subtraction of two Unsigned Numbers (M-N) Using the 2’s Complement
For n-bit, unsigned numbers M and N, find M - N: Add the 2's complement of the subtrahend N to the minuend M: M + (2n N) = M N + 2n If M N, the sum produces end carry (rn) which is discarded; leaving the correct answer M - N. If M < N, the sum does not produce an end carry and, from above, is equal to 2n ( N M ), i.e. the 2's complement of ( N M ). To obtain the result, i.e. negative (N – M): Take the 2's complement of the sum (N-M) and place a sign to its left!. M - N i.e. we are doing subtraction by addition! (Simpler)
59
Unsigned Subtraction with 2’s Complement Example 1
Find – – = 17 The end carry of 1 indicates that no correction is required on the result . No need to compare amplitudes or re-arrange! 84 1 - 67 2’s comp 17
60
Unsigned Subtraction with 2’s Complement Example 2
Find – – The 0 end carry indicates that a correction of the result is required (taking the 2’s comp of the result and considering it negative) Result = – ( ) = -17 67 - 84 2’s comp - 17 Take 2’s comp
61
Subtraction of two Unsigned Numbers (M-N) Using the 1’s Complement
For n-bit, unsigned numbers M and N, find M N: Add the 1's complement of the subtrahend N to the minuend M: M + (2n 1 N) = M N 1 + 2n If M N, the result is excess by 2n 1 and a carry is generated. Ignoring the carry removes 2n, leaving the result short by 1. To fix this shortage, whenever and end carry occurs, add it in the LSB position. This is called the end-around carry. If M < N, the sum does not produce an end carry. The sum is equal to 2n 1 ( N M ), the 1's complement of ( N M ). So, to obtain the final result, (N – M), take the 1's complement of the sum and place a to its left.
62
End-around carry occurs. 1
Subtraction of two Unsigned Numbers (M-N) Using the 1’s Complement Example 1 Find – – +1 End-around carry occurs. 1 84 - 67 1’s comp 17
63
Subtraction of two Unsigned Numbers (M-N) Using the 1’s Complement Example 2
Find – – The end carry of 0 indicates that a correction of the result is required (1’s comp and consider negative) Result = – ( ) 67 - 84 1’s comp -17 Take 1’s comp
64
Unsigned Numbers (Multiple Bits) Binary Multiplication
11 5 55 Verify the result by doing the operation on the equivalent decimal values
65
Unsigned Numbers BCD Addition
Use binary arithmetic to add the BCD digits: 3 8 1000 Eight +5 +5 +0101 Plus 5 8 OK (< 9) 13 1101 is 13 (> 9) If result is > 9, it must generate a carry and be corrected! To correct the digit, subtract 10 by adding 6 and ignoring the carry: -10 = -(16-6) = (the carry ignored within digit) 8 1000 Eight We try to avoid subtraction! Replacing it with addition! +5 +0101 Plus 5 13 1101 13 ( is > 9) +0110 so add 6 (always, for results > 9) carry = 1 0011 giving 3 + carry 0001 | 0011 Final answer (two digits) The adder circuit utilizes the resulting carry bit by sending it as carry-in to the next digit
66
Add 2905BCD to 1857BCD showing carries and digit corrections.
Unsigned Numbers BCD Addition: Example Add 2905BCD to 1857BCD showing carries and digit corrections. 1010 1 1 17>9 +6 6<9 +0 12>9 +6 4<9 +0 Add 6 or 0 4762
67
Unsigned Numbers Arithmetic in Octal & Hexadecimal
Perform digit calculations in decimal then express results in the appropriate base system Start here At the LS Digit Base 16 1 Carry to next digit 8 10 8 Remains in this digit 2 Base 8
68
Integer Representations using n bits
Unsigned Integers The full n bits are the magnitude of the number. The sign is not represented as part of the number Signed Integers The MS bit indicates the sign: 0 = Positive, 1 = Negative Signed-Magnitude The remaining (n – 1) bits represent the positive magnitude of the number (sign and magnitude are treated separately by the hardware) Signed-Complement (sign is part of the number) - Positive integer: Exactly as in signed-magnitude, - Negative integer: As the complement of the positive value of the number Two Types: Signed 1's Complement Uses 1's Complement Arithmetic Signed 2's Complement Uses 2's Complement Arithmetic Magnitude n bits Sign is separate Magnitude Sign 1 bit n-1 bits Sign is part of the number Sign 1 bit Magnitude only for + ive integers Example: Interpret as : a. Unsigned number b. Signed-Magnitude number c. Signed-1’s Complement c. Signed-2’s Complement
69
Signed Integer Representation in 3 notations Examples for n = 4
4-bits 16 different codes Number Range (4 bits16 values) (2n-1-1) To +(2n-1-1) (2n-1) To +(2n-1-1) (2n-1-1) To +(2n-1-1) Positive numbers have identical Representation in all 3 notations Dual representations of the 0 is a disadvantage 0000 2’s Complementing 1’s Complementing Unique Codes only: A code Should not Represent 2 numbers All Negative #s Represented Have MSB = 1 (Use as sign bit) How to negate A number? 16th code Used for -8 16 codes already used 16 codes already used
70
Signed-Magnitude Addition/Subtraction MN (each of M and B can be + ive or – ive)
Follow rules of ordinary arithmetic We compare only the signs of the two operands and conclude the sign of the result based on these signs and the outcome of operation Addition/Subtraction of magnitudes is similar to using unsigned integers Addition: (operations done on magnitudes) a. If the two signs are equal: (+A) + (+B) = +(A+B) or (-A) + (-B) = -(A+B) 1. Add the two (unsigned) magnitudes, overflow can occur 2. Check that there is no overflow (an end carry out of the mag.) 3. The sign of the result is the same as the common sign b. If the two signs are different: (+A) + (-B) = +(A-B) or (-A) + (+B) = -(A-B) Subtract the second magnitude from the first (Since we subtract 2 unsigned #s, Overflow can not occur) 2. If no borrow occurs (A>B), result is correct and has the sign of the first operand If a borrow occurs (A<B): Result needs correction Correct it by taking its two’s complement and give it the sign of the second operand.
71
Signed-Magnitude Addition/Subtraction MN (each of M and B can be + ive or – ive)
Follow rules of ordinary arithmetic We compare only the signs of the two operands and conclude the sign of the result based on these signs and the outcome of operation Addition/Subtraction of magnitudes is similar to unsigned integers Subtraction: (operations done on magnitudes) a. If the two signs are different: (+A) - (-B) = +(A+B) or (-A) - (+B) = -(A+B) 1. Add the two (unsigned) magnitudes, Overflow can occur 2. Check that there is no overflow (an end carry out of the mag.) 3. The sign of the result is the same as the sign of the first operand b. If the two signs are equal: (+A) - (+B) = +(A-B) or (-A) - (-B) = -(A-B) Subtract the second magnitude from the first (Since we subtract 2 unsigned #s, Overflow can not occur) 2. If no borrow occurs (A>B), results is correct and has the common sign If a borrow occurs (A<B): Result needs correction Correct it by taking its two’s complement and give it the opposite of the common sign.
72
Signed-Magnitude Arithmetic- Examples Operations are performed on magnitudes only, Get Sign separately Example 1: Example 2: Example 3: Example 3: +2 +5 Addition (a), overflow did not occur 0111 +2 - 5 Addition (b, borrow correction) Overflow not possible 1011 +2 - 5 Subtraction (a), overflow did not occur Example 1: Since the parity of the signs is 0: ADD magnitudes giving 111 Append the sign of the first operand to obtain 0111 Example 2: Since the parity of the signs is 1: SUBTRACT 010 - 101 101 Borrow of 1 Taking the 2’s complement and appending the complement of the sign: 1011 Example 3: Since the parity of the signs is 0, ADD the magnitudes 101 111 Appending the sign if the top operand: 0111 - 2 - 5 Subtraction (b, borrow correction) Overflow not possible 0011
73
1. Add the two numbers including the sign bits,
Signed-Complement Arithmetic Both addition and subtraction use addition Start with both numbers A, B represented in the complement notation: For Addition: 1. Add the two numbers including the sign bits, Discarding any carry out of the sign bits (for 2's Complement representations), or using it as end-around carry (for 1's Complement representation) This automatically determines the sign of the result 2. Overflow is possible only if both numbers added have the same sign. In this case if the sign of the result is different from this common sign, an overflow has occurred (result is wrong) For Subtraction: A – B = A + (-B) = A + Comp of B Form the complement of the number you are subtracting and then perform addition- applying the same addition rules above What does taking the 2’s complement of a number do to it?
74
Signed Complement Arithmetic Examples, n = 4 bits
Carry ignored Carry, End around Example 1: 0000 Example 2: Example 1: 0000 Example 2: -3 +3 -2 +3 An overflow? Using 1’s Complement Notation 1 Using 2’s Complement Notation Sign bit 0001 -3 +3 -2 +3 Carry ignored Carry, End around 1101 +1101 1010 1101 +1100 1001 Example 1: Result is The carry out of the MSB is discarded. Example 2: Complement 0011 to 1101 and add. Result is The carry out of the MSB is discarded. An overflow? Last 2 carries are identical 1 1 - ive, = - ive, = 1 1010 Judge Overflow from addition signs
75
Signed Complement Arithmetic Examples, n = 4 bits
Carry ignored Example 3: 0110 -6 -4 Largest negative number that can be Represented in 4 bits is -8 (-6) + (-4) = -10 , i.e. overflow expected Signs of overflow from addition operation: - Sign of result is different from that of the two numbers added - Last 2 carries are different - Result is wrong An overflow? 1 +4! Sign bit Using 2’s Complement Notation Example 1: Result is The carry out of the MSB is discarded. Example 2: Complement 0011 to 1101 and add. Result is The carry out of the MSB is discarded.
76
Overflow and its Detection
Overflow occurs if the result from an n-bit addition or subtraction can not be accommodated in the n-bit hardware used and an (n+1) th bit is needed With Unsigned numbers: Overflow is possible when adding two number Overflow is not possible when subtracting two number (since the results is the largest of the two, hence fits into the n-bits) With Signed Numbers: Overflow can occur when: Adding two operands of the same sign Subtracting two operands of different signs How to detect it: When the sign bit is not part of the binary operation, we could rely on the end carry as an indication of overflow But with 2’s complement or 1’s complement the sign bit is used as part of the number and we can not rely on end carry to indicate overflow In this case, overflow is detected if the two input numbers that are added have the same sign while the result has the opposite sign Another way is overflow V = cn XOR cn-1. (different carries from last 2 stages). Works fine for 1’s complement notation and with limitations for the 2’s Complement notation In both cases the two numbers are added as unsigned numbers overflow is possible
77
Overflow Detection Example, Adding with 2’s complement notation:
Carries ___________________ 1 Signs With 8 bits , max + ive number is +127 +150 > +127 Overflow Last 2 carries are different
78
Unit 1: Number Systems and Codes Overview
Introduction: Information processing and representation Number Systems: Binary, Octal and Hexadecimal Number Base Conversion: Dec, Bin, Oct, and Hex Digital Codes for Numbers and Symbols Numbers: Binary, BCD, Excess-3, Gray, Parity Alphanumeric (symbols) :ASCII, Unicode Computer Arithmetic on Unsigned Numbers: Basic Operations on binary: Add, Subtract, Multiply Arithmetic in Octal and Hexadecimal, BCD addition Binary Signed Number Representation and Arithmetic: Subtraction, complement notation Signed representation, Addition/Subtraction, Overflow
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.