Lecture 11
Computer Codes
Adapted from
Digital Electronics Principles & Applications
Fifth Edition
Content
Computer Codes • Seven-segment
• The BCD Code Displays
• The Excess-3 Code • Seven-segment LED
• The Gray Code Displays
• The ASCII Code • BCD-to-Seven-
• Encoders Segment Decoder/
Driver
Binary Codes
• A code is a systematic way of representing
data.
• A binary code is just an assignment of
information to bit patterns
• Digital devices use a variety of codes to
represent, manipulate or correct data.
Binary Codes
• Electronic digital systems use signals that have two
distinct values and circuit elements that have two
stable states.
• Digital systems represent and manipulate not only
binary numbers, but also many other discrete
elements of information.
• Any discrete element of information distinct among a
group of quantities can be represented by a binary
code.
• Binary codes merely change the symbols, not the
meaning of the elements of information that they
represent.
Characteristics
• Weighted Codes
• Non-weighted Codes
• Reflective Codes
• Sequential Codes
• Alphanumeric Codes
• Error Detecting and Correcting Codes
Weighted Codes
• They obey a positional weight system
• A specific weight is assigned to each
position of the number. Recall (H T U) ?
• Examples of weighted codes are Binary and
BCD.
• Weighted codes are used for Data
manipulation, Data storage, to represent the
decimal digits in calculators, volt meters
etc.
Non-weighted Codes
• These codes do not obey positional weight
principle
• In these codes, the digit value does not
depend upon their position
• Examples include Excess-3 code, Gray
code.
• Non weighted codes are used, to perform
certain arithmetic operations, for error
detecting purposes
Reflective vs Sequential Codes
• A code is said to be reflective when it’s
code for 9 is the complement of it’s code for
0. The excess-3 (XS-3) code is an example.
• A code is said to be sequential when each
succeeding code is one binary number
greater than preceding code. Example
Binary, XS-3
Alphanumeric Error Detecting and
Codes Correcting Codes
These are codes These are codes used
designed to represent to detect and correct
numbers as well as digital transmission
alphabetic characters errors. Examples
and other symbols. Parity code,
E.g. ASCII, EBCDIC Hamming code.
Binary Numbers
• The usual interpretation of a binary number is as
defined according to the definition of a number in
in base-2 system.
• There are, however, alternate methods used to
encode numeric data into binary bit patterns.
• NOTE: A binary number is mathematically
defined, while a binary code is just an assignment
of numeric values to bit patterns.
Binary Coded Decimal (BCD) Numbers
• BCD number is just a natural binary encoding of
the decimal digits from 0 to 9 on four bits.
• Therefore a string of bits is grouped into groups
of four bits, and interpreted as a string of decimal
digits.
Binary-Coded Decimal (BCD)
• Binary-Coded Decimal is a weighted code
because each decimal digit can be obtained from
its code word by assigning a fixed weight to each
code-word bit.
• The weights for the BCD bits are 8, 4, 2, and 1,
and for this reason the code is sometimes called
the 8421 code.
THE 8421 BCD CODE
• BCD stands for Binary-Coded Decimal.
• A BCD number is a four-bit binary group that
represents one of the ten decimal digits 0 through 9.
Example:
Decimal number 4926 4 9 2 6
8421 BCD coded number 0100 1001 0010 0110
TEST
Convert the BCD coded number
1000 0111 0001 into decimal.
BCD Coded Number 1000 0111 0001
Decimal Number 8 7 1
TEST
Convert the decimal number
350 to its BCD equivalent.
Decimal Number 3 5 0
BCD Coded Number 0011 0101 0000
2421 code
• This code has the advantage that it is self-
complementing, that is, the code word for the 9s’
complement of any digit may be obtained by
complementing the individual bits of the digit’s
code word.
0010 = 2
9s’complement of 2 can be obtained by complementing
individual bits
1101 = (2+4+0+1) = 7
Excess-3 code
• This code is also self-complementing like 2421
code.
• Although this code is not weighted, it has an
arithmetic relationship with the BCD code.
• The code word for each decimal digit is the
corresponding BCD code word plus 0011 2.
0010 = 2 in BCD
+ 00112
= 0101 = 2 in excess-3
THE EXCESS-3 CODE
• Add 3 to each digit of decimal and
convert to 4-bit binary form
• A BCD code (not 8421 BCD)
Decimal Binary +3 Excess-3 Sample Problem:
0 0000 0011 0011
1 0001 0011 0100
2 0010 0011 0101 Decimal 3 5 9
3 0011 0011 0110
4 0100 0011 0111
5 0101 0011 1000
6 0110 0011 1001 Excess-3 0110 1000 1100
7 0111 0011 1010
8 1000 0011 1011
9 1001 0011 1100
Gray Code
• Gray code is a code where only one bit changes
at a time while traversing from 0 to any decimal
number in sequence.
• This is a useful property when converting analog
values into digital values, since it eliminates the
problem of misinterpreting asynchronous changes
to bits between valid values.
Gray Code
• What value will the encoder produce if the disk is
positioned right on the theoretical boundary?
Gray Code
• The encoding-disk problem can be solved by devising a digital
code in which only one bit changes between each pair of
successive code words. Such a code is called a Gray code.
THE GRAY CODE
• The Gray code’s most Decimal Gray code
important characteristic is that 0 00000
1 00001
only one digit changes as you 2 00011
increment or decrement the 3 00010
4 00110
count. 5 00111
• The Gray code is commonly 6 00101
7 00100
associated with input/output 8 01100
devices such as an optical 9 01101
10 01111
encoder of a shaft’s angular 11 01110
position. 12 01010
13 01011
• The Gray code is NOT a BCD 14 01001
code. 15 01000
16 11000
Gray Code
Gray Code
Two ways to construct a Gray code with any
number of bits:
• Reflected code defined recursively
• From binary code
Gray Code
Gray code is defined recursively using the following rules:
• A 1-bit Gray code has two code words, 0 and 1.
• The first 2n code words of an (n+1)-bit Gray code equal
the code words of an n-bit Gray code, written in order
with a leading 0 appended.
• The last 2n code words of an (n+1)-bit Gray code equal
the code words of an n-bit Gray code, written in reverse
order with a leading 1 appended.
Gray Code
Gray code is defined from the binary code using the
following rules:
• The bits of an n-bit binary or Gray-code code-word are
numbered from right to left, from 0 to n-1.
• Bit i of a Gray-code code-word is 0 if bits i and i+1 of
the corresponding binary code word are the same,
else bit i is 1.
(When i+1=n, bit n of the binary code word is
considered to be 0.)
Character Codes
Introduction
• Computers only understand binary coded
data (zeros and ones)
– 00000000, 11111111, 01010101
• People like to count in decimals
00000000=0, 11111111=255, 01010101=85
• 1st problem: it is extremely hard for people
to work with binary data
Introduction
• Since computers only work with numbers,
they need to use numbers to identify letters
to print or show on screen e.g.,
01000001=65=A
– people who don't read English also use
computers
• next problem: what kind of numbering
should be used for different languages?
Solution
• using binary data to display characters
• make up a "coding scheme" that assigns
characters to numbers
– ASCII code: 7-8 bits (1 byte)
– Unicode: 16 bits (2 bytes)
THE ASCII CODE
• ASCII is acronym for American Standard Code
for Information Interchange
• Represents numbers, letters, punctuation marks
and control characters
• Standard ASCII is a 7-bit code (127 characters)
• Extended ASCII (IBM ASCII), an 8-bit code, is
also very popular
• Extended ASCII adds graphics and math symbols
to code (total of 256 symbols)
ASCII Code
• used for teletypes before computers
– 128 characters in original ASCII
• 0 to 31 (decimal) control the machine
7 (BEL) rings bell
8 (BS) backspace key
10 (LF) line feed (go down 1 line)
13 (CR) carriage return (to left of page)
Java: '\n' = 10 and 13 together (2 bytes)
ASCII Characters
• A = 41 hex (65 decimal), Z = 5A h (90)
• a = 61 hex (97 decimal), z = 7A h (122)
– see calculator (String or ASCII choices)
• space character = 20 hex (32 decimal)
– see how space character code is used in browser
Address textbox
– ; (semicolon) = 3B hex (59 decimal)
Printable ASCII Characters
(space)
ASCII mage is from Wikipedia
ASCII Code
ASCII Numbers
• codes are for characters on screen and do
NOT equal the values of the characters
– Code numeric values can NOT be used in
calculations without adjustments
0 = 30 hex (ASCII 0 is really 48 decimal)
9 = 39 hex (57 decimal)
Unicode
• ASCII is a 7-8 bit encoding scheme
– 128-256 character limit
• Unicode is a 16-bit scheme
– Uni comes from the word universal (also from
Unix)
– can code 65,536 characters (actually more)
– Java uses Unicode encoding so that it can be
used for many different languages
Unicode - 2
• Unicode characters for many languages
– Western alphabets: Latin (English), Greek,
Cyrillic (Russian), etc.
– Unicode uses 0000000 + ASCII for English
• 00000000 01000001 = A (65 decimal)
– Asian characters: CJK (Chinese, Japanese,
Korean) has over 20,000 characters
• many character systems require installing special
fonts onto user's computer
Using Unicode in Java
char letter = 'A' ; //easiest way
char letter = '\u0041' ; // also = 'A'
char letter = '\u3220' ; // or '\u3280' ;
// 1 Chinese character for 1
– \ (backslash) = escape character
– \u means Unicode (#s are in hexadecimal)
char sound = '\u0007' ; // BEL
– sounds speakers when "printed" to screen
Reading Characters in Java - 2
• 2 characters sent when hit Enter key
CR (13) and then LF (10 decimal)
• when accepting keyboard input from DOS
window in Java, need to "absorb" both
characters from Enter keystroke
System.in.read(); System.in.read();
– reads characters, doesn't store (=) them
– program is now ready to read next input
Using Characters for Math
• numbers (characters) read from keyboard
have numeric values
• need to convert character's decimal value to
its mathematical value
• 0 = 30 h (48 decimal), 9 = 39 h (57)
• math value = decimal value – 48
int quantity = System.in.read() – 48 ;
code // notes
Character Codes
• Many applications of digital computers require the
handling of data not only of numbers, but also of letters
and symbols.
• The most commonly used character code is ASCII (the
American Standard Code for Information Interchange).
• ASCII represents each character with a 7-bit string,
yielding a total of 128 characters.
• The code contains the uppercase and lower case
alphabet, numeral, punctuation, and various nonprinting
control characters.
Other Codes
• Characters can be encoded according to variety
standards:
– Baudot code uses 5 bits; used for teletype
transmission
– ASCII (American Standard Code for Information
Interchange) code uses 7 bits; used in PCs.
– EBCDIC (Extended Binary Coded Decimal
Interchange Code) uses 8 bits; used by IBM
mainframes. It is an extension of BCD code.
– Unicode and ISO10646 use 16bits; Windows NT
supports Unicode.
ELECTRONIC ENCODER -
DECIMAL TO BCD
BCD output
Decimal input
00
1011
0
7
Decimal
5 to
BCD
3 Encoder
0
• Encoders are available in IC form.
• This encoder translates from decimal
input to BCD output.
SEVEN SEGMENT DISPLAYS
LED (light emitting diode) display
• Commonly red but available in other colors
• High power consumption
• Viewing best under low light conditions
LCD (liquid crystal display)
• Commonly dark characters on a silvery background
• Very low power consumption
• Viewing only OK in good light
• Backlighted displays can be viewed in low light
VF (vacuum fluorescent) display
• Commonly blue-green display (without filters)
• Medium power consumption
• Viewing good from dark to lighter conditions
• Based on vacuum tube triode technology
OPERATION OF A
7-SEGMENT LED DISPLAY
Inputs
7-segment code
a
7-segment
b & c inputs display
active H +5V
Note
reads
that
therefore
decimal
inputs 1 are L b
(segments
segments
“active-LOW”
bb&& cc L c
lightare
on lit)
display H
H
H
H
g
Limiting resistors Common anode
Cathode terminals (a thru g)
7-segment LED display
(common anode)
TEST
QQ#4-
#1-What
#2-
#3- Whatiswill
the be the decimal
decimal outputoutput
on the on the 7-segment
display LED
for the new set display?
of inputs?
Inputs
7-segment code
a
L
H a +5V
L
H bb
L
L cc
H
L d
H
L
H e
H
L
L ff
H
L
L gg g
DECODERS: BCD TO
7-SEGMENT DECODER/DRIVER
BCD input Decimal output
LED
01
0010
1
BCD-to-
7-Segment
Decoder/
Driver
• Electronic decoders are available in IC form.
• This decoder translates from BCD to decimal.
• Decimals are shown on an 7-segment LED display.
• This IC also drives the 7-segment LED display.
TEST
Q #5-
#1- What is the decimal output from the decoder that
#2-
#3-
#4-
appears on the 7-segment display?
Answer: 6
3
0
9
7
BCD input
Decimal output
010 100 010 011
0
BCD-to-
7-Segment
Decoder/
Driver ?
Codes for detecting
and correcting
errors
Lecture 13
Codes for detecting and correcting errors
• An error in a digital system is the corruption of data from its
correct value to some other value.
• i.e., a change of some bits from 0 to 1 or vice versa.
• During the processing or transmission of digital data a noise may
change some bits from 0 to 1 or vice versa.
• A short duration noise can affect only a single bit causes a single-
bit error.
• A long duration noise can affect two or more bits causes a multi-
bit error.
Codes for detecting and correcting errors
• Error-detecting codes normally add extra information to the data.
• In general, error-detecting codes contains redundant code.
• That is a code that uses n-bit strings need not contain 2 n valid code
words.
• An error-detecting code has the property that corrupting or
garbling a code word will likely produce a bit string that is not a
code word.
• Thus errors in a bit string can be detected by a simple rule - if it is
not a code word it contains an error.
Parity check
• One of the most common ways to achieve error detection is by
means of a parity bit.
• A parity bit is an extra bit included with a message to make the
total number of 1’s transmitted either odd or even.
• If an odd parity is adopted, the P bit is chosen such that the total
number of 1’s is odd.
Two dimensional codes
• LRC: Longitudinal Redundancy Checking
– LRC adds an additional character, the Block Check Character
(BCC) to the end of the message or block of data. The BCC uses parity on
each bit position for each character in the message; in other words, by
determining parity on the first bit of each character and setting the first bit
of the BCC to that value; then determining parity on the second bit of each
character, etc.
– Has an improved error detection rate of more than 98%.
Letter ASCII Parity Bit
D 1000100 1
A 1000001 1
T 1010100 0
A 1000001 1
BCC 1101111 1
Other codes
• Hamming codes - forward error correcting code
• CRC - cyclic-redundancy-check
• Checksum
Other codes
• CRC - cyclic-redundancy-check
– This method is used for most network error detection today. An 8, 16, 24 or 32 bit
number is added to the end of the data. Known as CRC-8, CRC-16, CRC-24 or
CRC-32 based on the number of bits used.
– All of the data in the message is processed according to a mathematical algorithm
(A polynomial is divided by a prime number resulting in a quotient and a remainder.
Uses a "linear feedback shift register", normally implemented with hardware to
generate a unique 16 or 32 bit remainder based on the data that was sent. ) The
remainder is transmitted as the CRC code. The receiver processes the incoming
data using the same algorithm and then compares it’s CRC with the one transmitted.
If they are the same, the data must be correct.
· CRC-16 detects 99.99% errors; that is 1 in 10,000 errors goes undetected.
· CRC-32 is extremely proficient in detecting errors; approximately 1 in 1014
errors go undetected.
• Checksum
– A checksum is calculated by adding up the decimal value of each character in the
message, dividing the sum by 255 and using the remainder as the checksum to be
transmitted with the message.