number system

53
NUMBER SYSTEMS, OPERATIONS AND CODES Chapter 2

Upload: azrul-mohamad

Post on 23-Nov-2015

32 views

Category:

Documents


0 download

DESCRIPTION

BInaryOctaHexadecimal

TRANSCRIPT

Digital System

Number Systems, operations and codesChapter 2Number SystemNumber systemBase conversionWhy Learn Number SystemArithmeticSign NumberBCD and ASCII codes

Number SystemsIs an ordered set of symbols, called digits.Has relationship between digits:- (+) (-)(/)(x)Radix (r) or base, of the number system is the total number of single digits allowed in the number system.4 number systems commonly used in digital system design and computer programming are:

Decimal ~> r = 10Binary ~> r = 2Octal ~> r = 8Hexadecimal ~> r = 16

Decimal number system has 10 basic (base) digits:-0, 1, 2, 3, 4, 5, 6, 7, 8, 9These are the only digits allowed to be used repetitively to represent an amount in decimal system.As such, binary, octal and hexadecimal has these symbols as their base digits:-

Binary (r = 2) : 0, 1 (commonly called bits)Octal (r = 8) : 0, 1, 2, 3, 4, 5, 6, 7Hexadecimal (r = 16) : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f

Number representation for 1st 17 value of different radix.R = 10R = 8R = 2R = 16000011112210233113441004551015661106771117810100089111001910121010A11131011B12141100C13151101D14161110E15171111F16201000010Why learn number systems???Computers only understand binary (e.g. 1=HIGH/ON; 0=LOW/OFF)Number systems are used to represent the binary number.For e.g.:Colors (WWW, Photoshop/GIMP etc)Assembly languageImagine writing 111111111111111111111111 for color white instead of FFFFFF or 255,255,255.Number may have both integer and fractional part, which are separated by a radix point (.).

Number may also be represented in positional or polynomial notations.

Positional NotationDigits position indicates its relative weight or significance. N=(an-1an-2 a1a0 . a-1a-2 a-m )rMost significant digitLeast significant digitPolynomial Notation123.35 = 100 + 20 + 3 + 0.3 +0.05 = 1 x 100 + 2 x 10 + 3 x 1 + 3 x 0.1 + 5 x 0.01 = 1 x 102 + 2 x 101 + 3 x 100 +3 x 10-1 + 5 x 10-2

Each digit resides in a weighted positionThe weight of each position is a power of the radix (radix 10 in this case)N = ai rii=-mn-1Eg:1011.112 = 1x23 + 0x22 + 1x21 + 1x20 +1x2-1 + 1x2-2Base ConversionBase r DecimalThe conversion of a number in base r to decimal is done by expanding the number in polynomial notation and adding all the terms, e.g convert (101)2 to decimal system:

(101)2 = 1x22 + 0x21 + 1x20 = (5)10

Convert (274)8 to base 10

Convert (1F.3)16 to base 10Decimal Base rTwo methods: Integer partFractional PartInteger Partdivide the number and all successive quotients by r and then groups the remainders. E.g: convert (41)10 to binary:

41Remainder41/2 =20 120/2 = 10 0Successive quotient 10/2 =5 0 (quotient must be whole 5/2 = 2 1number) 2/2 =1 01/2 =0 1 1 0 1 0 0 1

MSB LSBThus , (41)10 = (101001)2Fractional Partmultiply the number by r and group the integers instead of remainders. new fraction is then multiplied by r again to obtain another integer and new fraction. The procedure is repeated until the fraction becomes 0 or the number of digits has sufficient accuracyE.g. Convert (0.6875)10 to binary:

Integer

Fraction

Coefficient

0.6875 x 2 =1.37501+0.3750

a-1 = 1

0.3750 x 2 = .75000+0.7500

a-2 = 0

0.7500 x 2 =1.5001+0.5000

a-3 = 1

0.5000 x 2 =1.0001+0.0000

a-4 = 1

Therefore, (0.6875)10 = (0.1011)2Zero fractionBase Conversion (Binary-Octal-Hex)To convert a binary number to octal or hexadecimal, we can partition and group binary numbers to three digits (for binary octal) and four digits (binaryhexadecimal) starting from the radix point.

E.g. convert 10110001101011.1111001 to octal and hexadecimal system.

Partition & group to 3 bits: 0 10 110 001 101 011 . 111 100 100Octal equivalent of each binary group: 2 6 1 5 3 . 7 4 4So, (10110001101011.11110010)2 = ( 26153.744)8Binary OctalBinary HexadecimalPartition & group to 4 bits: 0010 1100 0110 1011 . 1111 0010 Hexadecimal equivalent of each binary group: 2 C 6 B . F 2So, (10110001101011.11110010)2 = ( 2C6B.F2)16Hexa, Octal BinaryTo convert from Hexadecimal or Octal system to Binary system, reverse the process above:

Convert each hexadecimal digit into 4 bits binary number. The number in Hexadecimal system and the Binary system must be equivalent when converted in Decimal system.Convert each octal digit into 3 bits binary number. The number in Octal system and the Binary system must be equivalent when converted in Decimal system.

ARITHMETICIntroExample of Decimal Addition 11 8+ 1 9 3 7Example of Decimal Subtraction 1 108- 9 9

8+9 = 7 carry 1borrowBinary Addition/SubtractionAddition TableA + B

Subtraction TableA - BABCarry0000011010101101ABSubtBorrow0000110010100111 1 1+1 1 0 100-1 1Binary Addition ExampleAdd the four numbers: (101101)2, (110101)2, (001101)2 and (010001)210101100101111001110101001101+01000110000000Note: 1 + 1 + 1 + 1 = 100Binary Subtraction ExampleSubtract (10111)2 from (1001101)2111001000110011001-10111110110Binary Multiplication/DivisionMultiplication TableA B

DivisionFollows the same as division in decimalAB000010100111Binary Multiplication ExampleMultiply (10111)2 by (1010)21011110101000001101111000001011111100110Binary Division ExampleDivide (1110111)2 by (1001)21101100111101111001101110011011100110RemainderSigned NumberSigned Binary NumbersSo far, we have only considered unsigned numbers. But digital systems must be able to handle positive and negative numbers.Signed numbers consists of sign and magnitude info.

Sign = 0 +ve Sign = 1 -veThree forms:Sign magnitude1s complement2s complementSignMagnitude RepresentationSign-Magnitude FormSimple, but least used.Left most bit sign bit. The rest are magnitudeFor eg, +25 in 8-bit sign-magnitude form:

And -25 in the same form:

-ve number has the same magnitude bits as +ve, but sign bit =1.00011001100110011s Complement Form+ve numbers is represented the same as in sign magnitude form-ve numbers is the 1s complement of the positive number (invert all bits)For eg, +25 in 8-bit 1s complement:

-25 in 8-bit 1s complement:

0001100100011001+25Invert bits11100110-252s Complement Form+ve numbers is represented the same as in sign magnitude form-ve numbers is the 2s complement of the positive number (1s comp. than add 1)For eg, +25 in 8-bit 2s complement:

-25 in 8-bit 2s complement:

0001100100011001+251s comp. + 111100111-25Decimal Number Value of Signed NumbersSign MagnitudeSign bit and calculate decimal of magnitude1s complementAdd terms in polynomial form with ve weight on the sign bit. Then, plus 12s complementAdd terms in polynomial form with ve weight on the sign bit.Decimal Number Value of Signed Numbers (cont.)Determine the decimal value of the 8-bit binary signed number 10010101 expressed in:Sign magnitude form1s complement form2s complement formDecimal Number Value of Signed Numbers (cont.)10010101 as Sign magnitudeSign bit = 1, so negativeMagnitude = 0010101.

= - (16 + 4 + 1)= - 21

001010126252423222120001 2401 2201 20Decimal Number Value of Signed Numbers (cont.)10010101 as 1s complement form

= -128 + 16 + 4 + 1 + 1= -106

10010101-27262524232221201 -27001 2401 2201 20Negative weightfor sign bitDecimal Number Value of Signed Numbers (cont.)10010101 as 2s complement form

= -128 + 16 + 4 + 1= -107

10010101-27262524232221201 -27001 2401 2201 20Negative weightfor sign bitSigned Numbers ArithmeticOnly 2s complement will be discussed since it is the most widely used.Range of 2s complement signed number:Range = -(2n-1) to (2n-1 1); n=no. of bitsFor n=8,Range = -128 to 127= (10000000)2cns to (01111111) 2cnsSigned Numbers Arithmetic (cont.)Addition (A = B + C)Both positive numbersPositive number > negative numberNegative number > positive numberBoth negativeSigned Numbers Arithmetic (cont.)Both positive numbers addition7 + 4+ve > -ve numbers15 +(- 6)00000111+000001000000101100001111+11111010100001001Discard carrySigned Numbers Arithmetic (cont.)-ve > +ve number16 + -24Both ve numbers- 5 + (-9)00010000+111010001111100011111011+11110111111110010Discard carrySigned Numbers Arithmetic (cont.)Overflow conditionWhen two numbers added, result may exceed the range.Can only occur when both +ve or both ve numbersIndicated by an incorrect sign bitFor eg, 8-bit binary 2s complement,125 + 58 = 18301111101+0011101010110111 +ve plus +ve, but ve result 8-bit range = -128 to 127.But answer exceedsHence, OVERFLOW condition occurredSigned Numbers Arithmetic (cont.)Subtraction (A = B C)Computation is treated as A = B + (-C)Sign of C is changed by taking its 2s complement For e.g., 8-bit binary 2s comp:8 3 = 8 + (-3)

8 = (00001000)2cns3 = (00000011)2cns-3 = (11111101)2cns

So, (00001000)2cns - (00000011)2cns = (00000101)2cns00001000+11111101100000101For e.g., 8-bit binary 2s comp:12 (-9) = 12 + (9)

12 = (00001100)2cns-9 = (11110111)2cns9 = (00001001)2cns

So, (00001100)2cns (11110111)2cns = (00010101)2cns00001100+0000100100010101Computer codesBinary Coded Decimal (BCD)A way to express EACH decimal digits (0 to 9), with binary code.Uses 4 bits binary for each decimal digit

Invalid codes : 1010 to 1111For e.g., Encode 159710 in BCD:

Decimal0123456789BCD000000010010001101000101011001111000100115970001010110010111So, 159710 = 0001 0101 1001 0111BCDBCD AdditionAdd the two BCD numbers as in binaryIf 4-bit sum 9, valid numberIf 4 bit sum > 9 OR carry generated, invalid. Add 6 (0110BCD) to the 4-bit sum. If carry is generated when adding 6, carry to next 4-bit sumBCD Addition (cont.)490 + 4970100110010000+0100100101111001100100111+011010011000011198767 + 5701100111+010101111101111110+0110+0110000100100100124Gray CodeExhibits only a single bit change from one code to the next sequence.No specific weight assigned to bit positionsImportant for applications like shaft position encoder that reduces error due to high bit changesGray Code (cont.)DecimalBinaryGray Code000000000100010001200100011300110010401000110501010111601100101701110100810001100910011101101010111111101111101211001010131101101114111010011511111000Red marks bit changeAmerican Standard Code for Information Interchange (ASCII)Alphanumeric code used in most computers and other electronics devicesAmerican Standard Code for Information Interchange (ASCII)

American Standard Code for Information Interchange (ASCII)