Computer Science and IT‹›⌂ 0:00 studied

Module 2 · 18 min read · Free chapter

Number representation and computer arithmetic

In short

In n-bit 2's complement the range is −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1, and signed overflow happens when two same-sign numbers give an opposite-sign result. IEEE 754 single precision has 1 sign, 8 exponent and 23 fraction bits with bias 127; double has 1, 11 and 52 bits with bias 1023.

What are you preparing for? We show the practice and previous year questions of your exam.

Start from zero: the meter that rolls over

Look at the electricity meter outside your house. It has a few wheels, and each wheel shows a digit from 0 to 9. When the rightmost wheel passes 9, it goes back to 0 and pushes the next wheel up by one. Every number system works exactly like this, and a computer does the same thing with wheels that have only two positions.

Inside a computer each wheel is a bit that is either 0 or 1. A meter with five wheels can show 0 to 99,999, and after that it rolls over to 00000. An 8-bit register behaves the same way: it holds only 256 different patterns, and a sum that needs more bits simply wraps around.

This chapter answers three questions. How do we write any number, whether positive, negative or fractional, using only fixed bit patterns? How do we notice when an answer no longer fits, and how does the hardware multiply and divide using only adds, subtracts and shifts?

The gates that actually add the bits are taught under Boolean algebra and K-maps. Here we care about what the bit patterns mean.

Why it matters. A wrong reading of a bit pattern is not a small error. The same 8 bits 11111111 mean 255 to one instruction and −1 to another, and a program that mixes them up gives wrong balances or crashes. GATE, ISRO and RRB papers test exactly this skill of reading a pattern correctly.

Positional number systems and conversions

In the decimal number 507.25 the digit 5 means five hundreds only because of where it stands. In general, a number in base r with digits did_i has the value ∑di×ri\sum d_i \times r^i, where the position i is 0 just left of the point, rises to the left, and becomes −1, −2 and so on to the right. Each digit must lie between 00 and r−1r - 1, so binary uses 0 and 1, octal uses 0 to 7, and hexadecimal uses 0 to 9 and then A to F for ten to fifteen.

Going from any base to decimal is just this sum. For example, binary 1011.012=8+0+2+1+0+14=11.251011.01_2 = 8 + 0 + 2 + 1 + 0 + \dfrac{1}{4} = 11.25, and hexadecimal 2F16=2×16+15=47\mathrm{2F}_{16} = 2 \times 16 + 15 = 47.

Going from decimal to base r needs two separate rules, one for the integer part and one for the fraction. For the integer part, divide by r again and again and read the remainders from the last one to the first. For the fraction, multiply by r again and again and read the integer parts in the order they appear.

Worked example. Convert 25.625 to binary. Dividing 25 by 2 again and again gives the remainders 1, 0, 0, 1, 1, so reading upwards 25=11001225 = 11001_2. Multiplying 0.625 by 2 gives 1.25, then 0.25×2=0.50.25 \times 2 = 0.5 and 0.5×2=1.00.5 \times 2 = 1.0, so the fraction digits are 1, 0, 1 and 25.625=11001.101225.625 = 11001.101_2.

Binary, octal and hexadecimal are linked very simply, because 8=238 = 2^3 and 16=2416 = 2^4. Group the bits in threes from the point for octal, or in fours for hexadecimal, padding with zeros at the ends. So 11001.101 becomes 0001 1001.10102=19.A160001\,1001.1010_2 = \mathrm{19.A}_{16} in hexadecimal and 011 001.1012=31.58011\,001.101_2 = 31.5_8 in octal.

Not every decimal fraction has an exact binary form. Try 0.1: repeated doubling gives the digits 0, 0, 0, 1, 1, 0, 0, 1, 1 and the block 0011 repeats forever. That is why 0.1+0.20.1 + 0.2 is not exactly 0.3 inside most computers.

Why it matters. Hexadecimal is the everyday shorthand for memory addresses, colour codes and machine code, so every exam expects quick conversions. The recurring 0.1 also explains errors in money calculations, which is why banking software stores amounts in paise as whole numbers.

Signed integers: sign-magnitude, 1's and 2's complement

So far every pattern has been a positive number, and n unsigned bits cover 0 to 2n−12^n - 1. To store negative numbers we must give some patterns a new meaning, and there are three common ways to do it. Let us use 4 bits first, because all 16 patterns fit in one table.

Sign-magnitude. The leftmost bit is the sign, 0 for plus and 1 for minus, and the remaining bits give the size. So +5=0101+5 = 0101 and −5=1101-5 = 1101. It is easy to read, but 0000 and 1000 both mean zero, and the adder must compare signs before it adds.

1's complement. A negative number is made by flipping every bit of the positive one, so −5=1010-5 = 1010. It still has two zeros, 0000 and 1111, and an end-around carry must be added back after a sum. Both of these forms cover −(2n−1−1)-(2^{n-1} - 1) to +(2n−1−1)+(2^{n-1} - 1) for n bits, which is −7 to +7 in 4 bits and −127 to +127 in 8 bits.

2's complement. Flip every bit and then add 1, so −5=1010+1=1011-5 = 1010 + 1 = 1011. Another way to see it is that the leftmost bit has the weight −2n−1-2^{n-1} instead of +2n−1+2^{n-1}, so 1011=−8+2+1=−51011 = -8 + 2 + 1 = -5. There is only one zero, the range is −2n−1-2^{n-1} to 2n−1−12^{n-1} - 1, and a plain binary adder works for both signs.

Pattern Unsigned Sign-magnitude 1's complement 2's complement
0000 0 +0 +0 0
0111 7 +7 +7 +7
1000 8 −0 −7 −8
1011 11 −3 −4 −5
1111 15 −7 −0 −1
4-bit range 0 to 15 −7 to +7 −7 to +7 −8 to +7
8-bit range 0 to 255 −127 to +127 −127 to +127 −128 to +127

Worked example. Write −45 in 8-bit 2's complement. First +45=00101101+45 = 00101101, then flipping gives 11010010, and adding 1 gives 11010011. Check it with the weights: −128+64+16+2+1=−45-128 + 64 + 16 + 2 + 1 = -45.

To widen a 2's complement number, say from 8 to 16 bits, copy the sign bit into every new position, so −45 becomes 11111111 11010011. This is called sign extension, and filling with zeros instead would turn −45 into +211.

Why it matters. Nearly every processor stores signed integers in 2's complement, because one adder serves both signs and there is a single zero. GATE questions on ranges, on the most negative number and on sign extension all come from the weight −2n−1-2^{n-1} of the leftmost bit.

Overflow and how hardware detects it

An 8-bit 2's complement register holds −128 to +127, so what happens if we add 100 and 64? The true answer 164 does not fit, and the adder produces 01100100+01000000=1010010001100100 + 01000000 = 10100100, which reads as −92. This wrong result is called overflow, and the hardware must flag it.

The rule is easy to remember. Overflow can happen only when both numbers have the same sign, and it has happened exactly when the result has the opposite sign. Adding a positive and a negative number can never overflow, because the answer lies between the two.

Hardware uses an equivalent test on the carries. Let CinC_{\mathit{in}} be the carry into the sign bit and CoutC_{\mathit{out}} the carry out of it; then the overflow flag is V=Cin⊕CoutV = C_{\mathit{in}} \oplus C_{\mathit{out}}. In the example the carry into the sign bit is 1 and the carry out is 0, so V=1V = 1.

Worked example. Add −100 and −64 in 8 bits. Here −100=10011100-100 = 10011100 and −64=11000000-64 = 11000000, and the full sum is 1 010111001\,01011100. Dropping the ninth bit leaves 01011100=+9201011100 = +92, a positive answer from two negatives, so it is overflow; the carry into the sign bit is 0 and the carry out is 1, which agrees.

For unsigned numbers the test is different. A carry out of the leftmost bit means the result is too big, so the carry flag is the unsigned overflow signal while V is the signed one. The same addition 01100100+0100000001100100 + 01000000 is fine as unsigned, since 100+64=164100 + 64 = 164 is below 256, but it is an overflow as signed.

Why it matters. Overflow that nobody checks has caused real failures, from wrong totals to crashed control software. Processors keep separate carry and overflow flags so that the same add instruction serves both unsigned and signed programs, and exams ask which flag is set.

Fixed point and the IEEE 754 format

Integers are not enough for readings such as 3.75 volts or 0.0001 seconds. One simple answer is fixed point: we decide in advance that, say, the last 4 of 8 bits come after the binary point. Then 0011.110020011.1100_2 means 3.75, the smallest step is 2−4=0.06252^{-4} = 0.0625, and the largest unsigned value is 1111.11112=15.93751111.1111_2 = 15.9375.

Fixed point is fast and is used in cheap controllers and signal processors, but it wastes bits. Floating point solves this the way scientific notation does, writing 50,000 as 5×1045 \times 10^4 and 0.0001 as 1×10−41 \times 10^{-4}.

In binary, any non-zero number can be written as ±1.F×2e\pm 1.F \times 2^e, where F is the fraction after the binary point and e is the exponent. Writing it with exactly one 1 before the point is called normalising the number. Because that leading 1 is always there, IEEE 754 does not store it, which gives one free bit of precision called the hidden bit.

Field Single precision, 32 bits Double precision, 64 bits
Sign S 1 bit 1 bit
Exponent E 8 bits 11 bits
Fraction F 23 bits 52 bits
Bias 127 1023
Precision with the hidden bit 24 bits, about 7 decimal digits 53 bits, about 15 to 16 decimal digits

The exponent field is stored with a bias instead of in 2's complement. The stored value E equals the true exponent plus 127 in single precision, or plus 1023 in double, so a true exponent of 3 is stored as 130 in single precision. Biased exponents are never negative, which lets the hardware compare two positive floats as if they were plain integers.

For a normalised number the value is therefore (−1)S×1.F×2E−127(-1)^S \times 1.F \times 2^{E-127} in single precision. Since E=0E = 0 and E=255E = 255 are kept for special values, normal numbers use E=1E = 1 to 254254, that is true exponents −126 to +127.

Why it matters. Every scientific program, graphics card and spreadsheet uses these two formats, so the same bit pattern means the same number on every machine. GATE often asks for the hexadecimal pattern of a decimal number or the decimal value of a pattern, and the bias is the step most students forget.

Converting to and from IEEE 754, and the special values

Decimal to IEEE 754 single precision takes five steps. Write the number in binary, normalise it to 1.F×2e1.F \times 2^e, and add 127 to e to get E. Then fill S, E and F, padding F with zeros to 23 bits, and finally group the 32 bits in fours to read the hexadecimal pattern.

Worked example. Convert −13.25 to single precision. First 13.25=1101.012=1.101012×2313.25 = 1101.01_2 = 1.10101_2 \times 2^3, so S=1S = 1 and e=3e = 3. Then E=3+127=130=100000102E = 3 + 127 = 130 = 10000010_2, and F is 10101 followed by eighteen zeros.

S  E         F
1  10000010  10101000000000000000000
1100 0001 0101 0100 0000 0000 0000 0000  =  C1540000 (hex)

So −13.25 is stored as C1540000 in hexadecimal.

IEEE 754 back to decimal runs the same steps in reverse. Take the pattern 40A00000: in bits it is 0 10000001 01000000000000000000000, so S=0S = 0, E=129E = 129 and the true exponent is 129−127=2129 - 127 = 2. The value is 1.012×22=1012=5.01.01_2 \times 2^2 = 101_2 = 5.0.

Now come the special patterns. When E is all 0s or all 1s, the usual formula is switched off and the pattern has a fixed meaning, as the table shows.

Exponent field E Fraction F Meaning
0 0 +0 or −0, decided by the sign bit
0 not 0 Denormalised (−1)S×0.F×2−126(-1)^S \times 0.F \times 2^{-126}
1 to 254 any value Normalised (−1)S×1.F×2E−127(-1)^S \times 1.F \times 2^{E-127}
255, all 1s 0 +infinity or −infinity
255, all 1s not 0 NaN, not a number, as from zero divided by zero

Denormalised numbers fill the gap between 0 and the smallest normal number 2−1262^{-126}, which is about 1.18×10−381.18 \times 10^{-38}. They drop the hidden 1 and fix the exponent at −126, so the values thin out gradually towards zero instead of jumping straight to 0. The smallest positive denormal is 2−23×2−126=2−1492^{-23} \times 2^{-126} = 2^{-149}, about 1.4×10−451.4 \times 10^{-45}.

At the other end, the largest single-precision number has E=254E = 254 and all fraction bits 1, giving (2−2−23)×2127(2 - 2^{-23}) \times 2^{127}, about 3.4×10383.4 \times 10^{38}. A result beyond that becomes infinity, and dividing a non-zero number by 0 also gives infinity, while 0/0 gives NaN.

Why it matters. Special values let a long calculation carry on and report a problem at the end instead of crashing at the first bad step.

Booth's multiplication and restoring division

Multiplying by hand in binary is easy: for every 1 in the multiplier, add a shifted copy of the multiplicand. Hardware does this with an accumulator A, a multiplier register Q and the multiplicand M, adding and shifting once per bit. But this simple method handles negative numbers awkwardly, and a multiplier such as 01111110 needs many additions.

Booth's algorithm fixes both problems. It looks at the multiplier bit Q0Q_0 together with an extra bit Q−1Q_{-1}, which starts at 0, and acts on the pair: 10 means subtract M from A, 01 means add M to A, and 00 or 11 means do nothing. After the action, A, Q and Q−1Q_{-1} are shifted right together by one place while the sign bit of A is copied, which is an arithmetic shift, and this repeats n times for an n-bit multiplier.

The idea behind it is Booth recoding. A run of 1s such as 0111 equals 1000−00011000 - 0001, that is 8−18 - 1, so instead of three additions we need one subtraction at the start of the run and one addition after its end. Written as signed digits, 0111 recodes to +1 0 0 −1+1\ 0\ 0\ {-1}.

Worked example. Multiply M=5=01012M = 5 = 0101_2 by Q=−3=11012Q = -3 = 1101_2 with 4-bit registers. The trace below shows A, Q and Q−1Q_{-1} after each action and shift.

Step  Q0,Q-1  Action        A     Q     Q-1
 0    -       start         0000  1101  0
 1    1,0     A = A - M     1011
              shift right   1101  1110  1
 2    0,1     A = A + M     0010
              shift right   0001  0111  0
 3    1,0     A = A - M     1100
              shift right   1110  0011  1
 4    1,1     no action
              shift right   1111  0001  1

The answer sits in A and Q together as 11110001, which is −15 in 8-bit 2's complement, as expected.

Restoring division works like school long division in binary. The divisor is in M, the dividend in Q, and A starts at 0. Each of the n steps shifts A and Q left by one bit and then tries A←A−MA \leftarrow A - M; if A turns negative, the step restores A←A+MA \leftarrow A + M and sets Q0=0Q_0 = 0, otherwise it keeps the new A and sets Q0=1Q_0 = 1.

Worked example. Divide 7 (Q=0111Q = 0111) by 2 (M=00010M = 00010), using a 5-bit A. In steps 1 and 2 the trial subtraction goes negative, so A is restored and the quotient bits are 0. In steps 3 and 4 it succeeds, so at the end Q=0011=3Q = 0011 = 3 and A=00001=1A = 00001 = 1, and indeed 7=2×3+17 = 2 \times 3 + 1.

Why it matters. Booth's method is the starting point of the fast multipliers inside processors, and its recoding cuts the work on runs of 1s, although an alternating multiplier such as 0101 is its worst case. GATE asks you to recode a multiplier or count its additions and subtractions, and other papers ask for the steps of restoring division.

Descriptive lens: how written papers ask this

Written papers for IT officer posts, state Assistant Programmer exams and university-level tests ask about this topic in four ways:

  1. Explain and compare sign-magnitude, 1's complement and 2's complement, with their ranges and the problem of two zeros.
  2. Convert a decimal number to IEEE 754 single or double precision and back, showing every step.
  3. Trace Booth's multiplication or restoring division for given operands in a register table.
  4. Justify a design choice, for example why floating point uses a biased exponent and a hidden bit.

Practice questions (our own, not previous-year questions).

  1. Represent −98 in 8-bit sign-magnitude, 1's complement and 2's complement, and state the range of each form for n bits. (10 marks)
  2. Convert 0.15625 and −7.5 into IEEE 754 single-precision hexadecimal, and explain the role of the bias. (10 marks)
  3. Explain Booth's algorithm and use it to multiply −6 by 7 with 5-bit registers, listing A, Q and Q−1Q_{-1} after each step. (15 marks)

Answer plan for question 2.

  • Write 0.15625=0.001012=1.012×2−30.15625 = 0.00101_2 = 1.01_2 \times 2^{-3}, so S=0S = 0, E=−3+127=124=011111002E = -3 + 127 = 124 = 01111100_2, and F is 01 followed by 21 zeros.
  • Group the bits as 0011 1110 0010 0000 0000 0000 0000 0000, which gives 3E200000.
  • Write 7.5=111.12=1.1112×227.5 = 111.1_2 = 1.111_2 \times 2^2, so for −7.5 the sign is S=1S = 1, E=129=100000012E = 129 = 10000001_2, and F is 111 followed by 20 zeros.
  • Group these bits to get C0F00000 for −7.5.
  • Explain that the bias keeps the stored exponent non-negative, so floats compare like integers, and that it leaves E=0E = 0 and E=255E = 255 free for special values.
  • Finish by converting one pattern back to decimal as a check.

Quick revision

  • A base-r number has the value ∑di×ri\sum d_i \times r^i, and its digits run from 00 to r−1r - 1.
  • Integers convert by repeated division and fractions by repeated multiplication, and one hexadecimal digit equals 4 bits.
  • n unsigned bits cover 0 to 2n−12^n - 1.
  • Sign-magnitude and 1's complement cover −(2n−1−1)-(2^{n-1} - 1) to +(2n−1−1)+(2^{n-1} - 1) and have two zeros.
  • 2's complement covers −2n−1-2^{n-1} to 2n−1−12^{n-1} - 1, has one zero, and is formed by inverting and adding 1.
  • Signed overflow means same-sign inputs gave an opposite-sign result, and V=Cin⊕CoutV = C_{\mathit{in}} \oplus C_{\mathit{out}} at the sign bit.
  • IEEE 754 single precision is 1+8+231 + 8 + 23 bits with bias 127, and double is 1+11+521 + 11 + 52 bits with bias 1023.
  • E=0E = 0 gives zero or denormals, E all 1s gives infinity or NaN, and normal single-precision exponents run from −126 to +127.
  • Booth's algorithm subtracts on the pair 10, adds on 01 and then shifts right arithmetically.
  • Restoring division shifts left, subtracts, and restores A whenever it turns negative.

Check yourself

Exam-style questions on this chapter. Pick an answer to see the explanation. 0 of 15 answered

  1. The decimal number 25.625 in binary is:

  2. Binary 11001.101 written in hexadecimal is:

  3. The range of an 8-bit unsigned integer is:

  4. Which representations have two patterns for zero?

  5. The 8-bit 2's complement form of −45 is:

  6. In 8-bit 2's complement, which addition overflows?

  7. The signed overflow flag of an adder is computed as:

  8. An unsigned fixed-point number has 4 integer and 4 fraction bits. Its resolution is:

  9. IEEE 754 single precision has exponent and fraction fields of:

  10. The IEEE 754 single-precision pattern of −13.25 is:

  11. The double-precision exponent bias is:

  12. A single-precision pattern with E=255E = 255 and a non-zero fraction represents:

  13. The smallest positive denormalised single-precision value is:

  14. In Booth's algorithm, the bit pair (Q0,Q−1)=(1,0)(Q_0, Q_{-1}) = (1, 0) causes:

  15. In restoring division, if A becomes negative after A←A−MA \leftarrow A - M, the step:

Previous year questions

Previous year questions for UPSC CSE will appear here once each one is checked word for word against the official question paper.

Learn with AI

A tutor that asks instead of telling, a partner that argues the other side, and this chapter told through your own district.

Tell the tutor what you are stuck on. It will guide you with questions and give the answer after a few tries.

🙋 Raise a doubt or feedback

Stuck somewhere, or spotted a mistake? Write to the ExamHike team. We usually reply within a day, and you will see the reply on My progress.

Log in to ask a doubt or send feedback →

Frequently asked questions

What is the range of an 8-bit 2's complement number?

It is −128 to +127. There is one zero, and the leftmost bit has the weight −128.

How is overflow detected in 2's complement addition?

Overflow occurs when the carry into the sign bit differs from the carry out of it, which happens only when two numbers of the same sign give a result of the opposite sign.

What is −13.25 in IEEE 754 single precision?

−13.25 = −1.10101 × 2³, so S = 1, E = 130 and the fraction is 10101 followed by zeros, giving C1540000 in hexadecimal.

What do E = 0 and E = 255 mean in single precision?

E = 0 gives zero or denormalised numbers, and E = 255 gives infinity when the fraction is 0 and NaN otherwise.

More in this module: Digital logic

Official sources

Facts in this chapter were checked against these official sources.

  1. GATE 2026 Computer Science and Information Technology (CS) syllabus, IIT Guwahati
  2. NPTEL Computer Organization and Architecture (IIT Madras, 106106166): number representation and computer arithmetic