0% found this document useful (0 votes)
19 views

Lecture 13#CSE1012-2

Uploaded by

astroprogram111
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Lecture 13#CSE1012-2

Uploaded by

astroprogram111
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Lecture 13

What will cover today


• What is BCD?
• ASCII Code & EBCDIC code
• Excess-3 Code
• Gray Code
• Unicode
What is Binary-Coded Decimal (BCD)?
Binary-Coded Decimal (BCD) is a method of
representing decimal numbers (0-9) in a binary format where each digit of
the decimal number is represented as a 4-bit binary value.
This is different from directly converting the entire number to binary.
For example:
Decimal 12 in BCD is represented as 0001 0010 (1 = 0001, 2 = 0010).
Decimal 259 in BCD is represented as 0010 0101 1001.
BCD is useful in systems where decimal precision is important, such as in
calculators, digital clocks, and financial applications.
Packed and Unpacked BCD
1. Packed BCD:
In Packed BCD, two decimal digits are stored in a single byte (8 bits). Each
digit takes 4 bits (nibble):
The first 4 bits represent the higher decimal digit.
The last 4 bits represent the lower decimal digit.
For example:
Decimal 25 is stored as 0010 0101 in Packed BCD (2 = 0010, 5 = 0101).
Decimal 1234 would require 2 bytes: 0001 0010 0011 0100.
Packed BCD is memory-efficient because it uses 1 byte to store 2 decimal
digits.
2. Unpacked BCD:
In Unpacked BCD, each decimal digit is stored in a separate byte.
The lower nibble (4 bits) contains the digit, and the upper nibble is
usually filled with zeros.
For example:
Decimal 25 is stored as 0000 0010 and 0000 0101 in Unpacked BCD
(2 = 0000 0010, 5 = 0000 0101).
Decimal 1234 would require 4 bytes: 0000 0001 0000 0010 0000
0011 0000 0100.
Unpacked BCD is easier to process but less memory-efficient compared
to Packed BCD.
Why Use BCD?

BCD is used in applications where:


Decimal Precision is Critical: BCD ensures that numbers are handled
in base 10, avoiding binary rounding errors.
Ease of Conversion to Human-Readable Form: BCD is straightforward
to convert to decimal digits for display.
Arithmetic Operations on Decimals: Many hardware systems use BCD
for efficient and precise arithmetic operations on decimal numbers.
Legacy Systems: Some older systems use BCD for compatibility.
How Packed and Unpacked BCDs are Used:

Packed BCD is commonly used in memory storage or data transfer to save space.
Unpacked BCD is often used in processing and when interfacing with hardware
like 7-segment displays, where each digit is handled individually.

Summary of Advantages and Disadvantages:


Advantages Disadvantages
Easy conversion to decimal format Less memory-efficient than binary
Eliminates binary rounding errors Requires additional hardware/software for
arithmetic operations
Suited for human-readable outputs Processing BCD is slower compared to binary
ASCII
 Stands for American Standard Code for Information Interchange
 Seven bit character code
 can define 128 different characters because it is a 7-bit code, and can
support 27 combination
 ASCII characters are examples of unpacked BCD numbers
 Values in ASCII codes are represented as their 4-bit binary equivalents
stored in the lower nibble, while the upper nibble contains 011
 Most common format for text files in computers and on the Internet

CSE 1010 (Programming I) - MIIT 8


ASCII
• ASCII is of two types – ASCII-7 and ASCII-8
• ASCII-7 uses 7 bits to represent a symbol and can represent 128 (27) different
characters
• ASCII-8 uses 8 bits to represent a symbol and can represent 256 (28) different
characters can be called Extended Binary Coded Decimal Interchange Code

CSE 1010 (Programming I) - MIIT 9


12/25/2024 MIIT, Mandalay, CSE-1010 Programming I 10
• To convert the word "BOY" into ASCII7 (7-bit ASCII) and ASCII8 (8-bit
ASCII) representations, we follow these steps:
ASCII-7 Coding Scheme

CSE 1010 (Programming I) - MIIT 12


ASCII-8 Coding Scheme

CSE 1010 (Programming I) - MIIT 13


Excess-3 Code
• The Excess-3 (XS-3) code is a non-weighted binary code used to
express decimal numbers. It is obtained by adding 3 to the decimal
number and then converting the result into its 4-bit binary
equivalent.
• This code is useful in digital systems for error detection and handling.
Steps to Encode in Excess-3 Code
• Start with the decimal number.
• Add 3 to the decimal number.
• Convert the result to a 4-bit binary number.
4221 Code- Other weighted methods-8421
2421- 5211-
• The 4221 code is a weighted binary code where each digit in the 4-bit
binary representation has weights of 4, 2, 2, and 1 respectively. This is
different from standard binary codes due to the duplication of the
weight 2.
• Each decimal digit (0–9) is represented by a unique combination of
these weights such that their sum equals the decimal digit.
4221 Code Characteristics
• Only decimal digits (0–9) are represented.
• Each 4-bit code's weighted sum equals the decimal
number.
• It is primarily used in digital systems and error
detection.
• Steps to Calculate 4221 Code
• Decompose the decimal digit into the sum of 4, 2, 2,
and 1 based on the weights.
• Assign binary 1 for the weights used and 0 for the
weights not used.
Gray Code
• Gray Code, also known as Reflected Binary Code, is a type of binary
code where two successive values differ by only one bit. It is widely
used in digital systems, such as error correction, communication, and
encoding systems, where minimal bit change is necessary to reduce
errors.
Characteristics of Gray Code
• Single-bit change: Consecutive Gray Code values differ in only one bit.
• Cyclic: The code forms a cycle, meaning the last value transitions to
the first with one-bit difference.
• Applications: Commonly used in rotary encoders, Karnaugh maps,
and error correction.
• How to Convert Binary to Gray Code
• To convert binary to Gray Code:
• The most significant bit (MSB) of Gray Code is the same as the MSB
of Binary.
• For other bits, XOR each binary bit with the bit to its left.
• Formula:
• Applications of Gray Code
• Rotary Encoders: To avoid errors in mechanical transitions where
multiple bits may change at once.
• Karnaugh Maps: To simplify boolean expressions by minimizing bit
changes.
• Error Detection: Used in digital systems to reduce the likelihood of
errors during transitions.
What is Unicode?

• Unicode is a universal character encoding standard that assigns a


unique number (code point) to every character in all writing systems,
symbols, and emojis, regardless of the platform or language. Unicode
ensures that text is consistently represented and manipulated across
different systems.
• Code Point: Each character is represented by a code point written
as U+XXXX, where XXXX is a hexadecimal value.
What is UTF-8?
• UTF-8 (Unicode Transformation Format - 8-bit) is a variable-length encoding system for Unicode.
It can encode all Unicode characters and is backward-compatible with ASCII. UTF-8 uses 1 to 4
bytes depending on the character's code point:
• 1 byte: For ASCII characters (U+0000 to U+007F)
• 2 bytes: For characters in U+0080 to U+07FF
• 3 bytes: For characters in U+0800 to U+FFFF
• 4 bytes: For characters in U+10000 to U+10FFFF
1. Why does 1 byte use 7 bits in UTF-8?
A byte is made up of 8 bits, but in UTF-8 encoding, the first bit (the most significant bit) is reserved
to indicate whether the byte is part of a multi-byte sequence or the beginning of a character. This is
part of the encoding structure.
Why does 2 bytes use 11 bits?
When UTF-8 needs more than one byte to represent a character (for characters outside the ASCII
range, like those in European and other extended languages), the first byte uses the two highest bits
(110), while the second byte uses the leading bits as 10. The rest of the bits represent the character
data.
• The first byte uses 5 bits for the data (since 3 bits are used for indicating that it’s part of a multi-
byte sequence), and the second byte uses 6 bits to represent the data.
Reference
• Subramanian, K. R. V., & Subramanian, U. , “ Introductory problem
solving and programming (using C). “Courseware prepared especially
for Myanmar Institute of Information Technology, Mandalay, 2017
• Reema Thareja, “Fundamentals Of Computers”, Oxford University
Press, 2015
• Online content

You might also like