number systems and conversion

Upload: madhusiva

Post on 03-Jun-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 Number Systems and Conversion

    1/10

  • 8/12/2019 Number Systems and Conversion

    2/10

    EE-314 Spring 2001

    Conversion between Decimal and Binary

    6on*erting a number "rom binar to decimal is +uite eas! 7ll that is re+uired is to "ind thedecimal *alue o" each binar digit position containing a 1 and add them up!

    $or e%ample# con*ert 101102to decimal!

    1 0 1 1 0\ \ \___________1 x 21= 2 \ \____________1 x 22= 4 \_______________1 x 24= 16

    22

    7nother e%ample# con*ert 110112to decimal

    1 1 0 1 1\ \ \ \_________1 x 20= 1 \ \ \__________1 x 21= 2 \ \_____________1 x 23= 8

    \______________1 x 2

    4

    = 1627

    he method "or con*erting a decimal number to binar is one that can be used to con*ert "romdecimal to an number base! t in*ol*es using successi*e di*ision b the radi% until the di*idendreaches 0! 7t each di*ision, the remainder pro*ides a digit o" the con*erted number starting withthe least signi"icant digit!

    7n e%ample o" the process# con*ert 3/10to binar

    37 / 2 = 18 remainder 1 (least significant digit)18 / 2 = remainder 0 / 2 = 4 remainder 14 / 2 = 2 remainder 0

    2 / 2 = 1 remainder 01 / 2 = 0 remainder 1 (m!st significant digit)

    "#e res$lting %inar& n$m%er is' 100101

    7nother e%ample# con*ert 310to binar

    3 / 2 = 46 remainder 1 (least significant digit)46 / 2 = 23 remainder 023 / 2 = 11 remainder 111 / 2 = 5 remainder 15 / 2 = 2 remainder 12 / 2 = 1 remainder 0

    1 / 2 = 0 remainder 1 (m!st significant digit)

    "#e res$lting %inar& n$m%er is' 1011101

    Hexadecimal Numbers

    n addition to binar, another number base that is commonl used in digital sstems is base 1.!his number sstem is called he%adecimal, and each digit position represents a power o" 1.! $oran number base greater than ten, a problem occurs because there are more than ten smbolsneeded to represent the numerals "or that number base! t is customar in these cases to use the

    2

  • 8/12/2019 Number Systems and Conversion

    3/10

    EE-314 Spring 2001

    ten decimal numerals "ollowed b the letters o" the alphabet beginning with 7 to pro*ide theneeded numerals! Since the he%adecimal sstem is base 1., there are si%teen numeralsre+uired! he "ollowing are the he%adecimal numerals#

    0 1 2 3 4 5 6 7 8 * , - .

    he "ollowing are some e%amples o" he%adecimal numbers#

    1016 4716 3.16 03.16

    he reason "or the common use o" he%adecimal numbers is the relationship between thenumbers 2 and 1.! Si%teen is a power o" 2 (1. ' 24)! 8ecause o" this relationship, "our digits in abinar number can be represented with a single he%adecimal digit! his maes con*ersionbetween binar and he%adecimal numbers *er eas, and he%adecimal can be used to write largebinar numbers with much "ewer digits! When woring with large digital sstems, such ascomputers, it is common to "ind binar numbers with , 1. and e*en 32 digits! Writing a 1. or 32bit binar number would be +uite tedious and error prone! 8 using he%adecimal, the numberscan be written with "ewer digits and much less lielihood o" error!

    o con*ert a binar number to he%adecimal, di*ide it into groups o" "our digits starting with the

    rightmost digit! " the number o" digits isn&t a multiple o" 4, pre"i% the number with 0&s so that eachgroup contains 4 digits! $or each "our digit group, con*ert the 4 bit binar number into ane+ui*alent he%adecimal digit!

    $or e%ample# 6on*ert the binar number 10110101to a he%adecimal number

    9i*ide into groups "or 4 digits 1011 01016on*ert each group to he% digit * 5

    *516

    7nother e%ample# 6on*ert the binar number 0110101110001100to he%adecimal

    9i*ide into groups o" 4 digits 0110 1011 1000 1100

    6on*ert each group to he% digit 6 * 8 6*816

    o con*ert a he%adecimal number to a binar number, con*ert each he%adecimal digit into agroup o" 4 binar digits!

    E%ample# 6on*ert the he% number 3/4$ into binar

    3 7 4 .

    6on*ert the he% digits to binar 0011 0111 0100 111100110111010011112

    here are se*eral was in common use to speci" that a gi*en number is in he%adecimal

    representation rather than some other radi%! n cases where the conte%t maes it absolutel clearthat numbers are represented in he%adecimal, no indicator is used! n much written materialwhere the conte%t doesn&t mae it clear what the radi% is, the numeric subscript 1. "ollowing thehe%adecimal number is used! n most programming languages, this method isn&t reall "easible,so there are se*eral con*entions used depending on the language! n the 6 and 6:: languages,he%adecimal constants are represented with a 50%& preceding the number, as in# 0%31/$, or0%1234, or 0%7$! n assembler programming languages that "ollow the ntel stle, a he%adecimalconstant begins with a numeric character (so that the assembler can distinguish it "rom a *ariablename), a leading 50& being used i" necessar, with the letter 5h& su""i%ed onto the number! n ntel

    3

  • 8/12/2019 Number Systems and Conversion

    4/10

    EE-314 Spring 2001

    stle assembler "ormat# 3/1$h and 0$786h are *alid he%adecimal constants! ;ote that# 73/hisn&t a *alid he%adecimal constant! t doesn&t begin with a numeric character, and so will be taenb the assembler as a *ariable name! n assembler programming languages that "ollow the

  • 8/12/2019 Number Systems and Conversion

    5/10

    EE-314 Spring 2001

    *alues and the range is tpicall considered to be 0-2! 7n number larger than 2 can&t berepresented using bits! Similarl, 1. bits allows a range o" 0-.3!

    When "i%ed precision numbers are used, (as the are in *irtuall all computer calculations) theconcept o" o*er"low must be considered! 7n o*er"low occurs when the result o" a calculation can&tbe represented with the number o" bits a*ailable! $or e%ample when adding the two eight bit+uantities# 10 : 1/0, the result is 320! his is outside the range 0-2, and so the result can&t berepresented using bits! he result has o*er"lowed the a*ailable range! When o*er"low occurs,the low order bits o" the result will remain *alid, but the high order bits will be lost! his results in a*alue that is signi"icantl smaller than the correct result!

    When doing "i%ed precision arithmetic (which all computer arithmetic in*ol*es) it is necessar tobe conscious o" the possibilit o" o*er"low in the calculations!

    Si"ned and #nsi"ned Numbers!

    So "ar, we ha*e onl considered positi*e *alues "or binar numbers! When a "i%ed precisionbinar number is used to hold onl positi*e *alues, it is said to be unsigned! n this case, therange o" positi*e *alues that can be represented is 0-2n-1, where n is the number o" bits used! tis also possible to represent signed (negati*e as well as positi*e) numbers in binar! n this case,

    part o" the total range o" *alues is used to represent positi*e *alues, and the rest o" the range isused to represent negati*e *alues!

    here are se*eral was that signed numbers can be represented in binar, but the most commonrepresentation used toda is called two&s complement! he term two&s complement is somewhatambiguous, in that it is used in two di""erent was! $irst, as a representation, two&s complement isa wa o" interpreting and assigning meaning to a bit pattern contained in a "i%ed precision binar+uantit! Second, the term two&s complement is also used to re"er to an operation that can beper"ormed on the bits o" a binar +uantit! 7s an operation, the two&s complement o" a number is"ormed b in*erting all o" the bits and adding 1! n a binar number being interpreted using thetwo&s complement representation, the high order bit o" the number indicates the sign! " the signbit is 0, the number is positi*e, and i" the sign bit is 1, the number is negati*e! $or positi*enumbers, the rest o" the bits hold the true magnitude o" the number! $or negati*e numbers, thelower order bits hold the complement (or bitwise in*erse) o" the magnitude o" the number! t isimportant to note that two&s complement representation can onl be applied to "i%ed precision+uantities, that is, +uantities where there are a set number o" bits!

    wo&s complement representation is used because it reduces the comple%it o" the hardware inthe arithmetic-logic unit o" a computer&s 6>?! ?sing a two&s complement representation, all o" thearithmetic operations can be per"ormed b the same hardware whether the numbers areconsidered to be unsigned or signed! he bit operations per"ormed are identical, the di""erencecomes "rom the interpretation o" the bits! he interpretation o" the *alue will be di""erentdepending on whether the *alue is considered to be unsigned or signed!

    $or e%ample# $ind the 2&s complement o" the "ollowing bit number 00101001

    11010110 $irst, in*ert the bits+ 00000001 hen, add 1= 11010111

    he 2&s complement o" 00101001 is 11010111

    7nother e%ample# $ind the 2&s complement o" the "ollowing bit number 10110101

    5

  • 8/12/2019 Number Systems and Conversion

    6/10

    EE-314 Spring 2001

    01001010 n*ert the bits+ 00000001 then add 1= 01001011

    he counting se+uence "or an eight bit binar *alue using 2&s complement representation appearsas "ollows#

    01111111 /$h 12/ largest magnitude positi*e number 01111110 /Eh 12.01111101 /9h 12@00000011 03h00000010 02h00000001 01h00000000 00h11111111 0$$h -111111110 0$Eh -211111101 0$9h -3@10000010 2h -12.10000001 1h -12/10000000 0h -12 largest magnitude negati*e number

    ;otice in the abo*e se+uence that counting up "rom 0, when 12/ is reached, the ne%t binarpattern in the se+uence corresponds to -12! he *alues Aump "rom the greatest positi*e numberto the greatest negati*e number, but that the se+uence is as e%pected a"ter that! (i!e! adding 1 to

    B12 ields B12/, and so on!)! When the count has progressed to 0$$h (or the largest unsignedmagnitude possible) the count wraps around to 0! (i!e! adding 1 to B1 ields 0)!

    $SC%% C&aracter 'ncodin"

    he name 7S6 is an acronm "or# 7merican Standard 6ode "or n"ormation nterchange! t is acharacter coding standard de*eloped se*eral decades ago to pro*ide a standard wa "or digitalmachines to encode characters! he 7S6 code pro*ides a mechanism "or encoding alphabetic

    characters, numeric digits, and punctuation mars "or use in representing te%t and numberswritten using the Coman alphabet! 7s originall designed, it was a se*en bit code! he se*en bitsallows the representation o" 12 uni+ue characters, all o" the alphabet, numeric digits andstandard English punctuation mars are encoded! he 7S6 standard was later e%tended to aneight bit code (which allows 2. uni+ue code patterns) and *arious additional smbols wereadded, including characters with diacritical mars (such as accents) used in European languageswhich don&t appear in English! here are also numerous non-standard e%tensions to 7S6 gi*ingdi""erent encodings to the upper 12 character codes than the standard! $or e%ample, hecharacter set encoded into the displa card "or the original 8< >6 had a non-standard encoding"or the upper character set! his is a non-standard e%tension that is in *er wide spread use, andcould be considered a standard in itsel"!

    Some important things to note about 7S6 code#1) he numeric digits are encoded in se+uence starting at 30h2) he upper case alphabetic characters are se+uential beginning at 41h3) he lower case alphabetic characters are se+uential beginning at .1h4) he "irst 31 characters (codes 0-2$h) and /$h are control characters! he do not ha*e a

    standard smbol (glph) associated with them! he are used "or carriage control, andprotocol purposes! he include 09h (6C or carriage return), 07h (D$ or line "eed), 06h($$ or "orm "eed), 0h (8S or bacspace)!

    )

  • 8/12/2019 Number Systems and Conversion

    7/10

    EE-314 Spring 2001

    control character 09h is carriage return! t can be generated b pressing 6CD-

    Conversion Between #pper and (ower Case $SC%% (etters!

    ;otice on the 7S6 code chart that the uppercase letters start at 41h and that the lower caseletters begin at .1h! n each case, the rest o" the letters are se+uential and in alphabetic order!he di""erence between 41h and .1h is 20h! here"ore the con*ersion between upper and lowercase in*ol*es either adding or subtracting 20h to the character code! o con*ert a lower caseletter to upper case, subtract 20h, and con*ersel to con*ert upper case to lower case, add 20h!t is important to note that ou need to "irst ensure that ou do in "act ha*e an alphabeticcharacter be"ore per"orming the addition or subtraction! rdinaril, a chec should be made thatthe code is in the range 41hB7h "or upper case or .1h-/7h "or lower case!

    Conversion Between $SC%% and BCD!

    ;otice also on the 7S6 code chart that the numeric characters are in the range 30h-3h!6on*ersion between an 7S6 encoded digit and an unpaced 869 digit can be accomplished badding or subtracting 30h! Subtract 30h "rom an 7S6 digit to get 869, or add 30h to a 869 digit

    to get 7S6! 7gain, as with upper and lower case con*ersion "or alphabetic characters, it isnecessar to ensure that the character is in "act a numeric digit be"ore per"orming the subtraction!he digit characters are in the range 30h-3h!

    7

  • 8/12/2019 Number Systems and Conversion

    8/10

    EE-314 Spring 2001

    Binary, BCD and Hexadecimal Number )ables

    Powers o *+20!!!!!!!!!!!!!!!!!!!!!!!121!!!!!!!!!!!!!!!!!!!!!!!2

    22

    !!!!!!!!!!!!!!!!!!!!!!!423!!!!!!!!!!!!!!!!!!!!!!!24!!!!!!!!!!!!!!!!!!!!!1.2!!!!!!!!!!!!!!!!!!!!!322.!!!!!!!!!!!!!!!!!!!!!.42/!!!!!!!!!!!!!!!!!!!122!!!!!!!!!!!!!!!!!!!2.2!!!!!!!!!!!!!!!!!!!12

    210!!!!!!!!!!!!!!!!!1024211!!!!!!!!!!!!!!!!!204212!!!!!!!!!!!!!!!!!40.213!!!!!!!!!!!!!!!!!12214!!!!!!!!!!!!!!!1.3421!!!!!!!!!!!!!!!32/.21.!!!!!!!!!!!!!!!.3.

    Hexadecimal Di"its

    0!!!!!!!!!!!!!!!!!!!!!!!01!!!!!!!!!!!!!!!!!!!!!!!12!!!!!!!!!!!!!!!!!!!!!!!23!!!!!!!!!!!!!!!!!!!!!!!34!!!!!!!!!!!!!!!!!!!!!!!4!!!!!!!!!!!!!!!!!!!!!!!.!!!!!!!!!!!!!!!!!!!!!!!./!!!!!!!!!!!!!!!!!!!!!!!/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    10!!!!!!!!!!!!!!!!!!!!!!!711!!!!!!!!!!!!!!!!!!!!!!!812!!!!!!!!!!!!!!!!!!!!!!!613!!!!!!!!!!!!!!!!!!!!!!!914!!!!!!!!!!!!!!!!!!!!!!!E1!!!!!!!!!!!!!!!!!!!!!!!$

    BCD Di"its

    0!!!!!!!!!!!!!!!!!00001!!!!!!!!!!!!!!!!!00012!!!!!!!!!!!!!!!!!00103!!!!!!!!!!!!!!!!!00114!!!!!!!!!!!!!!!!!0100

    !!!!!!!!!!!!!!!!!0101.!!!!!!!!!!!!!!!!!0110/!!!!!!!!!!!!!!!!!0111!!!!!!!!!!!!!!!!!1000!!!!!!!!!!!!!!!!!1001

    8

  • 8/12/2019 Number Systems and Conversion

    9/10

    EE-314 Spring 2001

    'uivalent Numbers in Decimal, Binary and Hexadecimal Notation+

    9ecimal8inar Fe%adecimal

    0 00000000 001 00000001 012 00000010 023 00000011 034 00000100 045 00000101 056 00000110 067 00000111 078 00001000 08 00001001 0

    10 00001010 011 00001011 0*12 00001100 013 00001101 0,14 00001110 0-15 00001111 0.

    16 00010000 1017 00010001 1131 00011111 1.32 00100000 2063 00111111 3.64 01000000 4065 01000001 41127 01111111 7.128 10000000 8012 10000001 81255 11111111 ..256 0000000100000000 0100

    32767 0111111111111111 7...

    32768 1000000000000000 800065535 1111111111111111 ....

    9

  • 8/12/2019 Number Systems and Conversion

    10/10

    EE-314 Spring 2001

    $SC%% C&aracter Set

    Hi"& Order BitsLow Order

    Bits

    0000 0001 0010 0011 0100 0101 0110 0111- . * / 0 1 2 3

    0000 - ,- ace 0 0001 . ,1 9 1 : a ;0010 * "< ,2 2 * > % r0011 / -"< ,3 ? 3 c s0100 0 -" ,4 @ 4 , " d t0101 1 -: A B 5 - e $0110 2 A C D 6 . E f F0111 3 *- -"* G 7 H I g J1000 4 * ( 8 < # x1001 5 " -K ) L C i &

    1010 $ . * M ' N O P Q1011 B E" - + R A S T U1100 C .. . V \ l W1101 D > H X = K Y m Z1110 ' > [ ] n ^1111 F L / _ ! ,-

    10