Radix Conversions, Complement Arithmetic, Binary Codes & Error Detection
Idea in plain words
Radix r conversions: Successive division by r for integer part; successive multiplication by r for fractional part.
Exam tip
High-yield exam topic (Very High).
Explanation
Digital computation relies on positional number systems where the weight of each digit is a power of base r. In n-bit 2's complement representation, values span from -2^{n-1} to +(2^{n-1} - 1). Gray codes minimize electrical switching noise and eliminate transient decoding spikes in rotary optical shaft encoders. Error-detecting and correcting codes introduce controlled redundancy: a Hamming code with k parity bits protects m data bits provided 2^k ≥ m + k + 1.
Key points
- Signed numbers: Signed-magnitude has dual zero (+0 and -0); 1's complement has end-around carry; 2's complement (1's comp + 1) is standard in microprocessors because subtraction is executed via adder hardware.
- Hardware overflow rule: In 2's complement addition, overflow occurs if and only if two numbers of the same sign yield a result with the opposite sign (V = C_{in} ⊕ C_{out} on the MSB).
- Binary Codes: BCD (8421, invalid codes 1010-1111); Excess-3 (self-complementing BCD + 3); Gray code (unit-distance code where adjacent values change by exactly 1 bit).
- Hamming Code (Error correction): Parity bits placed at powers of 2 (positions 1, 2, 4, 8...). Detects and pinpoints single-bit errors via parity syndrome checks.
Example
Example
Convert Binary 10110 to Gray Code: Retain MSB = 1; Bit 1 = 1 ⊕ 0 = 1; Bit 2 = 0 ⊕ 1 = 1; Bit 3 = 1 ⊕ 1 = 0; Bit 4 = 1 ⊕ 0 = 1. Gray Code = 11101.
Code examples
// C++ Bitwise Verification of 2's Complement Addition & Overflow Detection
#include <iostream>
using namespace std;
bool addWithOverflow(int8_t a, int8_t b, int8_t &result) {
int16_t sum = (int16_t)a + (int16_t)b;
result = (int8_t)sum;
// Overflow occurs when both operands have same sign, but result has opposite sign
bool overflow = ((a > 0 && b > 0 && result < 0) || (a < 0 && b < 0 && result > 0));
return overflow;
}
int main() {
int8_t x = 100, y = 50, res;
if (addWithOverflow(x, y, res)) {
cout << "Arithmetic Overflow Detected! Result wrapped to: " << (int)res << endl;
}
return 0;
}
Exam questions
- [8 Marks] Perform subtraction using 2's complement arithmetic: (i) (42)₁₀ - (68)₁₀, (ii) (-35)₁₀ - (-18)₁₀ using 8-bit registers. State how overflow is detected.
- [7 Marks] Encode data bits 1011 using 7-bit even parity Hamming Code. Show how a single-bit error in the 5th bit position is detected and corrected.
- [7 Marks] Convert (523.6875)₁₀ to Binary, Octal, and Hexadecimal representations.