Coding and Error Checking of Binary Data
Concepts of Sign-Magnitude, One's Complement, Two's Complement, and Excess-n Notation, along with Parity Check and Hamming Code Error Detection Methods for Binary Data
Coding Methods
There are four ways to represent binary data in computers: sign-magnitude, one's complement, two's complement, and excess-n notation. These methods are used to represent and process signed numbers in computers, each solving the problems encountered when using binary to represent signed numbers.
-
Sign-Magnitude:
Sign-magnitude was the earliest method used for representation. The highest bit represents the sign, while the remaining bits represent the magnitude. However, sign-magnitude has a problem: there are two representations for zero, positive zero and negative zero. Moreover, special processing is required to compare the results of addition and subtraction, which adds complexity to operations and comparisons.
The main advantage of sign-magnitude is its simplicity, but the problem of representing zero makes it inconvenient for practical applications.
-
One's Complement:
To address the issue of the sign bit in sign-magnitude representation, one's complement was introduced. In one's complement, the representation of negative numbers is obtained by taking the complement of each bit of the corresponding positive number. Therefore, addition and subtraction operations for two one's complement numbers can be unified as addition operations, with the sign bit participating in the operation, and the sign bit of the result being determined by the operation.
One's complement solves the problem of the sign bit in sign-magnitude representation. However, when performing addition and subtraction operations, the issue of positive zero and negative zero still needs to be considered.
-
Two's Complement:
To address the issue of positive and negative zero in one's complement representation, two's complement was introduced. In two's complement, all bits are complemented based on one's complement representation, and then is added.
Since there is only one representation for zero in two's complement, the problem of positive and negative zero is avoided during operations.
-
Excess-N Notation (Bias): Excess-N notation is commonly used to represent the exponent part of floating-point numbers. Excess-N notation is introduced to enable direct comparison of floating-point numbers with integers without the need for additional operations.
Excess-N notation sets a bias value to represent unsigned integers as signed integers, enabling direct comparison with integers and simplifying the comparison process.
These different representation methods exist to more effectively represent and process signed numbers within a limited number of bits, while keeping operations and comparisons simple and efficient. The choice of representation method depends on specific application requirements and hardware design considerations.
Below, we will introduce their specific representation methods and calculation methods, assuming the binary number is , and the machine's word length is .
Sign-Magnitude Representation
- If is a pure integer
- When it is positive, the highest bit is , and the remaining bits are .
- When it is negative, the highest bit is , and the remaining bits are .
- If is a pure fraction
- When it is positive, the highest bit is , and the remaining bits are .
- When it is negative, the highest bit is , and the remaining bits are .
- If is , there are two representations: and .
- Addition rule: First, determine the sign bit. If the signs are the same, add the absolute values, and the sign of the result remains unchanged; if the signs are different, perform subtraction, and the sign of the result is the same as the sign of the absolute value of the larger number.
- Subtraction rule: Subtracting two numbers represented by sign-magnitude, first negate the sign of the subtrahend, then perform sign-magnitude addition on the minuend and the negated subtrahend. (Subtract numbers with the same sign; add numbers with different signs)
One's Complement Representation
- If is a pure integer
- When it is positive, the highest bit is , and the remaining bits are .
- When it is negative, take the complement of each bit of .
- If is a pure fraction
- When it is positive, the highest bit is , and the remaining bits are .
- When it is negative, take the complement of each bit of .
- If is , there are two representations: and .
- Addition and subtraction: . The sign bit is considered as part of the number, and any carry generated by the sign bit is discarded.
Two's Complement Representation
- If is a pure integer
- When it is positive, the highest bit is , and the remaining bits are .
- When it is negative, the highest bit is , and the remaining bits are .
- If is a pure fraction
- When it is positive, the highest bit is , and the remaining bits are .
- When it is negative, the highest bit is , and the remaining bits are .
- If is , there is only one representation, for example, when , it is .
- Addition and subtraction: . The sign bit is considered as part of the number, and any carry generated by the sign bit is discarded.
Excess-N Notation (Bias)
A bias value of is set.
- If is a pure integer, it is .
- If is a pure fraction, it is .
- If is , there is only one representation, for example, when , it is .
- In fact, when the bias value is , only the complement of the highest bit of the two's complement needs to be taken, to obtain the corresponding excess-n notation representation. In other words, the highest bits of the two's complement and excess-n notation are each other's complements.
- Addition and subtraction operations are the same as two's complement.
Fixed-Point Numbers
For fixed-point numbers, the decimal point is after the lowest bit. For fixed-point fractions, if there is a
sign bit, the decimal point is after the sign bit; if there is no sign bit, the decimal point is before the highest bit.
Among the above representation methods, for fixed-point integers,
- The representation range of sign-magnitude and two's complement is .
- The representation range of two's complement and excess-n notation is .
- They can both represent numbers.
For fixed-point fractions,
- The representation range of sign-magnitude and two's complement is .
- The representation range of two's complement and excess-n notation is .
- They can both represent numbers.
Floating-Point Numbers
Floating-point numbers are represented by exponent and mantissa , where the exponent is represented by bits of excess-n notation, and the mantissa is represented by bits of two's complement. The binary number can be expressed.
The range that can be expressed is .
Error Checking Codes
Parity Check
Parity check refers to adding to the check bit so that the number of s in the binary number is odd. The same applies to even parity. However, odd parity can only detect coding errors in odd-numbered bits, and cannot detect coding errors in even-numbered bits. Even parity is the same.
Hamming Code
Hamming codes insert check bits into bits of binary data. And satisfy .
Taking the checked data as an example, first determine the position of the check bits, and use the above formula to determine that . Calculate the positions of the check bits using . These are the subscripts of the Hamming code check bits. Write down the binary representations of these subscripts.
Then use to replace in the above table to use as a wildcard.
Write down the binary sequence from to , which is from to .
Then use the wildcard table to match the numbers.
Then, the Hamming code table to be determined is obtained.
Therefore, we can determine that
- is responsible for the parity check of bits .
- is responsible for the parity check of bits .
- is responsible for the parity check of bits .
Now, let's determine the values of . If it is even parity:
- , the number of s is odd, so .
- , the number of s is even, so .
- , the number of s is even, so .
If it is odd parity, simply take the complement of the above check bits.
Finally, the complete Hamming code is obtained.
To check for errors, simply perform XOR operations on the check bits and the bits to be checked:
If even parity is used, should be , and for odd parity, it should be . If it is not all or , an error has occurred, and the data bit corresponding to the error should be flipped.