0% found this document useful (0 votes)
125 views44 pages

FAC1002 - Computer Codes

This document discusses computer codes and binary number representation. It covers: 1) Computers use binary while humans use decimal, so computers convert between the bases. Binary represents data as strings of 0s and 1s while codes are needed for letters and characters. 2) There are different ways for computers to represent numerical data in binary format, including straight binary and binary-coded decimal (BCD). BCD codes make conversion between decimal and binary easier compared to straight binary. 3) BCD codes like 4-bit and 6-bit BCD assign a unique binary pattern to each decimal digit, allowing for easier arithmetic operations compared to straight binary. However, BCD requires more bits than straight binary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
125 views44 pages

FAC1002 - Computer Codes

This document discusses computer codes and binary number representation. It covers: 1) Computers use binary while humans use decimal, so computers convert between the bases. Binary represents data as strings of 0s and 1s while codes are needed for letters and characters. 2) There are different ways for computers to represent numerical data in binary format, including straight binary and binary-coded decimal (BCD). BCD codes make conversion between decimal and binary easier compared to straight binary. 3) BCD codes like 4-bit and 6-bit BCD assign a unique binary pattern to each decimal digit, allowing for easier arithmetic operations compared to straight binary. However, BCD requires more bits than straight binary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Programming I

Computer Codes

Ruhaila Md Kasmani
Pusat Asasi Sains, Universiti Malaya
 We naturally live in a base 10 environment
 Computer exist in a base 2 environment
 So give the computer/digital system the task of doing the
conversions for us.

Human input Converts to Convert back Display on


base 10 base 2 to base 10 screen
 Binary numbers are represented by a string of 0’s and 1’s.
 What about other characters such as R, M, N, +, 1 etc..?
 We need appropriate codes to represent letters, special
character and numerals.

Computer
Codes

Binary Format
 HOW DOES A COMPUTER REPRESENT THE NUMERICAL
DATA IN BINARY FORMAT?
(1) Base-2 digits [Straight Binary]
(2) BCD [Binary-Coded Decimal]

Straight Previous
Binary Lecture (MZ)

Binary 4-bit BCD

BCD Codes 6-bit BCD

8-bit BCD
4-Bit BCD CODES

BCD Codes

4-Bit 6-Bit 8-Bit

Non-Weighted
Weighted 8-4-2-1 BCD
XS-3

Conversion Addition Conversion


4-Bit BCD Code
Decimal 4-Bit BCD Code
 4-bit BCD can be arranged
into 10 different possible 0 0000
ways. 1 0001
 This arrangement is used 2 0010
to represent decimal digits 3 0011
0-9.
4 0100
 Table 1 shows the binary
5 0101
representation of 4-bit
BCD 6 0110
7 0111
8 1000
9 1001
Comparison Between BCD Codes and Straight Binary

BCD Codes Straight Binary

Easy conversion between decimal to Tedious conversion between decimal to


BCD code and BCD to decimal. binary and binary to decimal.

Example: Convert 3.37510 to BCD Example: Convert 3.37510 to BCD


3.37510 = 0011. 0011 0111 0101
2 3 1 0.375
2 1 1 x 2 Thus, the
refer the 4-bit BCD table. 0 [0].75 answer is
x 2 11.0112
[1]. 5
x 2
[1].0
Comparison Between BCD Codes and Straight Binary

BCD Codes Straight Binary


Rounding at a floating point digit is Rounding at a floating point digit is
simple. hard.

Example: 0.210 = 0000. 0010 Example: 0.210 = 0.001100110011..…2


(no round off error) (has round off error)

Very useful in digital systems whenever Not appropriate to use in digital system
floating point number information is
given either as inputs or displayed as
outputs.

Examples: Digital clocks, digital


voltmeters
Comparison Between BCD Codes and Straight Binary

BCD Codes Straight Binary


BCD codes require more bits than Straight binary requires less bits than
straight binary. BCD codes.

Examples: Examples:
(1) 410 = 0100 (4-bits) (1) 410 = 100 (3-bits)
(2) 1210 = 0001 0010 (8-bits) (2) 1210 = 1100 (4-bits)
4-Bit BCD : Conversion
Example 1: Convert the following decimal numbers to
its equivalent 4-bit BCD code

a) 23410 = 0010 0011 0100


b) 709310 = 0111 0000 1001 0011
c) 8610 = 1000 0110
d) 947210 = 1001 0100 0111 0010

 Note that BCD is not equivalent to binary


o Example: 23410 = 111010102
4-bit BCD Arithmetic : Addition
Example 2: Calculate 410 + 110 in 4-bit BCD.
410 = 0100 and 110 = 0001 in 4-bit BCD.

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

o 0101 = 510 is still within a 4-bit BCD representation.


4-bit BCD Arithmetic : Addition
Example 3: Calculate 310 + 310 in 4-bit BCD

310 = 0011 in 4-bit BCD.

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

o 0110 = 610 is still within a 4-bit BCD representation.


4-bit BCD Arithmetic : Addition
Example 4: Calculate 510 + 510 in 4-bit BCD
510 = 0101 in 4-bit BCD.
So, we must do the modification
1 1 by adding 610 = 0110 in the
0 1 0 1 calculation.

+ 0 1 0 1 1 1 1
1 0 1 0 1 0 1 0
+ 0 1 1 0
1 0 0 0 0
But 1010= 1010 is NOT in
the list of 4-bit BCD The answer is 0001 0000 which is
representation. equivalent to 1010.
Why Do We Add 6 (0110) in 4-bit BCD Addition

Four binary digits count up to 15 (1111) but in BCD we only


use the representations up to 9 (1001). The difference
between 15 and 9 is 6.

If the sum of two digits is < 10, BCD addition is similar to


binary addition.

But sometimes if the sum is > 9, the result requires a


correction. This corrections is +( 0110).
4-bit BCD Arithmetic : Addition
Example 5: Calculate 710 + 610 in 4-bit BCD
710 = 0111 and 610 = 0110 in 4-bit BCD.
1 1
But 1101= 1310 is NOT in the
0 1 1 1 range of 4-bit BCD
representation.
+ 0 1 1 0
1
So, we must do the
1 1 1 0 1 modification by adding
(Md) 0 1 1 0 610 = 0110 in the
calculation.
1 0 0 1 1

The answer is 0001 0011 which is


equivalent to 1310.
4-bit BCD Arithmetic : Addition
Example 6: Calculate 41710 + 19510 in 4-bit BCD
41710 =0100 0001 0111 and 19510 = 0001 1001 0101.

1 1 1 1 1 1 1
0 1 0 0 0 0 0 1 0 1 1 1
+ 0 0 0 1 1 0 0 1 0 1 0 1
0 1 1 0 1
1 1
0 1 1 11 1 0 0
Md 0 1 1 0 0 1 1 0
0 1 1 0 0 0 0 1 0 0 1 0

The answer is 0110 0001 0010 which is equivalent to 61210.


Categories of 4-Bit BCD Codes
8-4-2-1 Code

Weighted
4-3-1-1 Code
Codes

4-Bit BCD 2-4-2-1 Code


Codes

XS-3 Code
Non-Weighted
Codes
Gray Code
4-BIT BCD CODES
DECIMAL DIGITS
Weighted 8-4-2-1 Non-Weighted XS-3
0 0000 0011
1 0001 0100
2 0010 0101
3 0011 0110
4 0100 0111
5 0101 1000
6 0110 1001
7 0111 1010
8 1000 1011
9 1001 1100
Table : the binary representation for 4-bit codes in XS-3
Non-Weighted XS-3 BCD : Conversion
Example 7: Find the equivalent value of 46910 in
non-weighted XS-3 BCD.
46910 = 0100 0110 1001 in weighted 8-4-2-1 BCD.
To obtain XS-3 BCD, we add 0011 for each digit of 8-4-2-1 BCD.

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

Thus, 46910 = 0111 1001 1100 in non-weighted XS-3 BCD.


6-Bit BCD CODES

BCD
Codes

4-Bit 6-Bit 8-Bit

Representation Check Bit

Numeric Even
Zone Bits Odd Parity
Bits Parity
6-bit BCD Codes
Introduction

Character Encoding 64 Characters


A six-bit character code is Six bits can only encode 64
a character distinct characters.
encoding designed for use
on computers with unit of
data of multiple 6.
Type of Characters
IBM These characters generally
include only the upper-case
Six-bit BCD was used
letters, the numerals, some
by IBM on early computers
punctuation characters, and
such as the IBM 704 in 1954.
sometimes control
characters.

Why 6-Bit
4-bit BCD codes are Parity Bit
inadequate for representing
Six-bit BCD codes come
and handling non-numeric
with additional parity bit.
data. For this purpose, 6-bit
BCD code has been
developed.
 Memory cells representation of 6-bit BCD code can be
divided into two which are zone bits and numeric bits.

B A 8 4 2 1 6-bit cells

Zone Bits Numeric Bits

 Zone Bits – combinations of two-binary digits


 Numeric Bits – combinations of 4-binary digits
DECIMAL DIGITS 6-BIT BCD CODES

8-4-2-1
0 00 0 0 00
1 00 0 0 01
2 00 0 0 10
3 00 0 0 11
4 00 0 1 00
5 00 0 1 01
6 00 0 1 10
7 00 0 1 11
8 00 1 0 0 0
9 00 1 0 0 1

Table : the binary representation for 6-bit codes


DECIMAL DIGITS 6-BIT BCD CODES

8-4-2-1
A 01 0 0 01
B 01 0 0 10
C 01 0 0 1 1
D 01 0 1 0 0
E 01 0 1 0 1
F 01 0 1 1 0

Table : the binary representation for 6-bit codes


 Parity bit
o Definition: A parity bit, or check bit, is a bit that is added
as the leftmost bit to a string of binary code to ensure
that the total number of 1-bits in the string is even or
odd.
o Even parity: additional bit added to make
total number of 1’s even.
o Odd parity: additional bit added to make total number
of 1’s odd.

CS2100 Number Systems and Codes


MEMORY CELLS REPRESENTATION
Zone
Bits Numeric Bits

B A 8 4 2 1 6-bit cells

C B A 8 4 2 1
7-bit cells

Check Numeric Bits


Zone
Bit
Bits
RULES

Check Bit –Two variants of parity bits: even parity bit and odd parity bit

 Zone Bits – combinations of two-binary digits

Numeric Bits – combinations of 4-binary digits


Table 1 : 6-Bit BCD Code
6-Bit BCD Code : Parity
Example 9:
The following table shows the zone bits and numeric bits of 6-bit BCD
code for the characters, 7PW9. Complete the check bit column if the
computer uses an odd parity check to encode the characters.

Character Check bit Zone bit Numeric bit


7 ? 00 0111
P ? 10 0111
W ? 01 1100
9 ? 00 1001
6-Bit BCD Code : Parity
Example 9:

Solution : Odd parity

Character Check bit Zone bit Numeric bit Total number of 1’s
7 0 00 0111 3 (odd)  check bit is 0
P 1 10 0111 4 (even)  check bit is 1
W 0 01 1100 3 (odd)  check bit is 0
9 1 00 1001 2 (even)  check bit is 1
6-Bit BCD Code : Parity
Example 9:
The following table shows the zone bits and numeric bits of 6-bit BCD code
for the characters, EURO2016. If the computer uses an even parity check to
encode the characters, find the check bit for each character.

Character E U R O 2 0 1 6
Check bit
Zone bits 11 01 10 10 00 00 00 00
Numeric bits 0101 0100 1001 0110 0010 1010 0001 0110

Solution:

Check bit 0 0 1 1 1 0 1 0
8-Bit BCD CODES

BCD
Codes

4-Bit 6-Bit 8-Bit

Representation Examples

Zone Numeri
EBCDIC ASCII
Bits c Bits
WHY 8-BIT BCD CODES WERE BEING CREATED?

TO HANDLE MORE (SPECIAL) CHARACTERS IN MODERN DATA


PROCESSING. (Lowercase 26 alphabet characters)

 IT CAN HANDLE UP TO 28 = 256 CHARACTERS

MEMORY CELLS REPRESENTATION

Z Z Z Z 8 4 2 1

Zone Numeric Bits


Bits
TWO STANDARDS OF 8-BIT BCD CODES IN COMPUTER INDUSTRY

>>EBCDIC (Extended Binary-coded Decimal Interchange Code)


i) An extension of 6-bit BCD
ii)IBM mainframe and IBM mid range computer operating
systems.

>>ASCII (American Standard Code for Information Interchange)


i) Extension of the 7-bit BCD
ii) For non-IBM computers
Table 2 : 8-Bit EBCDIC Code
Table 3 : The very basic ASCII Code
COMPUTER CODES
Computer
Codes

Binary Format

Straight Zoned Packed


BCD Codes
Binary Decimal Decimal

Previous
Lecture (MZ)
Zoned Decimal & Packed Decimal Formats
 Computer code format is used to identify the sign of decimal numbers.

 There are two formats for decimal numbers:


1. Zoned Decimal
2. Packed Decimal

 The coding sign is:

Code (Our syllabus use


Sign
EBCDIC code for sign)
+ 1100
– 1101
Unsigned 1111
Zoned Decimal & Packed Decimal Formats

Zoned Decimal Format Packed Decimal Format

Each byte of storage is divided into Each byte of storage (except for the
two portions: a 4-bit zone portion low order byte) can contain decimal
and a 4-bit digit portion numbers

The sign portion of the low-order


The zone portion of the low-order
byte indicates whether the numeric
byte indicates the sign (unsigned,
value represented in the digit
positive or negative) of the decimal
portions is unsigned, positive or
number.
negative
Zoned Decimal & Packed Decimal Formats

Figure 1: Zoned Decimal Format

Zone Digit Zone Digit Zone Digit Sign Digit

8 bit = 1 byte

Figure 2: Packed Decimal Format

Digit Digit Digit Digit Digit Sign

byte
Zoned Decimal Format
Example 10 :
Encode the numbers 275, +275 and –275 in
zoned decimal format.
275

1111 0010 1111 0111 1111 0101


2 7 5
Sign indicator
Zoned Decimal Format
+ 275

2 7 + 5

1111 0010 1111 0111 1100 0101

– 275

2 7 – 5

1111 0010 1111 0111 1101 0101


Packed Decimal Formats
2 7 5 Unsigned

Example 11:
0010 0111 0101 1111
Illustrate the packed-
decimal format of
2 7 5
memory cell in computer
+
for the number 275,
0010 0111 0101 1100 +275 and −275

2 7 5 –

0010 0111 0101 1101


Zoned Decimal & Packed Decimal Formats
Example 12: Encode +21544 in zoned decimal format and packed decimal format.

Zoned decimal format for +21544:

2 1 5 4 + 4
1111 0010 1111 0001 1111 0101 1111 0100 1100 0100

5 bytes

Packed decimal format for +21544:

2 1 5 4 4 +

0010 0001 0101 0100 0100 1100

3 bytes
COMPUTER CODES

Computer
Codes

BCD
Formats
Codes

Zoned Packed
4-Bit 6-Bit 8-Bit
Decimal Decimal

Weighted Non-
8-4-2-1 Weighted Representation Check Bit Representation Examples
BCD XS-3 BCD

Conver Conver Numeric Odd Even Numeric


Addition Zone Bits Zone Bits EBCDIC ASCII
sion sion Bits Parity Parity Bits

You might also like