Number Systems and Data Representation
Bases and conversions
You must convert fluently among binary, octal, hexadecimal, and decimal, and perform signed representations: sign-magnitude, one's complement, and two's complement. Two's complement dominates because it gives a single representation of zero and lets addition work without sign logic. For an n-bit two's complement number, the range is -2^(n-1) to 2^(n-1)-1; unsigned range is 0 to 2^n - 1.
Encoding schemes
Know BCD (each decimal digit in 4 bits), ASCII, and the idea of fixed-point versus floating-point (sign, exponent, mantissa). Understand sign extension versus zero extension when widening a value, and truncation when narrowing. Also know that hexadecimal groups four bits and octal groups three.
Traps to watch
- Negating the most negative number: for 8-bit, -(-128) overflows because +128 is not representable.
- Confusing the value of a bit pattern across signed and unsigned interpretations (0xFF is 255 unsigned, -1 signed).
- Forgetting that one's complement has two zeros (+0 and -0).
- Sign-extending an unsigned value, or zero-extending a signed negative value - both corrupt the number.
- Mixing up bit numbering: least significant bit is bit 0.
Rule of thumb
To negate in two's complement: invert all bits and add one. To convert a negative decimal to binary: write the positive magnitude, invert, add one.
Worked example: Represent -5 in 8-bit two's complement. +5 = 0000 0101. Invert: 1111 1010. Add 1: 1111 1011 = 0xFB. Check: 0xFB unsigned is 251, and 251 - 256 = -5. Correct.