Presentation on theme: "HEXADECIMAL NUMBERS Code"— Presentation transcript:

WRITING one’s and zero’s can be error prone when dealing with large numbers. IBM came up with the following method of dealing with these numbers. BASE 16 with digits in base 10 are represented by the following notation in Hexadecimal. 016 = = = = 810 116 = = = = 910 216 = = 210 A16 = = 1010 316 = = 310 B16 = = 1110 416 = = 410 C16 = = 1210 516 = = 510 D16 = = 1310 616 = = 610 E16 = = 1410 716 = = 710 F16 = = 1510

EXAMPLE Convert A716 to binary
Base 16 Conversion EXAMPLE Convert A716 to binary Note: This is easy because of the relationship between the two bases A = = 0111 Therefore 0A7H or \$A7 = Note leading 0 before A. Since this is a number a different base than base 10, absence of the zero may confuse a compiler. Therefore it is customary to add the leading 0 to any hex character that begins with a letter of the alphabet (A,B,C,D,E,F) !! These are NUMBERS in base 16 !!

Similar Rules as to those in binary to decimal Convert to binary then weighted multiplication -OR- Leave in HEX and use HEX multiplication EXAMPLE (repeated multiplication) \$F8E6H = F(16)3 + 8 (16)2 + E(16)1 + 6(16)0 = 15(16)3 + 8 (16)2 + 14(16)1 + 6(16)0 = 61, = 63,718

Similar Rules as to those in decimal to binary Repeated HEX division EXAMPLE (repeated division) Divisors are for 16 binary digits (4 HEX) = 2479/16 = remainder 15 or F 154/16 = remainder 10 or A 9/ = remainder 9 or 9 Answer = 9AFH

Same EXAMPLE (but weighted division) Divisors are for 16 binary digits (4 HEX) 4096, 256, 16, 1 = 2479/256 = 9 remainder x256 = 175/16 = 10 or A remainder x 16 = 15/ = 15 or F Answer = 9AFH

BINARY CODES 7421 6311 5421 5311 5211 DECIMAL 8421 BINARY 0 0000 0000
BCD 4 Bit BCD CODES DECIMAL BINARY ....

MORE 4-BIT BCD CODES Decimal /2/1 74/2/1 The / is subtract weight

Decimal 2-out of-5 63210 Shift-Counter 86421 51111
5-BIT CODES Decimal 2-out of Shift-Counter

Alphanumeric Codes OTHER CODES ASCII CODE 7 BITS EBCDIC CODE 8 BITS
UNICODE Bits

ASCII CODE Part 1

ASCII CODE CONTROL CODES

EBCDIC Part I

EBCDIC PART II

UNICODE 16 BIT CODE First Page 0000H - 00FFH Same as ASCII
Has not been standardized the remaining 0FFFFH minus 00FFH available for all remaining countries and languages!

Representation of Negative Numbers
Number Systems Representation of Negative Numbers Three major schemes: sign and magnitude ones complement twos complement nines complement tens complement Assumptions: we'll assume a 4 bit machine word 16 different values can be represented roughly half are positive, half are negative

Number Systems Sign and Magnitude Representation
High order bit is sign: 0 = positive (or zero), 1 = negative Three low order bits is the magnitude: 0 (000) thru 7 (111) Number range for n bits = +/ Representations for 0 Two +/- n-1

Number Systems Ones Complement
Subtraction implemented by addition & 1's complement Still two representations of 0! This causes some problems Some complexities in addition

Number Representations
Twos Complement like 1's comp except shifted one position clockwise Only one representation for 0 One more negative number than positive number

1 + 1 = 0 + carry 1 to next column RULES FOR SUBTRACTION 0 - 0 = 0
BINARY ARITHMETIC RULES FOR ADDITION 0 + 0 = 0 0 + 1 = 1 1 + 0 = 1 1 + 1 = carry 1 to next column RULES FOR SUBTRACTION 0 - 0 = 0 0 - 1 = 1 and borrow from next column 1 - 0 = 1 1 - 1 = 0 Borrowing 1 from next column is equivalent to subtracting 1 from that column

EXAMPLE ADD 1010 to 1101 1010 + 1101 10111 SUBTRACT 1010 from 1101
Addition EXAMPLE ADD 1010 to SUBTRACT 1010 from 1101 borrow 4 subtract 2 nothing in 4 position - 1010 0011

Number Representations
Addition and Subtraction of Numbers Sign and Magnitude 4 + 3 7 0100 0011 0111 -4 + (-3) -7 1100 1011 1111 When signs is same, result sign bit is the same as the operands' sign. Just add magnitudes When signs differ, operation is subtract, sign of result depends on sign of number with the larger magnitude. This is what we do in base 10. 4 - 3 1 0100 1011 0001 -4 + 3 -1 1100 0011 1001

Number Systems Sign and Magnitude Cumbersome addition/subtraction. Must compare magnitudes to determine the sign of result.

/N = (2n - 1) - N Ones Complement
The symbol N, with a bar over it, is used to represent the complement. Also used to indicate inversion are /, ‘, or ! (CUPL and ABEL) N is positive number, then N is its negative 1's complement. N is the precision, as many 1’s as required. Binary can be any precision, but BCD requires 4 bits. Precision 4 bits Therefore, The general formula for finding /N = (2n - 1) - N To find 1's complement of 7 2 = -1 = 1111 -7 = 1000 = -7 in 1's comp. 4

NOTE: Ones Complement ZERO Applying the general formula
/N = (2n - 1) - N To find 1's complement of 0 2 = -1 = 1111 -0 = = -0 in 1's comp. 4 NOTE: The complement of 0 is 1111, which means, there are 2 representations of 0. Positive 0 Negative 0

SHORTCUT 1’s Complement
Shortcut method: Replace all 0’s with 1’s, and all 1’s with 0’s simply compute bit wise complement 7 in 1’s complement > 1000

Number Systems Addition and Subtraction of Numbers
Ones Complement Calculations 4 + 3 7 0100 0011 0111 -4 + (-3) -7 1011 1100 10111 1 1000 End around carry 4 - 3 1 0100 1100 10000 1 0001 -4 + 3 -1 1011 0011 1110 End around carry

Number Systems Addition and Subtraction of Binary Numbers
Ones Complement Calculations Why does end-around carry work? Its equivalent to subtracting 2 and adding 1 n For (M > N) M - N = M + / N = M + (2n - N -1) = (M - N) + 2n - 1 For M + N < 2n-1 -M + (-N) = M + N = (2n - M - 1) + (2n - N - 1) = 2n + [2n (M + N)] - 1 after end around carry the -1 is canceled if a carry, else not: = 2n (M + N) this is the correct form for representing -(M + N) in 1's complement!

Number Systems Two’s Complement Numbers
/N = 2n - N Note: subtract number immediately. 4 2 = 7 = 1001 = represents -7 sub Example: Twos complement of 7 2 = -0 = 0000 = 0 4 sub Example: Twos complement of 0 Only 4bits can represent number. The most significant 1 is dropped! Shortcut method Number 1: Twos complement =1’s complement + 1 0111 -> > 1001 (representation of -7) 1001 -> > 0111 (representation of 7)

SHORTCUT Shortcut method Number 2:
Starting from the right, least significant bit, if 0 leave unchanged. Continue from right to left, if 0, no change. When the first 1 is encountered, we leave it unchanged, but complement each digit to the left. 710 = Apply method = 6810 = yields The lead 0 is important so that the number to be converted is positive! In base 10, the - sign does that for us. With only on and off (0,1) we need this extra bit of information to describe the signed number. -6810 = yields =

Number Systems Addition and Subtraction of Binary Numbers
Twos Complement Calculations 4 + 3 7 0100 0011 0111 -4 + (-3) -7 1100 1101 11001 4 - 3 1 0100 1101 10001 -4 + 3 -1 1100 0011 1111 Simpler addition scheme makes twos complement the most common choice for integer number systems within digital systems

Number Systems Addition and Subtraction of Binary Numbers
Twos Complement Calculations Why can the carry-out be ignored? -M + N when N > M: n n M* + N = ( M) + N = (N - M) n Ignoring carry-out is just like subtracting 2 n-1 -M + -N where N + M < or = 2 n n -M + (-N) = M* + N* = (2 - M) + (2 - N) = (M + N) + 2 n n After ignoring the carry, this is just the right twos compl. representation for -(M + N)!

Number Systems Overflow Conditions
Add two positive numbers to get a negative number or two negative numbers to get a positive number -1 +0 -1 +0 -2 1111 -2 0000 +1 1111 0000 +1 1110 0001 1110 -3 -3 0001 +2 +2 1101 1101 0010 0010 -4 -4 1100 +3 0011 1100 +3 0011 -5 -5 1011 1011 0100 +4 0100 +4 1010 -6 1010 0101 -6 0101 +5 +5 1001 1001 0110 0110 -7 +6 -7 1000 +6 0111 1000 0111 +1=6 (ok) -8 +7 -8 +7 +2=7 (ok) +3=-8 (error) = +7 4 bit representation 3 bits + sign in msb range -8 to +7

This is true for both 1’s and 2’s complement addition.
OVERFLOW If carry-in (penultimate) is added to sign bit and there is a carry-out then no overflow. if carry-in differs from carry-out then overflow This is true for both 1’s and 2’s complement addition.

Number Systems Overflow Conditions Example is using 2’s Complement 5 3
-8 carry -7 -2 7 carry Overflow Overflow Note: the carry and penultimate carry are not the same! This can be very easily implemented in hardware. XOR 5 2 7 carry -3 -5 -8 carry No overflow Overflow when carry in to sign does not equal carry out

Decimal digits 0 thru 9 represented as 0000 thru 1001 in binary Addition: 5 = 0101 3 = 0011 1000 = 8 5 = 0101 8 = 1000 1101 = 13! Problem when digit sum exceeds 9 Solution: add 6 (0110) if sum exceeds 9. 5 = 0101 8 = 1000 1101 6 = 0110 = 1 3 in BCD 9 = 1001 7 = 0111 = 16 in binary 6 = 0110 = 1 6 in BCD

BCD Subtractin Must use 10’s complement
To find 10’s complement, first generate 9’s complement by subtracting The number from 9. If n = 9, 9-9 =0 9’s complement of 9 is 0 or 0000 If n = 8, 9-8 =1 9’s complement of 8 is 1 or 0001 If n = 7, 9-7 =2 9’s complement of 7 is 2 or 0010 If n = 6, 9-6 =3 9’s complement of 6 is 3 or 0011 If n = 5, 9-5 =4 9’s complement of 5 is 4 or 0100 If n = 4, 9-4 =5 9’s complement of 4 is 5 or 0101 If n = 3, 9-3 =6 9’s complement of 3 is 6 or 0110 If n = 2, 9-2 =7 9’s complement of 2 is 7 or 0111 If n = 1, 9-1 =8 9’s complement of 1 is 8 or 1000 If n = 0, 9-0 =9 9’s complement of 0 is 9 or 1001 Add 1 to 9’s complement to find 10’s complement.

Example of BCD Subtract
To subtract 2 from 5, find 10’s complement of 2 which is Bit Representation. The 1 carry, is normal form and indicates answer is 3. In binary, 1001 is exceeded! Must add 6 to correct. Known as BCD adjust. 0110 The 1 carry is normal form answer is 3 base 10.

Addition Examples 4 BITS signed and unsigned

Addition Examples 4 BITS signed and unsigned
Note: overflow in all cases is determined by the precision, the carry, and in signed representation, the penultimate carry. This slide assumes a 4 bit precision. In base 10, the minimum sum is 0 and the maximum sum is 15 unsigned. A carry would indicate overflow. Base 10 max and min signed is -8 to +7, which is also true for 2’s and 10’s complement. The limit drops to + and - 7 for 1’s and 9’s complement because of the double representation of zero. Overflow in these cases is determined by the carry and the penultimate carry. If both are same, no overflow! If opposite, overflow. Note in base 10 you 9’s and 10’s complement overflow conditions are recognized by the carry and penultimate carries. However, the range for single digit is -9 to +9 for 9’s complement, and -10 to + 9 for 10’s complement. If the arithmetic is using base 2, the overflow range will change to -7 to +7 in 9’s complement, and -8 to, +7 in 10’s complement. A negative number is preceded by a 9 in base 10, so a -7 in 9’s complement is 92. In 10’s complement it is a 93. The next slide shows more examples. In a computer the carry will contain all the information when used with the penultimate carry to indicate over flow.

Excess-3 code is another important BCD Code.
To encode a decimal number, add 3 to each digit before converting to binary. EXAMPLE (Note: 2 digits represented by 2 4 bit numbers) to excess-3 = = =5 to excess-3 = = =12

CASE 1 In excess-3 addition, whenever we add two numbers whose sum is 9 or less, an excess-6 number is formed. To return to excess-3 we must subtract 3. EXAMPLE 2 +5 = 7 1101 excess-6 1010 excess-3 equivalent of 7

CASE 2 In excess-3 addition, whenever we add two numbers whose sum is greater than 9, there will be a carry from one group to the next. When this happens, the group that produced the carry will revert to 8421 (BCD). To return to excess-3 we must subtract 3 from that group. EXAMPLE carry not carried to 10’s because of sum excess-3 for 29 excess-3 for 39 first result subtract less than 9, add 3 excess-3 for 68

Similar presentations