0 ratings0% found this document useful (0 votes) 150 views9 pagesComputer Codes
English, science and computer codes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
CHAPTER 4
4, COMPUTER CODES
In the previous chapter, we have discussed about
true or “pure” binary numbers. In this chapter, we will see
how these binary numbers are coded to represent characters,
in the computer memory. Thus, the goal of this chapter is to
present the formats used in computer memory to record
data, Although many coding schemes have been developed
over the years, we will be discussing only the most
commonly used computer codes.
‘Numeric data is not the only form of data that is to
be handled by a computer. We often require to process
alphanumeric data also, An alphanumeric data isa string of
symbols where a symbol may be one of the letters
ABC.uoZ or one of the digits 0,1,2,..9 or a special
character, such as + - * / , . () = (space or blank) etc. An
alphabetic data consists of only the letters A,B,C,...Z and
the blank character. Similarly, numeric data consists of
only numbers 0,1 However, any data must be
represented internally by the bits 0 & 1. As such, binary
coding schemes are used to represent data internally in the
computer memory. In binary coding, every symbol that
appears in the data is represented by a group of bits: The
‘group of bits used to represent a symbol is called a byte. To
indicate the number of bits in a group, sometimes a byte is,
referred to as “n-bit byte" where the group contains 1 bits.
However, the term byte is commonly used to mean an 8-bit
byte (a group of 8 bits) because most of the modem
computers use 8 bits to represent a symbol.
BCD CODE
‘The Binary Coded Decimal (BCD) code is one of
the early memory codes. It is based on the idea of
converting each digit of a decimal number into its binary
‘equivalent rather than converting the entire decimal value
into a pure binary form, This facilitates the conversion
Drocess to a great extent.
‘The BCD equivalent of each decimal digit is shown in
Table 4.1. Since 8 and 9 require 4 bits, all decimal digits
are represented in BCD by 4 bits. You have seen in
Example 3.9 that 42,. is equal to 101010, in a pure binary
form.
Converting 42,.into BCD, however, produces the following
result:
429 = 0100 0010
42
or 01000010 in BCD
@ scanned with OKEN Scanner% Computer Fundamentals
Note that each decimal digit is hipaa
Converted to a 4 ‘it binmy number ad hence the
Conversion proces is ery easy. Alo note that when 4b
c ted, aogier 16 (2) coniguaton ae possible
(refer to hexadecimal number system). But from Table 4.
yovcan seta ony the rs 10 ofthese combinations are
used 10 represent decimal digits. The remaining
arrangements (1010,
Table 4.1. BCD Equivalent of Decimal Digits
aera eset tees ae CEC
Decimal Digits BCD Equivalent
eee eee ee eee
0000
001
010
oon
100
o101
110
oul
1000
1001
—
FOU, 1100, 1101, 1110 and 1111) have decimal values
fom 10 to 15. These arrangements are not used in BCD
Coding. That is, 1010 does not represent 10,, in BCD.
Instead,
10, = 0001 9009
——oxntcoinnco
Similarly,
15, =0001 9101
or cooio101n BCD
rumbers because 4 bits are insufficient to represent i
[ious characters used by a computer, Inead of ting 4
bits with only 16 possible characters, computer designe
commonly use 6 bits to represent characters in BCD nae
In the 6-bit BCD code, the four BCD numero any
Positions are retained, but two addtional zone positions
Added. With 6 bits, it is possible to represent 64
diferent characters, This 8a sutiigy
decimal digits (10), alphabetic eters he
characters (28). Table 42 itlsene, Mog,
alphabetic and numeric character in es a
In Chaps 3, We have son he
hexadecimal number systems. ag Shorey
binary, Because BCD is a 6-it cage" tt
divided into two 3-bit groups. Each of inti
it 8 34 Sy
can be represented by 1 octal dig, yas
system if used 28 shot notation for 2
computers that use BCD code for interny Tepe yp,
characters. This results in a one-o-hyes 2
volume of memory dump. Table 4.2 ajey"tin ad
equivalent of the alphabetic and numerie Chara ng
in BCD. “oy
Example 4.1. ‘Show the binary digit
ihe word BASE in Be"!
Solution :
B = 110010 in BCD binary Notation
A i inary notation
s inary notation
E 110101 in BCD binary notation
So the binary digits
4110010 110001 o10010 110101
B A s E
will record the word BASE in BCD,
Example4.2. Using octal notation show th 8
‘coding forthe word DIGIT.
Solution :
D = 64 in BCD octal notation
1 = 71 in BCD octal notation
G = 67 in BCD octal notation
= 71 in BCD etal notation
T = 23 in BCD octal notation
i :
Sut BCD coding forthe word DIGHT in ot
“ugg
Plait
@ scanned with OKEN ScannerComputer Codes 27
Table 4.2, Alphabetic And Numeric EBCDI
Characters tn BCD Along, With
ir Octal Equivale
The major problem with BCD code is that only 64
(28) aifferent ch be represented in it. Thi
sufficient for providing, decimal numbers (10), tow
Character eco Gano Ietiers (26), capital letters (26), and a fairly large number of
oth characters (22).
Zone Digit
Hence, the BCD code was extended from a 6-bit
ee code to an B-bit code, The added 2 bits are used as
A 61 | additional zone bits, expanding the zone to 4 bits. The
B 1 ono Py resulting code is called the extended binary-coded decimal
c 1 oon a interchange code (EBCDIC). In this code, it is possible to
D, W 0100 a represent 256 (28) different characters instead of 64 (26). In
E} Mono 65 addition to the various character requirements mentioned
FI Mono 66 above, iis also allows a large variety of printable
G \ nu oom 7 characters and several nonprintable control characters. The
H \ 1 1000 0 control characters are used to control such activities as
1 1001 n printer vertical spacing, movement of cursor on the
terminal sereen, etc. All of the 256 bit combinations have
in 10 0001 4 not yet been assigned characters, so the code can still grow
K 10 O10 aa. as new requirements develop.
L 10 0011 43
M 10 0100 44 Because EBCDIC is an 8-bit code, it can be easily
N 10 oll. divided into two 4-bit groups. Each of these 4-bit groups
a 10 ono 46 ‘can be represented by 1 hexadecimal digit (refer to Chapter
PR 10 olll a7 3). Thus, hexadecimal number system is used as shortcut
Q 10 1000 50 notion for memory dump by computers that use EBCDIC
R 10 for. 51 for internal representation of characters. This results in a
: one-to-four reduction in the volume of memory dump.
s on 0010 oy Table 4,3 shows the alphabetic and numeric characters in
T OL ool 2B EBCDIC along with their hexadecimal equivalent.
uv 01 0100
v o1 onn5 Developed by IBM, EBCDIC code is used in most
Ww ol 10 26 IBM models and in many other computers.
x 01 oll 7
i o1 1000 30 ZONED AND PACKED DECIMAL NUMBERS
Zz 01 1001.31
From Table 4.3, it can be observed that in the
1 0 ool EBCDIC code, the digit values are the same as the numeric
2 0 = oo10 characters - 0 through 9 (0000-1001). But numeric values
3 0 = ool 03 need some special consideration because we must have a
4 0 0100 og ‘way of indicating whether the number is positive, negative,
5 00 101 05 or unsigned (implies positive), Thus, when a numeric value
ete sepa of eign a
8 rightmost digit. A sign indicator of
8 00 1000 10 hexadecimal C is a plus sign, hexadecimal D is a minus
: fo 101 Sign, and a hexadecimal F means the number is unsigned,
0 | oD Table 4.4 illustrates the representation of numeric values in
oy ._|_§_—— 1 EBCDIC. Note that the only zone affected by the sign is the
Zone of the rightmost digit. All other zones remain as F, the
Zone value for numeric characters in EBCDIC. Because
cach decimal digit has a zone with it, numbers coded in
EBCDIC are called zoned decimal numbers, Numeric data
@ scanned with OKEN Scanner7 Computer Cundanentals
stecnnal nan
Apa into the computer are wy 2H TT a
Printers can print onty those numetie CIM
voned decimal format
cannot pesto
a before
he
However, most
wont on nied
fan be performed, the at
iahmetic Ope
pushed
possible, One such aeeptate format is the pale
foumat. The following step are nso 10 convert
1 umber ton paces eal
metic operation ¢
ted 40 format on which Saal
pag a of te
7 This
wet
Step 1 “The vone hal
rightmost byte
rs the sign vo the extcme Fgh
are everset
mov
the number
Siep2: All remaining zones axe dropped U-
on process of zoned
Table 4.5 illustrates. the conver
ato packed data
dec
It may be observed that packed data requires fewer
number of bytes (group of 8 bits) as compared to zoned
data, In the zoned format, there is only one digit per byte
(cach digit along with the zone requires 8 bits). But there
are to digits in each byte in the packed format (cach digit
requires 4 bis). Ifthe packing process does not completely
fill a byte it is filled with 2 zero, For example, the zoned
data FRF4FSF6 will convert to packed dala 03456F.
Observe that in this example, the zoned data requires 4
bytes and the packed data requires only 3 bytes.
Example 43. Using binary notation, write the
EBCDIC coding for the word BIT.
How many bytes are required for this
representation?
Solution »
B = 1100 0010 in EBCDIC binary notation
1 1100 1001 in EBCDIC binary notation
T = 1110 0011 in EBCDIC binary notation
So the EBCDIC coding for the word BIT in binary notation
will be :
1090010 41001001 11100011
B di T
3 bytes will be required fortis representation because each
etter requires 1 byte (or 8 bis) for its representation,
fabledd. Alphabetic
Characters tn pyr
hele Hexader
ng
Me. A
I Aig
ath
Vay)
Mis,
Chameter — PRCDIC Con
You Digt
A 00 (np
B 1100 Ny
c 100 i}
D 100410
E 1100 O10
F 1100 NG
G NO Ol
Hu 11001000
1 11001001
J 010001. py
K 101 10
ii Nol 001!
M uot 010g
N N01 = 0101._—S ps
0 NOL 0110 g
Pp 101 Oly
Q 101-1000" pg.
R nol 1001. g
s 1100010.
T 10001”
u m0 0100
v 110 O10l-S ES
w 10 old
x m0 Ol
Y 1101000 BB
Zz 1101001,
0 mi 000% =F
1 nn odo. FI
2 1 010s
3 mn oo BB
4 Mu 0100 A
5 a 0101 BS
6 m1 ono
7 mn oul FI
8 Mu 100 BB
9 uu 101
Pe ee
@ scanned with OKEN ScannerNumeric Values In ERCDIC In
Hexadecimal Notation
Table 4.4
Numeric EBCDIC Sign
Value Indicator
35 F3FAES—F for unsigned
4345 F3FICS_—C for positive
5 F3FADS__D for negative
Table4.5. Zoned And Packed Decimal
Numbers
—
Numeric Zoned Packed
Value Format, Format
44s BRIS 345F
+45 ICS 345C
“HS BIRDS -345D
3456 FSAFSF6 —O34S6F
Example 44. Write the EBCDIC coding for the
word ZONE (use _ hexadecimal
notation). How many bytes will be
required for this representation ?
Solution
Z = E9 in EBCDIC hexadecimal notation
© = D6 in EBCDIC hexadecimal notation
N = DS in EBCDIC hexadecimal notation
Es
So the EBCDIC coding for the word ZONE in hexadecimal
notation will be :
E2 D6 DS GS
ZONE
Each hexadecimal digit requires 4 bits and there are
altogether 8 hexadecimal digits. So in all 8 x 4 = 32 bits
will be required. But 8 bits = 1 byte. So 32 bts = 4 bytes.
Hence, 4 bytes will be required for this representation,
‘We may also write directly that since each letter
requires 1 byte for its representation in EBCDIC and there
are 4 letters in the word ZONE, so 4 bytes will be required
Computer Codes 29
for this representation.
Write the EBCDIC zoned-decimal
Example 4.5
coding for the value +256 (u
hexadecimal), How many bytes wil
be required for this representation ?
Solution :
+4256 = F2FSC6 in EBCDIC
Each hexadecimal digit requires 4 bits and there are
altogether 6 hexadecimal digits. So in all 6x4 = 24 bits or 3
bytes (@ bits = 1 byte) will be required for this
representation.
We may also write directly that since each digit
requires 1 byte for its representation in the EBCDIC zoned
decimal coding and there are 3 digits in the given number,
803 bytes will be required for this representation.
Example 46, Write -128 as-packed decimal number
(use hexadecimal). How many bytes
will be required for this
representation?
Solution :
-128 = FIF2D8 in EBCDIC
= 128D in packed format.
Each hexadecimal digit requires 4 bits and there are
altogether 4 hexadecimal digits. So in all 4x4 = 16 bits or 2
bytes (1 byte = 8 bits) will be required for this
representation,
ASCII
Another computer code that is very widely used is
the American Standard Code for Information Interchange
(ASCID). ASCII has been adopted by several American
‘computer manufacturers as their computers’ intemal code.
‘This code is popular in data communications, is used
almost exclusively to represent data internally in
‘microcomputers, and is frequently found in the larger
computers produced by some vendors.
ASCII is of two types : ASCI-7 and ASCIL-8.
ASCIL- is a 7 bit code that allows 128 (2%) different
characters. The first 3 bits are used as zone bits and the last
4 bits indicate the digit. Microcomputers using 8-bit byte
(group of 8 bits for one byte) use the 7-bit ASCII by
leaving the leftmost first bit of each byte as a zero, Table
@ scanned with OKEN Scanneracters in ASCI-T
{shows the alphabenic andl meric ch2
ation
ASCH. His a
aracters rather
ASCH te an extended version O!
6, (28) different oh
Kbit cove that allow
See «bro the rome
I> and ASCTI-R are identical. ASCII also
7 also. show the
notation ther than the
differences, ASCTL-7
ses hexadecimal .
memory dump. Tables 46 and 4
hexadecimal equivalent of the ASCH notations
Write the binary coding for the word
ROY in ASCII-7. How many bytes
are required for this representation?
Solution
1009010 in ASCHL-7 binary notation
(001111 in ASCIL-7 binary notation
(01 1001 in ASCII-7 binary notation.
Hence the binary coding for the word BOY it
7 will be
90910 101111 1011001
B Onn ty,
As each character in ASCIL-7 requires one byte for
its representation and since there are 3 characters in the
word BOY, so 3 bytes will be required for this
representation
Lxample 48. ‘Write the hexadecimal coding for the
‘word GIRL in ASCIL7. How many
bytes are required for this
representation?
Solution
G =47 in ASCIL-7 hexadecimal notation
49 in ASCII-7 hexadecimal notation
R = 52 in ASCU-7 hexadecimal notation
L = 4C in ASCU-7 hexadecimal notation
Hence the hexadecimal coding for the word GIRL in
ASCUL-7 will be :
42 49 32 4c
GIRL
AAs each character in ASCIL-7 equites one byte for
its represemtation and since there are 4 characters in the
word
Tonle. Numeric ang
araracters
Along. With 7ySCH9
Equivalent Mot
Character ASCH Core ;
sg
| “ing
oon 0009
1 oll oor?
2 Ol m0 x
3. Ol ony
4 oll 0100 ag
5 On O11 3g
6 Ol 109g
Waeatoll ory
8 oll 10003
9 Ol 10013
A100 00014)
B 100 0010
c 100 oo gg
D 100 0100 ag
E 100 O11 4s
F100 0104
G 100 om 47
H 100 1000 4g
I 100 100149
J 100 1010 4a
K 100 101 4B
L100 100 ac
M 100 101 4D
N 100 M0 4E
Oo 100 un 4
P 101 000050
Q 10 0001 SI
R101 001082
s 101 oo 83
T 101 010054
U 101 0101 35
vo 101 oo 56
Ww 101 ol 7
x 101 100058
Y 101 1001 38
z 101 1010 SA
Gece eke PEL een eeeeesteeeeee
GIRL, so 4 bytes will be required {
@ scanned with OKEN Scannerrepresentation.
‘Example 4.9. Write the binary coding for the word
SKY in ASCILS, How many bytes are
required fr this representation?
Solution :
$= 10110011 in ASCIL8 binary notation
(0101011 in ASCIL-® binary notation
Y= 10111001 in ASCH binary notation
Hence the binary coding for the word SKY in
ASCILS will be:
O110011 10101011 10111001
$ K Y
‘As each character in ASCIL8 requires one byte for
its representation and since there are 3 characters in the
word SKY, so 3 bytes will be required for this
representation.
Example 4.10, Write the hexadecimal coding forthe
word STAR in ASCIL8. How man;
bytes are required for this
representation?
Solution
B3 in ASCIL-8 hexadecimal notation
4 in ASCII-8 hexadecimal notation
A=Al in ASCIL-8 hexadecimal notation
B2 in ASCIL-8 hexadecimal notation
Hence the hexadecimal coding for the word STAR
in ASCI-8 will be :
B3 B4 Al B2
STAR
‘As each character in ASCII-8 requires one byte for
its representation and since there are 4 characters in the
word STAR, so 4 bytes will be required for this
representation.
COLLATING SEQUENCE
‘The value of an alphanumeric or alphabetic data
‘element is usually the name of some object. Obviously one
‘would not like to perform any arithmetic on such data but
‘one may like to compare them in order to arrange them in
some desired sequence. Now, if we compare the alphabetic
Computer Codes 31
values A andl B, which one will be treated as greater by the
computer? For an answer to such questions, itis necessary
to have some assigned ordering among the characters used
by the computer, This ordering is known as the collating
sequence
Collating sequence may vary from one computer
system to another depending on the type of computer code
used by a particular computer. To illustrate this, Tet us
consider the computer codes already discussed in this
chapter. Observe from Tables 4.2 and 4.3 that the zone
values of the characters A through 9 decreases in BCD code
from the equivalent of decimal 3 down to 0, while in
EBCDIC, the zone values of the characters A through 9
increases from the equivalent of decimal 12 to 15. This
means that a computer which uses BCD code for its internal
representation of characters will teat alphabetic characters
(A,B, ww Z) to be greater than numeric characters (0,1,
9). On the other hand, a computer which uses EBCDIC for
its internal representation of characters will teat numeric
characters 10 be greater than alphabetic characters.
Similarly, observe from Tables 4.6 and 4.7 that a computer
which uses ASCII for its internal representation of
characters will place numbers ahead of letters during a sort
(ascending) because the number characters have a zone
valué that i less than the zone value for eters.
However, whatever may be the type of computer
code used, in most (not all - in BCD 0>9) collating
sequences the following rules are observed :
Letters are considered in alphabetic order
(AcBeC«..<2)
2, Digits are considered in numeric order
(Ocle2«..<9).
Suppose a computer uses EBCDIC as
its internal representation of
characters, In which order will this
computer sort the following strings :
2B, AL, 1A?
Example 4.11
Solution :
In EBCDIC, numeric characters are treated t0 be
‘greater than alphabetic characters, Hence the mumeric
characters will be placed after the alphabetic characters. So
the computer will eat the given string as :
AlcIA23
Hence the sorted sequence will be: Al, 1A, 23.
@ scanned with OKEN Scanner32
Toble 4.7, Numeric And
Character ASCII-R Code
Alphabetic:
Characters. In ASCIL® Notation
Along With ‘Their Hexadecimal
Equivalent
| Zone Digit
| eee
| 00101 00050.
| 1 101 0001 51
| 2 0101 0010 52
| 3 o101 oon x)
4 101 0100 54
5 0101 0101 55
6 onor 01056
7 0101 oll 37
8 O11 100058
2 O10 100159
A 1010 0001 Al
B 1010 0010 a2
c 1010 oo a3
D 1010 0100 ag
E 1010 O11 As
F 1010 0110 AG
G 1010 our a7
H 1010 1000 Ag.
I 1010 1001 Ag
I 1010 1010 AA
K 1010 1011 AB
L 1010 1100 AC
M 1010 101 aD
N 1010 M10 AE
oO 1010 1 AF
P ton 0000 Bo
Q 1011 001s
R101 0010 Ba
S101 0011 B3
piece 0100 Bg
Uo 0101 Bs
Vv yo11 0110 BG
Woon our B7
X 10 1000 Bg
Yip. p., 1011 1001 Bg
Z 1011 1010 BA
F = |
Example 4.12 Suppose a computer,
its internal ‘epee Ao,
characters. In which getig
COMPUEE Ort the Foyt
23, AL, 1A? wing
Solution
In ASC, numeric characters ae seagy
than alphabetic characters. Hence the numer’ t b
will be place before the alphabetic chrage
computer will treat the given string as, Bctaeay
Hence the sorted sequence will be: 25, A
QUESTIONS
1. Define the term “byte’. What is the lite,
between a bit and a byte ?
2. Write the 4-bit BCD code for the folloy
numbers : (a) 25, (b) 64,5 (c) 128,, (4) 1024,
3. Using binary notation, show the BCD covi
the following words : (a) BIT () By
(© CODE.
4. Using octal notation, show the BCD coding
the following words : (a) COMPUTER
INPUT (c) VIDEO.
5. Why was BCD code extended to EBCDIC?
§. How many different characters are possible
the following codes: BCD, EBCDIC, ASCIL
and ASCI-8?
7, Suppose a new computer code is designed tt
uses 9 bits. How many different characters @
Possible in this code ?
8. Why are octal and hexadecimal shorts
Rotations used Identify the shortcut notatct
used for each of these computer codes: BCL
EBCDIC, ASCII-7, and ASCIL-8,
9% ‘Why do we have a packed decimal format ? Hot
does fer from a zoned decimal format?
@ scanned with OKEN Scanner10. Using binary notation, write the EBCDIC eaxtin
for the following words : (a) SUN (b) MOON,
(©) AT. How many bytes are required for cach of
these representations ?
11, Using hexadecimal notation, write the EBCDIC
coding for the following words : (a) PROGRAM.
(b) OUTPUT (c) BYTE. How many bytes are
required for each of these representations ?
12,Using hexadecimal notation, write the zoned-
decimal coding for the following numbers :
(@) 1256 (b) +439 (©) -63, How many bytes will
be required for each of these representations ?
13, Using hexadecimal notation, write the packed-
decimal coding for the following numbers :
(@) 12915 (b) +9876 (c) 872.
How many bytes will be required for each of
these representations ?
14, List out the similarities and differences between
‘7-bit and 8-bit ASCII.
15. Using binary notation, write the ASCII-7 and
ASCIL-8 codes for the foilowing words :
(a) DRY (b) WET (c) DAMP.
Computer Codes 33
How many bytes are required for each of these
representations ?
16, Using hexadecimal notation, writs the ASCH-7
dl ASCILK codes for the following, words :
(a) PRINT (by TYPE (©) RUB.
How many bytes are required for each of these
representations ?
ating
17.Explain the meaning of the term
sequence”.
18.A. computer uses EBCDIC as its internal
representation of characters. In which order will
this computer sort the following strings : ABC,
123,245, ADD?
19. A computer uses ASCII, In which order will this
computer sort the following strings: BED, 512,
ADD, 128, BAD?
20. Give the full form of the following abbreviations
BCD, EBCDIC, ASCII.
@ scanned with OKEN Scanner