COMPUTER CODES Although the capacity of their storage locations can vary,every computer stores numbers,letters,and other characters in a coded form.
$ f& a# V6 O& r2 q2 _
Every character in storage is represented by a string of 0s and 1s—the only digits found in the binary numbering system. Let’s see how it’s possible to use just two digits to represent any character.
9 r- y; w' @$ g" l- b/ BMost computers use a coded version of true binary to represent decimal numbers.
4 b' S* [/ }2 xAlthough many coding schemes have been developed over the years,the most popular of these use a binary coded decimal(BCD)approach.
# O( E e' r. |, L5 d
The BCD Approach.
5 _7 u( |% y7 f3 B% V- E With BCD,it’s possible to convert each decimal number into its binary equivalent rather than convert the entire decimal value into a pure binary form.
) X& M3 ]$ q4 Z7 F. O' m/ O. H The BCD equivalent of each possible symbol is shown in Fig.1-3.
( P/ `; o2 @! x" _+ H+ F7 J Since 8 and 9 require 4 bits,all decimal digits are presented in BCD by 4 bits.
1 {) n: D7 }! R7 Z; t
You’ve just seen that 202 is equal to 1100 1010 in a pure binary form.
" P+ a% J) x E+ ~/ ~: ~
Converting 202 into BCD,however,produces the following result:202(10)in BCD=0010 0000 0010 or 0010 0000 0010
% }6 j1 u& j, j3 S. b# ~: Z( S& T2 d
7 z$ W. l. a* z8 B' a6 _+ I Six-bit BCD Code.
6 F# r% n0 r+ c5 ~9 c# Z
Instead of using 4 bits with only 16 possible characters,computer designers commonly use 6,7,8 bits to represent characters in alphanumeric versions of BCD.
9 G7 ?2 \+ v& o) @! h7 }9 j7 u# ^" ^ In the 6-bit code,the four BCD numeric place positions(1,2,4,and 8)are retained,but two additional zone positions are included(Fig. 1-4(a)).
" ]% m z; N. h9 Q
With 6 bits,it’s possible to represent 64 different characters(26).
" ~) i( M( o! n
This is a sufficient number to code the decimal digits(10),capital letters(26),and other special characters and punctuation marks(28).
/ H( T& n* x* c: W8 d
Fig. 1-4(b)shows you how a few of the 64 possible characters are represented in a standard 6-bit BCD code.
9 n# C* e) u) f9 V0 ^ Seven and Eight-Bit Codes.
! I3 y8 r, o( {5 \! X
Since 64 possible bit combinations isn’t sufficient to provide decimal numbers(10),lower-case letters(26),capital letters(26),and a large number of other characters (28),designers have extended the 6-bit BCD code to 7 and 8 bits.
, D0 B/ V9 ?6 p& S
With 7 bits,it’s possible to provide 128 different arrangements(27);with 8 bits,256 variations are possible(28).
1 d; ^4 Z4 `* ^2 G9 \" t7 @In addition to the four numeric place positions,there are three zone bit positions in a 7-bit code,and four zone bit positions in an 8-bit code.
: X3 t i7 ~6 q4 N0 E8 k! i The 7-bit American Standard Code for Information Interchange(ASCII)is widely used in data communications work and is by far the most popular code used to represent data internally in personal computers.
' \ Q) }$ a0 E) l8 p5 y% i( DThe ASCII format and the coding used to represent selected characters are shown in Fig. 1-5.
3 Y) V/ u: @" v( b; A( `) _These are other two 8-bit codes in common use.
0 r) Z1 b8 b3 \" r& z! m1 QOne is the Extended Binary Coded Decimal Interchange Code(EBCDIC).
, H( }5 X+ a2 _This code is used in IBM mainframe models and in similar machines produced by other manufactures.
: R( s7 ~# O$ T$ n7 e7 ]
The other 8-bit code is ASCII-8,an 8-bit version of ASCII that is frequently used in the larger machines produced by some vendors.
" @2 A. k J; |/ F! q6 O+ uFig. 1-6 presents the 8-bit format and shows how selected characters are represented in these 8-bit codes.
The main difference is in the selection of bit patterns to use in the zone positions. : f( s# L7 t( w1 s+ s0 V: |" O8 E
. m% G g( o0 D
$ u, K% y1 W- D) O. V1 G
计算机代码
; S }8 c, ~' G% W! r 尽管计算机存储单元的容量可大可小,但是每台计算机都是以代码形式存储数、字母和其他字符的。
# E9 R% T& B. e+ x9 G/ ` 存储器中的每个字符都用0、1(二进制编码系统中仅有的两个数字)串表示。下面让我们看看如何只用两个数字来表示任何字符。
+ f) l E' s2 [( J- A 大多数计算机用准确的二进制编码方法表示十进制数。
1 e; @0 T# R- @2 R% F; D, [" s 多年来研制了许多编码方案,其中最流行的是二进制编码的十进制(BCD)方案。
0 \9 L# D" B+ d) l1 i
BCD方案:用BCD码,可以将十进制数的每一位转换成相等的二进制数,而不是将整个十进制数的值转换成纯二进制数的形式。各十进制数位对应的BCD码如图1-3所示。因为8和9需要4位二进制数表示,所以,所有十进制数位均用4位BCD码表示。众所周知,用纯二进制数表示202等于11001010,但是将202转换成BCD码,结果如下:202(10)的BCD码 = 0010 0000 0010或001000000010。
* _ U( p, x5 }( T
6位BCD码。计算机设计者普遍采用6位、7位或8位BCD字母数字型字符来代替只有16种可能字符的4位BCD码。在6位BCD码中,4位BCD数字位(1,2,4,8)仍保留,但是,包含了两个附加的标志位,如图1-4(a)所示。用6位二进制数可以表示64个不同字符(26)。这对十进制数字(10个)、大写字母(26个)及其他特殊字符和标点符号(28个)进行编码已经够用。其中,几个对应的标准6位BCD码字符如图1-4(b)所示。
}! l; _3 f8 Q& B5 `" t4 G# `0 d& x7 O 7位和8位编码。因为64种可能的位排列仍不能满足十进制数(10个)、小写字母(26个)、大写字母(26个)和大量的其他字符(28个)编码的需要,所以设计者将6位BCD码扩充到7位和8位。7位二进制数可以提供128种不同的排列(27);而8位二进制数可以有256种排列(28)。除了4位数字位之外,在7位编码中有3个标志位,在8位编码中则有4个标志位。7位美国信息交换用标准代码(ASCII)广泛地用于数据通信,而且是目前个人计算机内部数据表示中最为流行的代码。ASCII码的格式及所选字符的编码如图1-5所示。
8 V$ n5 f9 d8 ~- {0 @1 w( `
还有另外两种普遍使用的8位编码。一种是扩展的二-十进制交换码(EBCDIC),该编码用于IBM大型计算机及其他厂家生产的类似机器中;另一种8位编码是ASCII-8,是ASCII代码8位格式,常用于一些厂商生产的较大机器中。8位编码的格式及如何表示所选字符如图1-6所示。这两种编码的主要区别在于标志位模式的选择。
0 _$ x4 w3 ~. E
Detecting Code Errors.
. l) R* \* r) z Computers are very reliable,but they’re not infallible.
5 ^6 X: n/ d8 N- M2 p; J7 N' V
If just one bit in a string of 6,7,or 8 bits is lost during data input,processing,or output operations,an incorrect character code will be created.
0 p" _3 ]1 Z" w5 Z7 m5 d
Such an error can be caused by dust particles on storage media,by improper humidity levels near the computer,or by many other factors.
' T) W) r3 q: R/ J( B# v Q Fortunately,however,computer designers have developed a method for detecting such errors by adding an extra check bit or parity bit to each 6,7,or 8-bit character represented in storage.
3 B& ?# p% G& C
Thus,as you can see in Fig. 1-7,a total of 7,8,or 9 bits may actually be stored.
2 ~$ z- s! M- l* a
The designers of a particular computer model may then use the check bit to make sure that every valid character code will always have an even number of 1 bits.
, c% [+ w, ]) X# Q
The 6-bit coding used to represent selected characters was presented earlier in Fig.1-4.
$ s: f8 H6 O# n: Q
Several of these characters have been reproduced in Fig.1-8.
1 d6 `7 r& ?& G1 k' B
0 Z% J5 j* b; s i, ` p7 f
You’ll notice that if the basic code for a character such as 1,2,or A requires an odd number of 1 bits,an additional 1 bit is added in the check-bit location so that there will always be an even number of such bits.
. q' y+ _# X% L
This is an example of an even-parity format,but other computers use the check bit to produce an odd parity.
: \5 B2 M' r. g& A' O
Since every valid character in a computer that uses even parity must always have an even number of 1 bits,circuits for parity,checking are built into the computer to constantly monitor the data moving through the system.
! G8 p, L' r1 i
The computer operator is notified if a bit is lost and a parity error is detected.Of course,parity checking will only detect coding errors.
3 k2 P2 \6 {1 D4 w& W3 R It cannot signal the fact that incorrect data have been entered into the system if the data are properly coded.
$ Z- [5 q; l; c. ~( Z5 L
代码检错。
4 n/ l, J0 b7 {' l5 O! b 计算机可靠性高,但并非不会出错。在数据输入、处理或输出*作中,6位、7位或8位二进制数串即使仅有1位丢失,也会产生错误的字符代码。这样的错误可由存储介质上的灰尘微粒、计算机附近的湿度不适宜或许多其他因素造成。
' m5 Q' L4 _8 \: k1 k6 b+ f
不过幸运的是计算机设计者已研制了一种检测这种错误的方法,这就是对在存储器中表示的每个6位、7位或8位字符增加一个附加的检查位或奇偶检验位。这样,正如你在图1-7中所看到的,实际存储的可以是7位、8位或9位。于是,特定计算机的设计者可使用校验位来保证每个合法字符的代码总保持偶数个“1”。前面图1-4所示的是所选字符的6位编码。其中一些字符在图1-8中重新表示。注意,如果像1、2或A这样的字符的基本代码有奇数个“l”,那么就要在检验位上加一个“l”,使得代码总保持偶数个“l”。这是一个偶检验的例子,而其他计算机也可使用检验位生成奇检验。因为计算机中使用偶检验时,合法字符总有偶数个“1”,所以计算机中装入了奇偶检验检测电路,经常监测系统中移动的数据。如果有位丢失,就会检测到奇偶检验错,并通知计算机*作人员。当然,奇偶检验只能检测编码
. P A1 Y( [& Y, u, k0 z
错。它不能告知数据编码正确但输入数据本身出错的情况。