Module 2 · 18 min read · Free chapter
Number representation and computer arithmetic
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.
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 has the value , 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 and , 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 , and hexadecimal .
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 . Multiplying 0.625 by 2 gives 1.25, then and , so the fraction digits are 1, 0, 1 and .
Binary, octal and hexadecimal are linked very simply, because and . 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 in hexadecimal and 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 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 . 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 and . 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 . 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 to 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 . Another way to see it is that the leftmost bit has the weight instead of , so . There is only one zero, the range is to , 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 , then flipping gives 11010010, and adding 1 gives 11010011. Check it with the weights: .
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 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 , 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 be the carry into the sign bit and the carry out of it; then the overflow flag is . In the example the carry into the sign bit is 1 and the carry out is 0, so .
Worked example. Add −100 and −64 in 8 bits. Here and , and the full sum is . Dropping the ninth bit leaves , 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 is fine as unsigned, since 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 means 3.75, the smallest step is , and the largest unsigned value is .
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 and 0.0001 as .
In binary, any non-zero number can be written as , 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 in single precision. Since and are kept for special values, normal numbers use to , 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 , 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 , so and . Then , 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 , and the true exponent is . The value is .
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 to 254 | any value | Normalised |
| 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 , which is about . 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 , about .
At the other end, the largest single-precision number has and all fraction bits 1, giving , about . 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 together with an extra bit , 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 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 , that is , 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 .
Worked example. Multiply by with 4-bit registers. The trace below shows A, Q and 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 ; if A turns negative, the step restores and sets , otherwise it keeps the new A and sets .
Worked example. Divide 7 () by 2 (), 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 and , and indeed .
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:
- Explain and compare sign-magnitude, 1's complement and 2's complement, with their ranges and the problem of two zeros.
- Convert a decimal number to IEEE 754 single or double precision and back, showing every step.
- Trace Booth's multiplication or restoring division for given operands in a register table.
- 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).
- 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)
- Convert 0.15625 and −7.5 into IEEE 754 single-precision hexadecimal, and explain the role of the bias. (10 marks)
- Explain Booth's algorithm and use it to multiply −6 by 7 with 5-bit registers, listing A, Q and after each step. (15 marks)
Answer plan for question 2.
- Write , so , , and F is 01 followed by 21 zeros.
- Group the bits as 0011 1110 0010 0000 0000 0000 0000 0000, which gives 3E200000.
- Write , so for −7.5 the sign is , , 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 and free for special values.
- Finish by converting one pattern back to decimal as a check.
Quick revision
- A base-r number has the value , and its digits run from to .
- Integers convert by repeated division and fractions by repeated multiplication, and one hexadecimal digit equals 4 bits.
- n unsigned bits cover 0 to .
- Sign-magnitude and 1's complement cover to and have two zeros.
- 2's complement covers to , has one zero, and is formed by inverting and adding 1.
- Signed overflow means same-sign inputs gave an opposite-sign result, and at the sign bit.
- IEEE 754 single precision is bits with bias 127, and double is bits with bias 1023.
- 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.