Computer Codes
Computer Codes
Computer Codes
Computer codes are used for internal representation of data in computers
As computers use binary numbers for internal data representation, computer codes use
binary coding schemes
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
As most modern coding schemes use 8 bits to represent a symbol, the term byte is often
used to mean a group of 8 bits
• 8 = 1000
• 9 = 1001
Numbers larger than 9, having two or more digits in the decimal system, are expressed digit by
digit. For example, the BCD rendition of the base-10 number 1895 is
1 8 9 5
1. It is easy to encode and decode decimals into BCD and vice versa. It is also simple
to implement a hardware algorithm for the BCD converter.
3. Digital voltmeters, frequency converters and digital clocks all use BCD as they display
output information in decimal
BCD Addition
BCD is a numerical code which has several rules for addition. The rules are given below in three
steps with an example to make the idea of BCD Addition clear.
1.At first the given number are to be added using the rule of binary. For example
BCD Addition
If the four bit result of addition is greater than 9 and if a carry bit is present in the result
then it is invalid and we have to add 6 whose binary equivalent is (0110)2 to the result of
addition. Then the resultant that we would get will be a valid binary coded number. In case
1 the result was (1111)2 which is greater than 9 so we have to add 6 or (0110)2 to it.
BCD Substraction
• At first 1’s compliment of the subtrahend is done, which is 1101 1110 1001 and is added to 0101 0100
0001. This step is called adder 1.
• Now after addition if any carry occurs then it will be added to the next group of numbers towards MSB.
Then EAC will be examined. Here, EAC = 1. So the result of addition is positive and true result of adder
1 will be transferred to adder 2.
• Now notice from LSB. There are three groups of four bit numbers. 1010 is added 1011 which is the
first group of numbers because it do not have any carry. The result of the addition is the final answer.
0 changed to 1
Received Sent
Types of Errors
• Burst error :-
- It means that two or more bits in the data unit has changed.
Sent
Received
Error Detection Codes
• Error detecting code is to include only enough redundancy to allow the receiver to deduce that an
error occurred, but not which error, and have it request a re- transmission.
• Error detection uses the concept of redundancy, which means adding extra bits for detecting
error at the destination
• In Single parity check, a parity bit is added to every data unit so that the total
number of 1s is even or odd.
11010011 1 Additional Bit added(PARITY BIT)
• use XOR and XNOR logic gates in circuits to generate parity bits
in practically (in devices).
How is the even parity bit generated?
Total number of ‘1’s should be even.
If the byte that we want to transmit is: 10 10 110 1
• Step 1:count the number of 1’s in the byte.
• Answer: 5
• Step 2: compute the parity value. 10 10 110 1 1
• Since the total number of 1’s is 5, the even parity bit will have a value of 1.
• If the number of bits are already even, the parity bit will be ‘0’.
How is the odd parity bit generated?
Total number of ‘1’s should be odd.
• The parity of the word is checked after reading it from memory. The data word is
accepted if the parity of the bits read out is correct. If the parity checked results in an
inversion, an error is detected, but it cannot be corrected.
ERROR CORRECTION
• An error‐correcting code generates multiple parity check bits that are stored with the data word
in memory.
• Each check bit is a parity over a group of bits in the data word.
• When the word is read back from memory, the associated parity bits are also read from memory
and compared with a new set of check bits generated from the data that have been read.
• If the check bits do not match the stored parity, they generate a unique pattern, called a
syndrome, that can be used to identify the bit that is in error.
Hamming Code
• k parity bits are added to an n ‐bit data word, forming a new word of n + k bits.
• Those positions numbered as a power of 2 are reserved for the parity bits. The remaining bits
are the data bits. The code can be used with words of any length.
• Consider, for example, the 8‐bit data word 11000100. We include 4 parity bits with the 8‐bit
• Bit position:
1 2 3 4 5 6 7 8 9 10 11 12
P1 P2 1 P4 1 0 0 P8 0 1 0 0
The Parity bits are
• The 8‐bit data word is stored in memory together with the 4 parity bits as a 12‐bit composite word.
• When the 12 bits are read from memory, they are checked again for errors. The parity is checked over
the same combination of bits, including the parity bit.
• Bit Position
0 0 1 1 1 0 0 1 0 1 0 0
1 2 2 4 5 6 7 8 6 10 11 12
• The Check bits are
• A 0 check bit designates even parity over the checked bits and a 1 designates odd parity.
• Since the bits were stored with even parity, the result, C = C8C4C2C1 = 0000, indicates that no error has
occurred.
• if C ≠ 0, then the 4‐bit binary number formed by the check bits gives the position of the erroneous bit.
• Bit position: 1 2 3 4 5 6 7 8 9 10 11 12
0 0 1 1 1 0 0 1 0 1 0 0 No error
1 0 1 1 1 0 0 1 0 1 0 0 Error in bit 1
0 0 1 1 0 0 0 1 0 1 0 1 Error in bit 5
C8 C4 C2 C1
• For no error: 0 0 0 0
• When the binary number C is not equal to 0000, it gives the position of the bit in error.
• The syndrome value C consists of k bits and has a range of 2k values between 0 and 2k - 1.
• One of these values, usually zero, is used to indicate that no error was detected, leaving 2k – 1 values
to indicate which of the n + k bits was in error.
2k – 1-K ≥n
Range of Data Bits for k Check Bits
Number of Check Bits, k Range of Data Bits, n
3 2–4
4 5–11
5 12–26
6 27–57
7 58–120
• The Hamming code can detect and correct only a single error. By adding another parity bit to the coded
word, the Hamming code can be used to correct a single error and detect double errors.
• If this additional parity bit is included, then the previous 12‐bit coded word becomes
001110010100P13. This produces the 13‐bit word 0011100101001 (even parity).
• P = 0, the parity is correct (even parity)
• P = 1, then the parity is incorrect (odd parity)
• If C ≠ 0 and P = 0, a double error occurred that is detected, but that cannot be corrected.
• This scheme may detect more than two errors, but is not guaranteed to detect all such errors.
• Integrated circuits use a modified Hamming code to generate and check parity bits for single‐error
correction and double‐error detection.
Drawbacks
• Both simple parity and 2D parity do not catch all the errors.
• Simple parity only catches odd numbers of bit errors, and can not catch if 2
bits interchanged.
• 2D parity is better at catching errors, but requires too many check bits added
to a block of data.
• Can not use in 4 bit errors and more bit errors in some cases.
WHAT IS A BINARY CODE ?
• The reflected binary code(RBC), also known as Gray code after Frank
Gray, is a binary numeral system where two successive values differ in only
one bit. This code was originally designed to prevent spurious output from
electromechanical switches.
THE GRAY CODE
Binary-to-Gray code conversion
• The MSB in the Gray code is the same as corresponding MSB in the binary
number.
• Going from left to right, add each adjacent pair of binary code bits to get
the next Gray code bit. Discard carries.
CONVERTING CIRCUIT