Binary Codes
Binary Code
A binary code is just an
assignment of information to bit
patterns.
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.
Binary Codes
• To represent a group of 2n distinct
elements in a binary code requires a
minimum of n bits.
• There is no maximum number of bits
that may be used for a binary code.
Binary Codes for Decimal Numbers
• A set of n-bit string in which different bit
strings represent different numbers or
other things is called a code.
• A particular combination of n bit values
is called a code word.
• A code that uses n-bit strings need not
contain 2n valid code words.
Binary Codes for Decimal 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. Table 1 in the course text
presents five such codes. These are BCD,
2421, Excess-3, Biquinary (5043210) and
lastly the “1-out-of-10” code.
Binary Codes for Decimal Numbers
TABLE-1
Binary Codes for Decimal Numbers
• It is important to note that this table
presents binary codes and not binary
numbers.
• A binary number is mathematically
defined, while a binary code is just an
assignment of numeric values to bit
patterns.
Binary Codes for Decimal Numbers
• Each such assignment in table 2-9 does
have some particular property associated
with it that makes it a reasonable method
of assignment.
• For example, a 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)
• A BCD number is just a natural binary
encoding of the decimal digits from 0 to 9
on four bits.
0101 0111
59 in BCD (0 ~ 99)
because there are unused code words
87 in normal unsigned binary number (0 ~
255)
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.
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
00112.
0010 = 2 in BCD
+ 00112
= 0101 = 2 in excess-3
1-out-of-10 code
• This code is is the sparsest encoding of
decimal digits, using 10 out of 1024
possible 10-bit code words.
10000 00000 = 0
01000 00000 = 1
00100 00000 = 2
• The redundant bits gave this code the
error-detecting property.
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
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.)
Gray Code
20
Binary Gray
Exam
ple:
Binary
:
Gray-to-binary:
Gray:
• bi = gi if no. of 1’s
preceding gi is even
• bi = gi’ if no. of 1’s
preceding gi is odd
21
Reflection of Gray Codes
22
Character Codes
• Many applications of digital computers require
the handling of data not only of numbers, but
also of letters.
• 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.
ASCII Code
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.
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 2n
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
Digital Encoding of Digital Data
• Unipolar (the voltage always positive or negative, 0v for 0 and 5v for 1)
• Bipolar (fewer errors than unipolar signaling as changing the polarity of
a current/voltage is more difficult than changing its magnitude, -5v for 0,
+5v for 1)
• RZ (return to zero)
• NRZ (non return to zero)
• NRZ-L (NRZ, level)
• NRZ-I (NRZ, invert on ones)
• Biphase (all of the biphase techniques require at least one transition per
bit time and may have as many as two transitions.
• Manchester (used in Ethernet LANs) - The midbit transition
serves as a clocking mechanism and also as data.
• 0 - a high-to-low represents a 0, and
• 1 - A low-to-high represents a 1.
• Differential Manchester (used in Token-ring LANs) - The
midbit transition is used only to provide clocking.
• 0 - A transition at the beginning of the bit period represents 0, and
• 1 - an absence of a transition at the beginning of the bit period
represents 1.
Serial Line codes
Serial Line codes
Serial Line codes
Binary Coded Decimal (BCD)
Recall: Binary Coded Decimal
Decimal BCD
0 0000
1 0001
Binary Coded Decimal (BCD) 2 0010
– Each Decimal Digit is 3 0011
represented 4 0100
by 4 bits 5 0101
– (0 – 9) ⇨ Valid combinations 6 0110
– (10 – 15) ⇨ Invalid 7 0111
8 1000
combinations
9 1001
37
BCD Addition
One decimal digit + one decimal digit
– If the result is 1 decimal digit ( ≤ 9 ), then it
is a simple binary addition
5 0101
Example:
+ 3 + 001
1
8
– If the result is two decimal 1digits
0 0 0( ≥ 10 ),
then binary addition gives invalid
combinations
Example: 5 0101
+ 5 + 010
1 0 1
0001 000
38
BCD Addition
If the binary result 5 0101
is greater than 9,
correct the result by + 5 + 010
adding a “6” 10 1
1010
+ 011
Multiple Decimal
0001 0000
Digits 0
Two Decimal
351 Digits
0011 0101 0001
39
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
you want 9+1 to produce 10, which is 1
0000, you have to add 6 to make 1010
wrap to 1 0000.
• It is done to skip the six invalid states of
binary coded decimal i.e from 10 to 15
and again return to the BCD codes.
40
BCD Arithmetic
8 100 Eigh
+ +010
0 Plus
t
1
5 110
1 is
Five13 (>
▪ Note
3 that the
1 result9) is MORE THAN 9, so
must be
▪ To correct
represented by two digits!
the digit,
8 add 6 100 Eigh
+ +0101
0 Plus
t
1
5 110 is513 (>
3 +0110
1 so
9)
carry = 001 leaving
add 6 3 +
1 0001 |1 Finalcy answer (two
0011 digits)
41