You are on page 1of 10

EE-314

Spring 2001

Number Systems, Base Conversions, and Computer Data Representation

Decimal and Binary Numbers When we write decimal (base 10) numbers, we use a positional notation s stem! Each digit is multiplied b an appropriate power o" 10 depending on its position in the number# $or e%ample# 843 = 8 x 102 + 4 x 101 + 3 x 100 = 8 x 100 + 4 x 10 + 3 x 1 = 800 + 40 + 3 $or whole numbers, the rightmost digit position is the one&s position (10 0 ' 1)! (he numeral in that position indicates how man ones are present in the number! (he ne%t position to the le"t is ten&s, then hundred&s, thousand&s, and so on! Each digit position has a weight that is ten times the weight o" the position to its right! )n the decimal number s stem, there are ten possible *alues that can appear in each digit position, and so there are ten numerals re+uired to represent the +uantit in each digit position! (he decimal numerals are the "amiliar ,ero through nine (0, 1, 2, 3, 4, -, ., /, 0, 1)! )n a positional notation s stem, the number base is called the radi%! (hus, the base ten s stem that we normall use has a radi% o" 10! (he term radi% and base can be used interchangeabl ! When writing numbers in a radi% other than ten, or where the radi% isn&t clear "rom the conte%t, it is customar to speci" the radi% using a subscript! (hus, in a case where the radi% isn&t understood, decimal numbers would be written li2e this# 12710 1110 567310

3enerall , the radi% will be understood "rom the conte%t and the radi% speci"ication is le"t o""! (he binar number s stem is also a positional notation numbering s stem, but in this case, the base is not ten, but is instead two! Each digit position in a binar number represents a power o" two! So, when we write a binar number, each binar digit is multiplied b an appropriate power o" 2 based on the position in the number# $or e%ample# 101101 = 1 x 25 + 0 x 24 + 1 x 23 + 1 x 22 + 0 x 21 + 1 x 20 = 1 x 32 + 0 x 16 + 1 x 8 + 1 x 4 + 0 x 2 + 1 x 1 = 32 + 8 + 4 + 1 )n the binar number s stem, there are onl two possible *alues that can appear in each digit position rather than the ten that can appear in a decimal number! 4nl the numerals 0 and 1 are used in binar numbers! (he term 5bit& is a contraction o" the words 5binar & and 5digit&, and when tal2ing about binar numbers the terms bit and digit can be used interchangeabl ! When tal2ing about binar numbers, it is o"ten necessar to tal2 o" the number o" bits used to store or represent the number! (his merel describes the number o" binar digits that would be re+uired to write the number! (he number in the abo*e e%ample is a . bit number! (he "ollowing are some additional e%amples o" binar numbers# 1011012 112 101102

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 the decimal *alue o" each binar digit position containing a 1 and add them up! $or e%ample# con*ert 101102 to decimal!

1 0 1 1 0 \ \ \___________1 x 21 = 2 \ \____________1 x 22 = 4 \_______________1 x 24 = 16 22 7nother e%ample# con*ert 110112 to decimal x x x x 20 21 23 24 = 1 = 2 = 8 = 16 27

1 1 0 1 1 \ \ \ \_________1 \ \ \__________1 \ \_____________1 \______________1

(he method "or con*erting a decimal number to binar is one that can be used to con*ert "rom decimal to an number base! )t in*ol*es using successi*e di*ision b the radi% until the di*idend reaches 0! 7t each di*ision, the remainder pro*ides a digit o" the con*erted number starting with the least signi"icant digit! 7n e%ample o" the process# 37 / 2 = 18 18 / 2 = / 2 = 4 4 / 2 = 2 2 / 2 = 1 1 / 2 = 0 con*ert 3/10 to binar remainder remainder remainder remainder remainder remainder 1 0 1 0 0 1 (least significant digit)

(m!st significant digit)

"#e res$lting %inar& n$m%er is' 100101 7nother e%ample# 3 46 23 11 5 2 1 / / / / / / / 2 2 2 2 2 2 2 = = = = = = = 46 23 11 5 2 1 0 con*ert 1310 to binar remainder remainder remainder remainder remainder remainder remainder 1 0 1 1 1 0 1 (least significant digit)

(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 s stems is base 1.! (his number s stem is called he%adecimal, and each digit position represents a power o" 1.! $or an number base greater than ten, a problem occurs because there are more than ten s mbols needed to represent the numerals "or that number base! )t is customar in these cases to use the

EE-314

Spring 2001

ten decimal numerals "ollowed b the letters o" the alphabet beginning with 7 to pro*ide the needed numerals! Since the he%adecimal s stem is base 1., there are si%teen numerals re+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 the numbers 2 and 1.! Si%teen is a power o" 2 (1. ' 24)! 8ecause o" this relationship, "our digits in a binar number can be represented with a single he%adecimal digit! (his ma2es con*ersion between binar and he%adecimal numbers *er eas , and he%adecimal can be used to write large binar numbers with much "ewer digits! When wor2ing with large digital s stems, such as computers, it is common to "ind binar numbers with 0, 1. and e*en 32 digits! Writing a 1. or 32 bit binar number would be +uite tedious and error prone! 8 using he%adecimal, the numbers can be written with "ewer digits and much less li2elihood 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 each group contains 4 digits! $or each "our digit group, con*ert the 4 bit binar number into an e+ui*alent he%adecimal digit! $or e%ample# 6on*ert the binar number 10110101 to a he%adecimal number 9i*ide into groups "or 4 digits 6on*ert each group to he% digit 1011 0101 * 5 *516

7nother e%ample# 6on*ert the binar number 0110101110001100 to he%adecimal 9i*ide into groups o" 4 digits 6on*ert each group to he% digit 0110 1011 1000 1100 6 * 8 + 6*8+16

(o con*ert a he%adecimal number to a binar number, con*ert each he%adecimal digit into a group o" 4 binar digits! E%ample# 6on*ert the he% number 3/4$ into binar 6on*ert the he% digits to binar 3 7 4 . 0011 0111 0100 1111 00110111010011112

(here are se*eral wa s 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 ma2es it absolutel clear that numbers are represented in he%adecimal, no indicator is used! )n much written material where the conte%t doesn&t ma2e it clear what the radi% is, the numeric subscript 1. "ollowing the he%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/$, or 0%1234, or 0%7$! )n assembler programming languages that "ollow the )ntel st le, a he%adecimal constant begins with a numeric character (so that the assembler can distinguish it "rom a *ariable name), a leading 50& being used i" necessar , with the letter 5h& su""i%ed onto the number! )n )ntel

EE-314

Spring 2001

st le assembler "ormat# 3/1$h and 0$786h are *alid he%adecimal constants! ;ote that# 73/h isn&t a *alid he%adecimal constant! )t doesn&t begin with a numeric character, and so will be ta2en b the assembler as a *ariable name! )n assembler programming languages that "ollow the <otorola st le, he%adecimal constants begin with a 5=& character! So in this case# =3/1$ or =$786 or =01 are *alid he%adecimal constants! Binary Coded Decimal Numbers 7nother number s stem that is encountered occasionall is 8inar 6oded 9ecimal! )n this s stem, numbers are represented in a decimal "orm, howe*er each decimal digit is encoded using a "our bit binar number! $or e%ample# (he decimal number 13. would be represented in 869 as "ollows# 136 = 0001 0011 0110 1 3 6 6on*ersion o" numbers between decimal and 869 is +uite simple! (o con*ert "rom decimal to 869, simpl write down the "our bit binar pattern "or each decimal digit! (o con*ert "rom 869 to decimal, di*ide the number into groups o" 4 bits and write down the corresponding decimal digit "or each 4 bit group! (here are a couple o" *ariations on the 869 representation, namel pac2ed and unpac2ed! 7n unpac2ed 869 number has onl a single decimal digit stored in each data b te! )n this case, the decimal digit will be in the low "our bits and the upper 4 bits o" the b te will be 0! )n the pac2ed 869 representation, two decimal digits are placed in each b te! 3enerall , the high order bits o" the data b te contain the more signi"icant decimal digit! 7n e%ample# (he "ollowing is a 1. bit number encoded in pac2ed 869 "ormat# 01010110 10010011 (his is con*erted to a decimal number as "ollows# 0101 0110 1001 0011 5 6 3 (he *alue is -.13 decimal 7nother e%ample# (he same number in unpac2ed 869 (re+uires 32 bits) 00000101 00000110 00001001 00000011 5 6 3 (he use o" 869 to represent numbers isn&t as common as binar in most computer s stems, as it is not as space e""icient! )n pac2ed 869, onl 10 o" the 1. possible bit patterns in each 4 bit unit are used! )n unpac2ed 869, onl 10 o" the 2-. possible bit patterns in each b te are used! 7 1. bit +uantit can represent the range 0-.--3- in binar , 0-1111 in pac2ed 869 and onl 0-11 in unpac2ed 869! Fixed Precision and Over low! So "ar, in tal2ing about binar numbers, we ha*en&t considered the ma%imum si,e o" the number! We ha*e assumed that as man bits are a*ailable as needed to represent the number! )n most computer s stems, this isn&t the case! ;umbers in computers are t picall represented using a "i%ed number o" bits! (hese si,es are t picall 0 bits, 1. bits, 32 bits, .4 bits and 00 bits! (hese si,es are generall a multiple o" 0, as most computer memories are organi,ed on an 0 bit b te basis! ;umbers in which a speci"ic number o" bits are used to represent the *alue are called "i%ed precision numbers! When a speci"ic number o" bits are used to represent a number, that determines the range o" possible *alues that can be represented! $or e%ample, there are 2-. possible combinations o" 0 bits, there"ore an 0 bit number can represent 2-. distinct numeric

EE-314

Spring 2001

*alues and the range is t picall considered to be 0-2--! 7n number larger than 2-- can&t be represented using 0 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) the concept o" o*er"low must be considered! 7n o*er"low occurs when the result o" a calculation can&t be represented with the number o" bits a*ailable! $or e%ample when adding the two eight bit +uantities# 1-0 : 1/0, the result is 320! (his is outside the range 0-2--, and so the result can&t be represented using 0 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 to be 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 precision binar number is used to hold onl positi*e *alues, it is said to be unsigned! )n this case, the range o" positi*e *alues that can be represented is 0-2n-1, where n is the number o" bits used! )t is 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 is used to represent negati*e *alues! (here are se*eral wa s that signed numbers can be represented in binar , but the most common representation used toda is called two&s complement! (he term two&s complement is somewhat ambiguous, in that it is used in two di""erent wa s! $irst, as a representation, two&s complement is a 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 be per"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 the two&s complement representation, the high order bit o" the number indicates the sign! )" the sign bit is 0, the number is positi*e, and i" the sign bit is 1, the number is negati*e! $or positi*e numbers, the rest o" the bits hold the true magnitude o" the number! $or negati*e numbers, the lower order bits hold the complement (or bitwise in*erse) o" the magnitude o" the number! )t is important 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 in the arithmetic-logic unit o" a computer&s 6>?! ?sing a two&s complement representation, all o" the arithmetic operations can be per"ormed b the same hardware whether the numbers are considered to be unsigned or signed! (he bit operations per"ormed are identical, the di""erence comes "rom the interpretation o" the bits! (he interpretation o" the *alue will be di""erent depending on whether the *alue is considered to be unsigned or signed! $or e%ample# $ind the 2&s complement o" the "ollowing 0 bit number 00101001 11010110 + 00000001 = 11010111 $irst, in*ert the bits (hen, add 1

(he 2&s complement o" 00101001 is 11010111 7nother e%ample# $ind the 2&s complement o" the "ollowing 0 bit number 10110101

EE-314 )n*ert the bits then add 1

Spring 2001

01001010 + 00000001 = 01001011

(he counting se+uence "or an eight bit binar *alue using 2&s complement representation appears as "ollows# 01111111 /$h 12/ largest magnitude positi*e number 01111110 /Eh 12. 01111101 /9h 12@ 00000011 03h 00000010 02h 00000001 01h 00000000 00h 11111111 0$$h -1 11111110 0$Eh -2 11111101 0$9h -3 @ 10000010 02h -12. 10000001 01h -12/ 10000000 00h -120 largest magnitude negati*e number ;otice in the abo*e se+uence that counting up "rom 0, when 12/ is reached, the ne%t binar pattern in the se+uence corresponds to -120! (he *alues Aump "rom the greatest positi*e number to the greatest negati*e number, but that the se+uence is as e%pected a"ter that! (i!e! adding 1 to B120 ields B12/, and so on!)! When the count has progressed to 0$$h (or the largest unsigned magnitude possible) the count wraps around to 0! (i!e! adding 1 to B1 ields 0)! $SC%% C&aracter 'ncodin" (he name 7S6)) is an acron m "or# 7merican Standard 6ode "or )n"ormation )nterchange! )t is a character coding standard de*eloped se*eral decades ago to pro*ide a standard wa "or digital machines to encode characters! (he 7S6)) code pro*ides a mechanism "or encoding alphabetic characters, numeric digits, and punctuation mar2s "or use in representing te%t and numbers written using the Coman alphabet! 7s originall designed, it was a se*en bit code! (he se*en bits allows the representation o" 120 uni+ue characters, all o" the alphabet, numeric digits and standard English punctuation mar2s are encoded! (he 7S6)) standard was later e%tended to an eight bit code (which allows 2-. uni+ue code patterns) and *arious additional s mbols were added, including characters with diacritical mar2s (such as accents) used in European languages which don&t appear in English! (here are also numerous non-standard e%tensions to 7S6)) gi*ing di""erent encodings to the upper 120 character codes than the standard! $or e%ample, (he character 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, and could 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 30h 2) (he upper case alphabetic characters are se+uential beginning at 41h 3) (he lower case alphabetic characters are se+uential beginning at .1h 4) (he "irst 31 characters (codes 0-2$h) and /$h are control characters! (he do not ha*e a standard s mbol (gl ph) associated with them! (he are used "or carriage control, and protocol purposes! (he include 09h (6C or carriage return), 07h (D$ or line "eed), 06h ($$ or "orm "eed), 00h (8S or bac2space)! -) <ost 2e boards generate the control characters b holding down a control 2e (6(CD) and simultaneousl pressing an alphabetic character 2e ! (he control code will ha*e the same *alue as the lower "i*e bits o" the alphabetic 2e pressed! So, "or e%ample, the

EE-314

Spring 2001 control character 09h is carriage return! )t can be generated b pressing 6(CD-<! (o get the "ull 31 control characters a "ew at the upper end o" the range are generated b pressing 6(CD and a punctuation 2e in combination! $or e%ample, the ES6 (escape) character is generated b pressing 6(CD-E (le"t s+uare brac2et)!

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 case letters 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 lower case in*ol*es either adding or subtracting 20h to the character code! (o con*ert a lower case letter 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 alphabetic character be"ore per"orming the addition or subtraction! 4rdinaril , a chec2 should be made that the code is in the range 41hB-7h "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-31h! 6on*ersion between an 7S6)) encoded digit and an unpac2ed 869 digit can be accomplished b adding 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 is necessar 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-31h!

EE-314

Spring 2001

Binary, BCD and Hexadecimal Number )ables


Powers o *+ 20!!!!!!!!!!!!!!!!!!!!!!!1 21!!!!!!!!!!!!!!!!!!!!!!!2 22!!!!!!!!!!!!!!!!!!!!!!!4 23!!!!!!!!!!!!!!!!!!!!!!!0 24!!!!!!!!!!!!!!!!!!!!!1. 2-!!!!!!!!!!!!!!!!!!!!!32 2.!!!!!!!!!!!!!!!!!!!!!.4 2/!!!!!!!!!!!!!!!!!!!120 20!!!!!!!!!!!!!!!!!!!2-. 21!!!!!!!!!!!!!!!!!!!-12 210!!!!!!!!!!!!!!!!!1024 211!!!!!!!!!!!!!!!!!2040 212!!!!!!!!!!!!!!!!!401. 213!!!!!!!!!!!!!!!!!0112 214!!!!!!!!!!!!!!!1.304 21-!!!!!!!!!!!!!!!32/.0 21.!!!!!!!!!!!!!!!.--3. Hexadecimal Di"its 0!!!!!!!!!!!!!!!!!!!!!!!0 1!!!!!!!!!!!!!!!!!!!!!!!1 2!!!!!!!!!!!!!!!!!!!!!!!2 3!!!!!!!!!!!!!!!!!!!!!!!3 4!!!!!!!!!!!!!!!!!!!!!!!4 -!!!!!!!!!!!!!!!!!!!!!!!.!!!!!!!!!!!!!!!!!!!!!!!. /!!!!!!!!!!!!!!!!!!!!!!!/ 0!!!!!!!!!!!!!!!!!!!!!!!0 1!!!!!!!!!!!!!!!!!!!!!!!1 10!!!!!!!!!!!!!!!!!!!!!!!7 11!!!!!!!!!!!!!!!!!!!!!!!8 12!!!!!!!!!!!!!!!!!!!!!!!6 13!!!!!!!!!!!!!!!!!!!!!!!9 14!!!!!!!!!!!!!!!!!!!!!!!E 1-!!!!!!!!!!!!!!!!!!!!!!!$ BCD Di"its 0!!!!!!!!!!!!!!!!!0000 1!!!!!!!!!!!!!!!!!0001 2!!!!!!!!!!!!!!!!!0010 3!!!!!!!!!!!!!!!!!0011 4!!!!!!!!!!!!!!!!!0100 -!!!!!!!!!!!!!!!!!0101 .!!!!!!!!!!!!!!!!!0110 /!!!!!!!!!!!!!!!!!0111 0!!!!!!!!!!!!!!!!!1000 1!!!!!!!!!!!!!!!!!1001

EE-314

Spring 2001

',uivalent Numbers in Decimal, Binary and Hexadecimal Notation+ 9ecimal 0 1 2 3 4 5 6 7 8 10 11 12 13 14 15 16 17 31 32 63 64 65 127 128 12 255 256 32767 32768 65535 8inar 00000000 00000001 00000010 00000011 00000100 00000101 00000110 00000111 00001000 00001001 00001010 00001011 00001100 00001101 00001110 00001111 00010000 00010001 00011111 00100000 00111111 01000000 01000001 01111111 10000000 10000001 11111111 0000000100000000 0111111111111111 1000000000000000 1111111111111111 Fe%adecimal 00 01 02 03 04 05 06 07 08 0 0) 0* 0+ 0, 00. 10 11 1. 20 3. 40 41 7. 80 81 .. 0100 7... 8000 ....

EE-314

Spring 2001

$SC%% C&aracter Set


Hi"& Order Bits 0000 0001 0010 . * /01 ,123ace 278 ,+1 9 2"< ,+2 = -"< ,+3 ? -7" ,+4 @ -/: /)A B )+A 2C/ D *-1 -"* G *2 +)/ ( 8" -K ) 1. 20* M E" -2+ + .. .2 ( +> H2 X 27 >2 [ 2L 02 /

Low Order Bits 0000 0001 . 0010 * 0011 / 0100 0 0101 1 0110 2 0111 3 1000 4 1001 5 1010 $ 1011 B 1100 C 1101 D 1110 ' 1111 F

0011 / 0 1 2 3 4 5 6 7 8 ' R V = \ _

0100 0 4 ) * + , . H 8 L N A 1 K / 7

0101 1 5 : > 2 " 0 E I < C O S \ Y ] _

0110 2 6 a % c d e f g # i P T l m n !

0111 3 3 ; r s t $ F J x & Q U W Z ^ ,-1

10

You might also like