You are on page 1of 14

15/06/2012

Biu

din th ng tin

tr n m y t nh

III. BIU DIN THNG TIN TRN MY TNH


1.Biu din cc k t Mt trong cc phng php biu din cc k t trong my tnh l thit k mt b m. ngha ca cch thit k ny l cc k t khc nhau s c c trng bi mt nhm bit duy nht khc nhau, bng cch ny thng tin s c m ha thnh mt chui bit trong b nh hoc cc thit b lu tr. Tuy nhin, s c nhiu b m khc nhau. gii quyt vn ny, Vin Chun Ha Hoa K (American National Standards Institute) a ra b m chun trong giao tip thng tin trn my tnh gi l b m ASCII (American Standard Code for Information Interchage) v tr thnh chun cng nghip cho cc nh sn xut my tnh. B m ny dng 7 bit biu din cc k t, tuy vy mi k t trong bng m ASCII vn chim ht mt byte khi thc hin trong b nh my tnh, bit d ra s b b qua hoc c dng cho biu din mt cho k t c bit. Trong bng m ASCII s bao gm cc k t ch hoa, thng, k t s, k t khong trng,... V d dy bit sau l biu din ca chui k t "Hi Sue "

Hin nay bng m ASCII vn l bng m c s dng nhiu nht. Mt bng m khc cng khng km phn c a chung l EBCDIC (Extended Binary Code Decimal Interchange Code) l b m mi k t c biu din vi 8 bit, b m ny ca cng ty IBM. 2. Biu din gi tr ca cc con s Mc d phng php lu tr thng tin nh l s m ha cc k t bng cc dy bit, nhng n dng nh khng hiu qu khi lu tr d liu thun s. Chng ta hy xem ti sao iu ny xy ra? Chng ta mun lu tr s 25, nu dng bng m ASCII biu din th mi k s s cn n mt byte lu tr do ta cn ti 16 bit lu tr. Hn th na, i vi cc con s ln hn mun lu tr ta phi cn phi dng t 16 bit tr ln. Mt phng php hiu qu hn lu tr gi tr cho vi d liu l s my tnh l dng h nh phn, phng php ny da trn v d sau: Mt ng h o kilomet ca xe, khi xe cn mi th ng h ch mc 0000000 Figure03_002.jpg (10518 bytes)

Mi s 0 c trng cho mt vng quay, vng quay s nhn ln lt cc con s 1, 2, 3, 4, 5, 6, 7, 8, 9. Khi xe bt u


www4.hcmut.edu.vn/~huynhqlinh/TinhocDC/THDC11/Bai01_3.htm

1/14

15/06/2012

Biu

din th ng tin

tr n m y t nh

chy th vng quay bn phi nht s bt thay i cho n khi ch s ng h l 00000009

Vo thi im tip theo vng quay phi nht s y vng quay k ln mt n v, kt qu l vng quay phi nht quay c mt vng v s tr v 0. Lc ch s ng h nh sau: 00000010

Khi xe tip tc chy v vng quay phi nht s tip tc thay i cho n 9 v sau s y vng quay k ln 1, khi ch s ng h chuyn t 00000019 thnh 00000020 Phng php m trn h nh phn cng ging nh qu trnh trn, mi vng ch c 0 v 1 khi 0 thay th cho 9. Nu ng h kilomet da trn h m nh phn th chng s xut hin ln lt nh sau 00000000 00000001 00000010 00000011 00000100 00000101 00000110 S thay i ch s trn thc cht l qu trnh m t 0 n 6, nu thay i t 00000011 thnh 00000100 th cng ging nh ch s ng h chuyn t 00000099 thnh 00000100. Nn nh rng vic chuyn i t 9 thnh 0 ng h tng t cho chuyn i t 1 thnh 0 khi h nh phn. Quay tr li vn biu din gi tr s khi dng h nh phn, ta nhn thy mt byte c th lu tr mt s nguyn c gi tr trong khong t 0 n 255 (00000000 n 11111111), vi 2 byte c th lu tr mt s nguyn c gi tr t 0 n 65535. Cch lm ny s lm tng hiu qu kh nng lu tr cc s nguyn so vi cch dng mt byte cho mt ch s trong bng m ASCII. Mt l do khc su xa hn cho vic lu tr thng tin dng s khi dng h nh phn hay hn dng bng m, l h thng nh phn m t chnh xc k thut lu tr dng bit trong my tnh. Ngoi ra ta c th s dng h nh phn biu din
www4.hcmut.edu.vn/~huynhqlinh/TinhocDC/THDC11/Bai01_3.htm

2/14

15/06/2012

Biu

din th ng tin

tr n m y t nh

cc s nguyn m vi phng php b 2 (twos complement notation) hoc dng phng php du chm ng (floating point notation) biu din hn s. Tu theo gi tr ca s m ta c phng php biu din khc nhau. y ta c hai khi nim l trn s (overflow) l khi gi tr ca s qua ln vt qu s lng bit biu din ca chng hoc lm trn (round-off) xy ra khi phn s c gi tr b lm trn dn n sai s. Cc s biu din h nh phn s l mt chui bit, ng vi mi v tr bit c gn mt trng s. Cc trng s ny c xc nh t phi sang tri vi cc gi tr l 1, 2, 4, 8,... Vi v tr cc bit tng ng 0, 1, 2, 3,... Da theo qui lut : s sau s bng 2 ln s trc, v d vi biu din nh phn 100101 l biu din nh phn ca 37.

Phng php chuyn i gia h thp phn v nh phn bn c c th tham kho phn 1. Sau y chng ta cng tm hiu cc thao tc x l khc trn h nh phn. 3. Cng nh phn Trong h nh phn thao tc cng cng ging nh thao tc cng trong h thp phn vi mt s qui tc sau 0+0=0 1+0=1 0+1=1 1 + 1 = 10

Khi cng vn thc hin cng cc ct t phi sang tri, ng vi mi ct ta cng 2 s theo qui tc trn, nu c nh th cng nh sang ct k bn V d : cho 2 dy bit 0 0 1 0 1 1 1 1 0 0 1 1 0 1

+ 0 0

www4.hcmut.edu.vn/~huynhqlinh/TinhocDC/THDC11/Bai01_3.htm

3/14

15/06/2012

Biu

din th ng tin

tr n m y t nh

+ 0 0 0 1 1 0 1 1 Nh 1
0 1

+ 0 0 0 1 1 0 1 1
1 0 1

+ 0 0 0 1 1 0 1 1 Nh 1
0 1 0 1

+ 0 0 0 1 1 0 1 1 Nh 1
1 0 1 0 1

+ 0 0 0 1 1 0 1 1 Nh 1
0 1 0 1 0 1

+ 0 0 0 1 1 0 1 1
www4.hcmut.edu.vn/~huynhqlinh/TinhocDC/THDC11/Bai01_3.htm

4/14

15/06/2012

Biu

din th ng tin

tr n m y t nh

+ 0 0 0 1 1 0 1 1
0 1 0 1 0 1 0 1

+ 0 0 0 1 1 0 1 1 = 0 1 0 1 0 1 0 1
Cc php ton khc ta cng thc hin tng t. Khi nghin cu k thut biu din cc s thng qua biu din s trong h nh phn i din cho cc bit, ta ch cp n cc s nguyn dng, cn cc s m th sao? Chnh iu ny ta cn c mt h c th biu din cho c s m v s dng. Cc nh ton hc trong thi gian di quan tm n h thng biu din s, nhiu kin c a ra, trong cc kin , c mt s kin rt ph hp vi kh nng thit k cc mch in trong my tnh, v hu ht cc kin ny vn da trn h nh phn nhng c mt s bin i l h nh phn c du. C ba cch biu din mt s m h nh phn c du l : phng php du lng. 4. Phng php du lng (sign - magnitude) Theo cch biu din ny, bit cc tri c dng lm bit du (1 l du + v 0 l du - ) cc bit cn li biu din ln ca s. V d: vi mu l 4 bit th cc s biu din nh sau: Mu bit 1111 1110 1101 1100 1011 1010 1001 Gi tr c biu din 7 6 5 4 3 2 1

www4.hcmut.edu.vn/~huynhqlinh/TinhocDC/THDC11/Bai01_3.htm

5/14

15/06/2012

Biu

din th ng tin

tr n m y t nh

1000 0111 0110 0101 0100 0011 0010 0001 0000 Qui tc 5

0 -1 -2 -3 -4 -5 -6 -7 -8

Phng php biu din mt s m v dng nh phn c du vi mu K bit l ly s cn biu din cng thm 2K-1 sau biu din chng h nh phn. V d: vi s +5 trong mu 4 bit th biu din l 5 + 8 =13 s l 1101 vi s -5 trong mu 4 bit th biu din l -5 + 8 =3 s l 0011 Ngoi cch biu din bng du lng cc nh ton hc cn a ra 2 cch biu din sau:

5. Phng php biu din s b 1 (ones complement) Theo cch biu din ny vn dng bit cc tri lm bit du nhng vi qui nh c thay i l 0 cho s dng v 1 cho s m. biu din s n theo dng b 1 ta thc hin cc thao tc sau : Qui tc 6 : Biu din di dng nh phn ca tr tuyt i n theo mu k bit c nh cho trc. Nu n < 0 th i 1 thnh 0 v ngc li trong dy s nh phn. V d: vi n = 5 dng mu 4 bit th biu din theo phng php b 1 l 0101 n = -5 dng mu 4 bit th biu din theo phng php b 1 l 1010 vi n = 6 dng mu 4 bit th biu din theo phng php b 1 l 0110 n = -6 dng mu 4 bit th biu din theo phng php b 1 l 1001

www4.hcmut.edu.vn/~huynhqlinh/TinhocDC/THDC11/Bai01_3.htm

6/14

15/06/2012

Biu

din th ng tin

tr n m y t nh

nu biu din nh phn ca 6 l th biu din s b 1 ca -6 s l 6. Phng php biu din s b 2 (twos complement)

0 1

1 1 0 0 0 1

Theo cch biu din ny vn s dng bit cc tri lm bit du ging nh b 1, nhng c mt s khc bit khi i sang h nh phn c du, cc buc thc hin nh sau : Qui tc 7 : Biu din di dng nh phn ca tr tuyt i n theo mu k bit c nh cho trc. Nu n < 0 th bt u t phi qua tri gi nguyn cc bit cho n khi gp bit c gi tr l 1 u tin, sau cc bit tip theo bn tri bit 1 u tin i 1 thnh 0 v ngc li. V d : cho n = -6 th biu din nh phn ca tr tuyt i ca n cho mu 4 bit l 0110 khi biu din ca s b 2 cho -6 l 1010 Biu din s b 2 qua mu 4 bit Mu bit 0111 0110 0101 0100 0011 0010 0001 0000 1111 1110 1101 1100 1011 1010 1001 1000 Gi tr c biu din 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8

www4.hcmut.edu.vn/~huynhqlinh/TinhocDC/THDC11/Bai01_3.htm

7/14

15/06/2012

Biu

din th ng tin

tr n m y t nh

Thc cht s biu din di dng b 2 l s biu din b 1 sau ta cng thm 1. V d : S -6 c biu din b 1 l 1001 nu ta ly s b 1 ny cng thm 1 th kt qu l 1001 + 1 = 1010 y chnh l dng b 2 Hnh v sau s minh ho biu din s b 2 cho s -6 : nu biu din nh phn ca 6 l th biu din s b 1 ca -6 s l cng thm 1 th biu din s b 2 ca -6 s l

0 1 +
=

1 1 0 0 0 1 1 0 1 0

7. Php cng khi s c biu din b 1 v b 2 Qui tc 8: i vi s dng b 1 khi thc hin php cng ta vn thc hin nh php ton tng ng trn h nh phn, nu 2 bit cc tri khi thc hin php cng m pht sinh bit nh th s cng nh vo kt qu. v d 1 : -6 biu din b 1 vi mu 4 bit l 1001 4 biu din b 1 vi mu 4 bit l 0100 Kt qu php cng dng b 1 l 1101 (l biu din ca -2 b 1) -6 + 4 -2 v d 2 : -6 biu din b 1 vi mu 5 bit l 11001 -4 biu din b 1 vi mu 5 bit l 11011 Kt qu php cng dng b 1 l 10100 v cn nh 1 khi cng 2 bit cc tri khi kt qu s l 10100 + 1 = 10101 l biu din ca s -10 dng b 1. 1 0 0 1 + 0 1 0 0 1 1 0 1

-2 b 1

-6 + -4 +

1 1 1

1 1 0

0 0 1

0 1 0

1 1 0

www4.hcmut.edu.vn/~huynhqlinh/TinhocDC/THDC11/Bai01_3.htm

8/14

15/06/2012

Biu

din th ng tin

tr n m y t nh

+ -10 Qui tc 9 1 0 1 0

1 1

Nh 1 -10 b 1

i vi b 2 ta vn thc hin nh php cng nh phn, nhng nu 2 bit cc tri pht sinh bit nh th b. v d 1 : -6 biu din b 2 vi mu 4 bit l 1010 4 biu din b 2 vi mu 4 bit l 0100 Kt qu php cng dng b 2 l 1110 (l biu din ca -2 b 2) -6 + 4 1 0 1 0 + 0 1 0 0 1 1 1 0

-2 b 2

v d 2 : -6 biu din b 2 vi mu 5 bit l 11010 -4 biu din b 2 vi mu 5 bit l 11100 Kt qu php cng dng b 2 l 10110 v cn nh 1 khi cng 2 bit cc tri nhng ta b nh ny v kt qu l 10110 l biu din ca -10 -6 + -4 1 1 0 1 0 + 1 1 1 0 0 1 0 1 1 0

-10 b 2 b i nh

8. Li trn s Trong cc v d trn bn c chc cng thc mc ti sao v d 2 trong php cng s b 2 ta li dng mu 5 bit ch khng l 4 bit? ngha ca li trn s gii thiu cc cc phn trc, l hin tng xy ra khi s cn biu din vt qu s bit cho trc biu din n. V d : nu v d 2 ta dng mu 4 bit cho biu din b 2 cho -6 v -4, khi bi ton c thc hin nh sau : -6 biu din b 2 vi mu 4 bit l 1010 -4 biu din b 2 vi mu 4 bit l 1100 Kt qu php cng dng b 1 l 0110 l biu din ca +6, do kt qu b sai. Nguyn nhn l do ta ly s lng bit biu din qu t nn xy ra li trn s. Do ngi s dng my tnh phi lng trc c tnh hung ny khi mun lu tr d liu, khc phc ta tng s lng bit nhiu hn th s khng gy hin
www4.hcmut.edu.vn/~huynhqlinh/TinhocDC/THDC11/Bai01_3.htm

9/14

15/06/2012

Biu

din th ng tin

tr n m y t nh

tng trn. V d vi mu 32 bit th gi tr dng ln nht l 2147483647. Tng qut ta c s php biu din b 1 v b 2 th gi tr dng ln nht cho php khi dng mu n bit l : 2n-1 -1 v gi tr m nh nht l -2n-1 9. Biu din hn s bng h nh phn biu din hn s bng h nh phn ta dng du chm c s (radix point) ging nh cch biu din s c phn thp phn trong h c s 10, khi s bn tri du chm c s l biu din nh phn ca phn nguyn ca hn s v bn phi l biu din nh phn ca phn s, v tr bit bn phi u tin sau du chm l biu din cho s k tip l , , ... Vi qui lut s sau s nh hn 2 ln so vi s trc. Cc gi tr ny gi l trng s ca bit tng ng vi v tr tnh t v tr u tin bn phi ca du chm c s. bin i hn s h nh phn sang hn s h thp phn, ta vn s dng cch thc hin nh i s nguyn sang h thp phn cho cc s nh phn bn tri v bn phi du chm nhng vi ch cc s nh phn bn phi du chm s c trng s l phn s bt u t v gim mt na khi i t tri sang phi V d : Cho hn s 5 th s bin i thnh 101.101 v c biu din theo lu sau :

Php cng trn hn s biu din di dng nh phn cng c thc hin nh php cng nh phn cho s nguyn, ch c ch l du chm c s phi sp thng hng cho 2 s. V d : 1 + = 1 1 0 1 0 0 1 . . . 0 1 0 1 1 0 1 1 l biu din ca 2 3 /8 l biu din ca 4 3 /4

www4.hcmut.edu.vn/~huynhqlinh/TinhocDC/THDC11/Bai01_3.htm

10/14

15/06/2012

Biu

din th ng tin

tr n m y t nh

l biu din ca 7 1 /8 10.Cc php ton lun l Ba php ton thng thng trong nhm ca cc php ton lun l l AND, OR, v EXCLUSIVE OR (XOR). Chng tng t nh php cng v tr vi hai ton hng v tr ra mt kt qu duy nht. (Tri li c mt s php ton m gi tr tr v ca n s cho ra 2 s khc du nhau nh l php rt cn bc hai, v d nh 4 khi rt cn s cho hai kt qu l 2 v -2). By gi chng ta s xem qua mt s php ton nh sau : a. Php ton AND Hnh 2-6 cho ta mt bng cc kt qu ca php ton AND vi mt bit duy nht. Ch rng kt qu l 1 ch khi c hai bit u l 1. 1 AND Kt qu 1 1 AND Kt qu 1 0 0 AND Kt qu 0 1 0 AND Kt qu 0 0 0

Tri li, vi php cng cho cc ton hng l cc bit th s cho kt qu khng ging nh php ton AND. Vi hai dy gm nhiu bit l ton hng cho php ton AND, th vn c p dng cc qui tc thc hin php ton AND nh trong v d d y, khi th ta s tch ring ra tng cp bit thnh cc ct mi dy v thc hin qui tc AND cho cp bit . V d thc hin php AND cho hai byte sau: 10011010 AND Kt qu 10011010 10001000

Mt trong nhng s dng chnh ca php ton AND l thnh phn 0 trong mt dy bit s khng b nh hng bi cc phn khc. Hy xem mt v d, iu g s xy ra nu dy bit 00001111 l ton hng u tin ca php ton AND. Mc d ta khng bit thnh phn ton hng hai, nhng chng ta vn suy ra c 4 bit bn tri nht l cc s 0. Hn th na, bn bit bn phi nht l 4 bit cui ca ton hng th hai, theo v d ta c : 00001111 AND Kt qu 00001010 00001010

Cch s dng php ton AND trong v d ny c gi l cch s dng mt n (masking). y, ton hng u c gi l mt n (mask), n c s dng xc nh phn ca ton hng cn li s nh hng n kt qu. Trong trng hp ny, mt n c s dng s cho ra kt qu l mi phn 4 bit ca cc ton hng trong
www4.hcmut.edu.vn/~huynhqlinh/TinhocDC/THDC11/Bai01_3.htm

11/14

15/06/2012

Biu

din th ng tin

tr n m y t nh

cc s 0 vi 4 bit u ca ton hng th nht v 4 bit sau l phn ca ton hng th hai. Php ton ny thng c s dng trong php kim tra bit l 1 hay 0. V d, mt chui gm 52 bit, vi mi bit l mt i din cho mt l bi, c th c s dng biu din trng thi cc l bi cho mt ngi chi bng cch gn 1 cho 5 bit tng ng vi cc l bi v cc bit cn li l 0. Khi nu mun kim tra l bi th 6 trong 52 l bi ny c thuc v mt ngi no hay khng, th ta c th s dng php ton AND. Mt v d khc ta c 8 bit trong mt nh ca b nh chnh, ta mun kim tra bit th 3 trong nhm bit cao c tn ti hay khng? Bng cch s dng mt n 00100000 v thc hin php ton AND gia dy bit v mt n. Nu byte nhn c c gi tr l 0 th bit th 3 trong phn cao khng tn ti v ngc li l tn ti. Do php ton AND thng c s dng trong chng trnh cng vi lnh nhy c iu kin. Ngoi ra ta c bit th 3 ny l 1, nhng ta mun thay i n thnh 0 m khng nh hng n cc bit khc, ta c th AND vi mt n 11011111 v sau a kt qu tr li dy bit gc. b. Php ton OR

1 OR Kt qu 1 1 OR Kt qu

1 0 1 OR Kt qu

0 1 1 OR Kt qu

0 0 0

By gi chng ta cng tm hiu php ton OR. Cc qui tc nh hnh 2-7. Ch rng kt qu l 0 ch khi c 2 bit ton hng u l 0. Mt ln na cc qui tc c bn c th c m rng n cc chui cc bit bng cch da trn vic thc hin php ton cho cc ct c lp, nh trnh by sau y: 10011010 OR 11001001 11010011 y php ton AND c th c s dng chp li mt phn ca dy bit v thm vo 0 phn khng chp li. Cn i vi php ton OR th c th s dng chp li mt phn ca dy bit, v t gi tr 1 vo cc phn khng chp li. Trong phn ny chng ta mt ln na s dng mt n, nhng thi im ny chng ta xc nh cc v tr bit c chp li 0 v s dng 1 ch cc v tr khng c chp li. V d, thc hin php ton OR vi mt byte c gi tr l 11110000 sau cho ta c c mt kt qu vi cc s 1 4 bit cao v 4 bit cn li l 4 bit thp ca ton hng kia. Bi ton c trnh by nh sau: 11110000 OR 10101010 11111010

www4.hcmut.edu.vn/~huynhqlinh/TinhocDC/THDC11/Bai01_3.htm

12/14

15/06/2012

Biu

din th ng tin

tr n m y t nh

T ta thy rng php ton AND v mt n 11011111 c th c s dng buc thnh 0 bit th 3 ca phn cao trong mt dy 8 bit, cn php ton OR v mt n 00100000 c th buc thnh 1 v tr . c. Php ton EXCLUSIVE OR (XOR) Cc qui tc cn bn ca php ton XOR c trnh by trong hnh 2-8. Trong trng hp kt qu l 1, th hai bit ton t ch c chnh xc mt bit l 1. Ngha l nu mt bit l 1 th bit kia khng c l 1, mi cho ra kt qu l 1. Ta c th p dng cc qui tc ny cho mt dy bit theo v d nh sau: 10011010 XOR 11001001 01010011

1 XOR Kt qu 1 1 XOR Kt qu

1 0 0 XOR Kt qu

0 1 0 XOR Kt qu

0 0 0

S dng chnh ca php ton ny l ly phn b ca mt chui bit.V d, ly phn b ca ton hng th 2 ta thc hin nh sau: 11111111 XOR 10101010 01010101 d. Cc php ton dch chuyn v quay Cc php ton thuc lp cc php ton nh php quay (rotation) v php dch chuyn (shift), u c ngha bin i cc bit trong mt thanh ghi v thng c s dng gii quyt cc bi ton thc hin trn bit. V d nh bin i mt byte theo mt yu cu no bng cch s dng mt n, hoc thao tc trn phn nh tr ca cc s biu din dng du chm ng. Nhng php ton ny c phn chia ty theo hng di chuyn ca cc dy bit (sang tri hay sang phi). Cho mt byte gm 8 bit, nu ta thc hin php ton SHIFT cho dy bit ca n sang hng tri hay phi th bit u tin ca byte (l bit cao nht nu dch chuyn sang phi, hay bit thp nht khi dch chuyn sang tri) s b chuyn i, v bit cui cng ca n (l bit cao nht nu dch chuyn sang tri, hay bit thp nht khi dch chuyn sang phi) s c t l 0. V d cho mt byte c gi tr l 10001110, khi SHIFT tri mt ln s l 00011100 hoc SHIFT phi kt qu l 01000111. i vi php ton quay, cng ging nh php SHIFT. Nhng bit cui s c chuyn vo bit u tin.
www4.hcmut.edu.vn/~huynhqlinh/TinhocDC/THDC11/Bai01_3.htm

13/14

15/06/2012

Biu

din th ng tin

tr n m y t nh

V d cho mt byte c gi tr l 10001110, khi ta quay tri mt bit th kt qu s l 00011101; quay phi mt bit th kt qu s l 01000111. Php ton SHIFT thng c s dng cho cc php nhn hay chia cho 2, i vi SHIFT tri chnh l nhn cho 2, v SHIFT phi l chia cho 2. Do php ton ny ny ta gi l php chuyn s hc (arithmetic Shifts).

www4.hcmut.edu.vn/~huynhqlinh/TinhocDC/THDC11/Bai01_3.htm

14/14