All notes

Number representation and arithmetic

GATE CS70 min read

Free preview: 29% of the note

Positional number systems

Definition. In base rr, a digit string dndn−1…d0.d−1…d_nd_{n-1}\ldots d_0.d_{-1}\ldots represents ∑idiri\sum_i d_i r^i, where each digit satisfies 0≤di<r0\le d_i<r. Binary, octal, decimal, and hexadecimal use bases 2, 8, 10, and 16. Hexadecimal digits A through F denote values 10 through 15.

Example. 1101.012=8+4+1+0.25=13.25101101.01_2=8+4+1+0.25=13.25_{10}.

Exam point. A digit cannot be used if it is at least the base.

Base conversion by positional expansion

Definition. Convert a base-rr integer to decimal by summing digit-weight products; fractional digits have negative powers. For an integer in decimal, repeated division by the target base yields remainders read from last to first. For a fraction, repeated multiplication gives digits in order after the radix point.

Example. 2F16=2⋅16+15=47102F_{16}=2\cdot16+15=47_{10}.

Exam point. Read integer remainders upward and fractional digits downward.

Solved calculation. Convert 45.6251045.625_{10} to binary.

  1. Integer part: 45/245/2 gives remainders 1,0,1,1,0,1 when read upward, so 4510=101101245_{10}=101101_2.
  2. Fraction: 0.625×2=1.250.625\times2=1.25, giving first bit 1 and remainder 0.25.
  3. 0.25×2=0.50.25\times2=0.5, giving second bit 0 and remainder 0.5.
  4. 0.5×2=1.00.5\times2=1.0, giving third bit 1 and remainder 0. Answer. 45.62510=101101.101245.625_{10}=101101.101_2.

Binary, octal, and hexadecimal conversion

Definition. Octal digits correspond to groups of three binary bits; hexadecimal digits correspond to groups of four. Group from the radix point outward, padding with zeros at the far left or right as needed. This conversion is exact because 8=238=2^3 and 16=2416=2^4.

Example. 111010012=1110 10012=E91611101001_2=1110\ 1001_2=E9_{16}.

Exam point. Pad at the ends, not between bits or across the radix point.

Solved calculation. Convert 10110111.011210110111.011_2 to octal and hexadecimal.

  1. Octal grouping: 010 110 111.011010\ 110\ 111.011, giving 267.38267.3_8.
  2. Hex grouping: 1011 0111.01101011\ 0111.0110, giving B7.616B7.6_{16}.
  3. Check decimal: integer 183183, fraction 3/83/8; both forms agree. Answer. 267.38=B7.616267.3_8=B7.6_{16}.

Unsigned fixed-width integers

Definition. An unsigned nn-bit word represents integers from 0 through 2n−12^n-1. Leading zeros do not change its value. Addition that produces a carry beyond bit n−1n-1 wraps modulo 2n2^n if the hardware discards that carry, while the carry flag may preserve information about unsigned overflow.

Example. In 4 bits, 1111 represents 15.

Exam point. Distinguish the stored low bits from the mathematical sum and carry-out.

Solved calculation. Add unsigned 4-bit values 1101 and 0111.

  1. Decimal values are 13 and 7; mathematical sum is 20.
  2. Binary addition is 1101+0111=101001101+0111=10100.
  3. The low four bits are 0100, and the carry-out is 1.
  4. As a 4-bit stored result the value is 4, with unsigned overflow indicated by carry. Answer. Stored result 0100, carry-out 1, mathematical sum 20.

Sign-magnitude representation

Definition. In an nn-bit sign-magnitude word, the most significant bit is the sign (0 positive, 1 negative) and the remaining n−1n-1 bits give magnitude. The range is −(2n−1−1)-(2^{n-1}-1) through +(2n−1−1)+(2^{n-1}-1) and there are two encodings of zero. Arithmetic needs sign and magnitude handling rather than ordinary unsigned addition.

Example. In 8 bits, +5 is 00000101 and −5 is 10000101.

Exam point. Sign-magnitude has both +0 and −0.

One's-complement representation

Definition. In one's complement, a negative number is formed by complementing every bit of the positive magnitude representation. For nn bits, the range is −(2n−1−1)-(2^{n-1}-1) through +(2n−1−1)+(2^{n-1}-1) and zero has two encodings. Addition may require an end-around carry.

Example. In 8 bits, +5 is 00000101 and −5 is 11111010.

Exam point. Negative values are not formed by complementing only the magnitude field.

Two's-complement representation

Definition. In nn bits, two's-complement values range from −2n−1-2^{n-1} through 2n−1−12^{n-1}-1. A negative value is obtained by inverting all bits of the positive magnitude and adding 1, equivalently interpreting the top bit with weight −2n−1-2^{n-1}. There is one zero encoding.

Example. In 8 bits, −5 is 1111101111111011.

Exam point. The most negative value has no positive counterpart in the same width.

Solved calculation. Interpret 8-bit word 11101010 as signed two's complement.

  1. The sign bit is 1, so the value is negative.
  2. Invert bits: 00010101.
  3. Add 1: 00010110, which is 22.
  4. Therefore the original value is −22.
  5. Check the weighted sum: −128+64+32+8+2+0?=−22-128+64+32+8+2+0?= -22 using bits 11101010: −128+64+32+8+2=−22-128+64+32+8+2=-22. Answer. −22.

Two's-complement range and overflow

Definition. An nn-bit two's-complement number has range [−2n−1,2n−1−1][-2^{n-1},2^{n-1}-1]. Signed overflow occurs when the exact mathematical result lies outside this interval. In addition, overflow occurs when equal-sign operands produce a result with the opposite sign; the carry into the sign bit differs from the carry out.

Example. In 4 bits, 0111+00010111+0001 gives 1000, representing −8 although the sum is +8; overflow occurs.

Exam point. Carry-out alone does not determine signed overflow.

Solved calculation. Add 4-bit two's-complement values 0101 and 1100.

  1. Interpret operands: 0101 is +5; 1100 is −4.
  2. Add: 0101+1100=100010101+1100=10001.
  3. Keep four bits: 0001; carry-out is 1.
  4. Exact sum is +1, in range, and signs differ, so signed overflow is absent. Answer. Result 0001 (+1), no signed overflow.

Signed extension and truncation

Definition. To widen a two's-complement word, replicate its sign bit into new high positions; this preserves value. Zero extension is appropriate for unsigned values. Truncating high bits can change the value and is safe for signed arithmetic only when the removed bits are redundant sign copies and the retained sign remains consistent.

Example. Sign-extend 8-bit 11110110 (−10) to 12 bits: 111111110110.

Exam point. Never zero-extend a negative two's-complement value.

Solved calculation. Sign-extend 5-bit 10110 to 8 bits and interpret it.

  1. The original sign bit is 1; add three leading 1s: 11110110.
  2. In 5 bits, 101102=−16+4+2=−1010110_2=-16+4+2=-10.
  3. In 8 bits, 111101102=−128+64+32+16+4+2=−1011110110_2=-128+64+32+16+4+2=-10. Answer. 11110110, still −10.

Binary addition and subtraction

Definition. Binary addition uses the usual bit sums with carry. In fixed-width two's-complement arithmetic, subtraction is A−B=A+B′+1A-B=A+B'+1; discard carry-out for the stored result, then check signed overflow separately. The same adder can perform both operations using conditional XOR inversion of BB.

Example. 0101−0011=0101+1100+1=00110101-0011=0101+1100+1=0011 in four bits.

Exam point. Carry-out and signed overflow answer different questions.

Solved calculation. Compute 7−5 using four-bit two's complement.

  1. Encode +7 as 0111 and +5 as 0101.
  2. Complement 0101 to 1010 and add 1, obtaining 1011 (−5).
  3. Add 0111+1011=100100111+1011=10010.
  4. Keep four bits: 0010; exact result +2 is in range. Answer. 0010, which is +2.

Fixed-point representation

Definition. A fixed-point format assigns a fixed number of fractional positions. If a signed integer code II is stored with ff fractional bits, its represented value is I/2fI/2^f under the chosen signed encoding. The representable spacing is 2−f2^{-f}; range depends on total width and sign convention.

Example. With four fractional bits, code integer 24 represents 24/16=1.524/16=1.5.

Exam point. The binary point is implied; it does not consume a storage bit.

Solved calculation. Interpret signed 8-bit two's-complement fixed point with 3 fractional bits, word 11110100.

  1. As an integer code, 11110100 represents −12.
  2. Divide by 23=82^3=8.
  3. The represented real number is −12/8=−1.5-12/8=-1.5. Answer. −1.5.

Fixed-point rounding and overflow

Definition. Quantization maps a real value to the nearest representable fixed-point value or follows a specified rounding rule. Truncation discards low fractional bits; round-to-nearest may reduce maximum error but needs a tie convention. Overflow occurs when the rounded value exceeds the format's finite range.

Example. With two fractional bits, 1.13 rounded to nearest quarter is 1.25.

Exam point. State whether truncation or rounding is used and how exact ties are handled.

Solved calculation. Quantize 2.6875 to a format with two fractional bits, using round-to-nearest, ties-to-even.

  1. The grid spacing is 2−2=0.252^{-2}=0.25.
  2. Neighboring values are 2.50 and 2.75; distances are 0.1875 and 0.0625.
  3. Choose 2.75.
  4. Its scaled integer is 11, so the stored fractional code is 10.11210.11_2. Answer. 2.75, encoded as 10.11 with two fractional bits.

Floating-point fields and value

Definition. A normalized binary floating-point number stores a sign bit, a biased exponent, and a fraction field. For a normal IEEE-style binary value, the significand is 1.f1.f in base 2 and the value is (−1)s(1.f)22e−bias(-1)^s(1.f)_2 2^{e-bias}. The hidden leading 1 is not stored in the fraction field.

You have read the free 29% of this note

Sign in with Google to keep reading. It is free, takes one tap, and you also get:

  • 6 practice questions from this note
  • Every note in full, plus the PDF library
  • Official past papers as timed tests, with explanations
  • An AI tutor for doubts, in Hindi or English
  • Your progress, mistakes and study plan saved on every device
Continue reading, free