Mathematics for Computer Science 201-N11 • CEGEP, Montreal

Revision sheet: number systems in computing (201-N11)

Number systems are the most mechanical chapter of the course, and yet the one where tests lose the most marks: the algorithms are simple, but each one has a reading DIRECTION, and reversing it gives a clean, wrong result.

This sheet gathers those reading directions and the edge cases: remainders from bottom to top, groups from the right, the 11 added after the inversion, and the one sign that reveals an overflow. It is written for CEGEP students in computer science programs taking 201-N11; once it is read, the corrected exercise set on the same chapter puts every reflex to the test.

Mark this sheet as read or add it to your favourites: a free account, no password, keeps your read sheets and favourites from one visit to the next, tells you which chapter to tackle next and lets you ask for the method or exercise you are missing. Create your space, an email is enough.

The thread of the chapter

A bit pattern means nothing on its own: it is the INTERPRETATION chosen, unsigned, two's complement or IEEE 754, that decides the value it represents.

This chapter is part of 201-N11, Mathematics for Computer Science

The essentials

Positional weights, in every base

  • • In base bb, the digit in position kk has weight bkb^{k}; after the radix point, the weights are b−1b^{-1}, b−2b^{-2}, and so on.
  • • A byte carries the weights 128128, 6464, 3232, 1616, 88, 44, 22, 11, from the most significant bit to the least significant one.
  • • One hexadecimal digit is exactly 44 bits, one octal digit 33 bits: the bits are ALWAYS grouped starting from the right.
  • • Unsigned, on nn bits: from 00 to 2n−12^{n}-1. In two's complement: from −2n−1-2^{n-1} to 2n−1−12^{n-1}-1.
01281641320160814020164 + 32 + 4 = 100the leftmost position weighs the most
The pattern 0110010001100100 adds up the weights of the positions holding a 11: 64+32+4=10064+32+4=100, and the leftmost position is the one that weighs the most.

Knowing the eight weights of a byte spares you any conversion calculation below 256256: reading them directly is faster and safer than repeated division.

Converting, one way and the other

  • • Decimal to base bb, integer part: repeated division by bb, remainders read from BOTTOM TO TOP.
  • • Fractional part: repeated multiplication by bb, integer parts read from TOP TO BOTTOM.
  • • Base bb to decimal: sum of the digits multiplied by their weights, with no division at all.
  • • A terminating decimal has a terminating binary expansion only if its reduced denominator is a power of 22.
0.375 × 2 = 0.7500.75 × 2 = 1.510.5 × 2 = 11read top to bottom: 0.011
0.3750.375: multiply by 22 and keep the integer part, read from TOP to BOTTOM: 0.01120.011_{2}. Stop when the fractional part drops to 00.

The two reading directions are opposite, and that is the only thing to memorize: integers from bottom to top, fractions from top to bottom.

Two's complement

  • • Two's complement of xx: invert all the bits, THEN add 11. The resulting pattern equals 2n−x2^{n}-x.
  • • The most significant bit acts as the sign bit: 11 means negative.
  • • The range is not symmetric: on 88 bits, from −128-128 to 127127, because zero takes up a slot on the positive side.
  • • Signed overflow: both operands have the SAME sign and the result has the opposite sign. Two operands of opposite signs never overflow.
patternunsignedsigned00000001117710008−8110012−4111115−1
The same four-bit pattern reads 1212 as unsigned and −4-4 in two's complement: it is the convention chosen, not the pattern, that gives the value.

Two's complement exists so that a single addition serves for positive and negative numbers alike. That is why subtraction is just the addition of the complement.

Bitwise operations, shifts and IEEE 754

  • • AND to CLEAR bits, OR to SET them, EXCLUSIVE OR to TOGGLE them.
  • • Shifting left by kk positions: multiplication by 2k2^{k}. Shifting right: integer division by 2k2^{k}.
  • • IEEE 754 single precision: 11 sign bit, 88 exponent bits with a bias of 127127, 2323 mantissa bits with an implicit 11.
  • • Integers are exact up to 2242^{24}, that is 16 777 21616\,777\,216; beyond that, two neighbouring integers share the same representation.
18 bits23 bitsSexponentmantissabias of 127with an implicit leading 1integers exact up to 16,777,216
The 3232 bits of a single-precision float: one for the sign, eight for the biased exponent, twenty-three for the mantissa, whose leading 11 is not stored.

An equality test between two floats is almost always a programming error: compare ∣a−b∣|a-b| with a tolerance, exactly as in the checks of this sheet.

The mistakes that cost marks

These are the errors I correct most often in session. Each one costs marks on a paper, even when the reasoning behind it is right.

1. Reading the remainders of repeated division in the wrong order

1.5 marks, and the weight check is immediate

What not to write

“1313 divided by 22 gives the remainders 11, 00, 11, 11, so 101121011_{2}.”

What to write

“The remainders are read from BOTTOM TO TOP: 110121101_{2}, which is indeed 8+4+1=138+4+1=13.”

13 = 2 × 6 + 116 = 2 × 3 + 003 = 2 × 1 + 111 = 2 × 0 + 111101: right1011: wrong
The first remainder is the LEAST significant bit: read the remainders from bottom to top, 11012=131101_{2}=13 (green); read from top to bottom, they give 10112=111011_{2}=11 (red).

Why: The first remainder obtained is the one of LOWEST weight, since the division starts from the ones. Rebuilding the number from its weights catches the error in ten seconds.

2. Grouping four bits at a time starting from the left

2 marks, and a value multiplied by a power of two

What not to write

“1101012110101_{2} splits into 11011101 and 0101, so D116\text{D}1_{16}.”

What to write

“Group from the RIGHT: 1111 and 01010101, padded on the left with zeros, so 0011 01010011\ 0101, that is 351635_{16}.”

110101from the right: 35110101from the left: D1
Groups of four bits are formed from the RIGHT: 11 ∣ 010111\,|\,0101, that is 351635_{16} (green); cutting from the left gives D116\text{D}1_{16}, a different number (red).

Why: The grouping follows the weights, which are counted from the ones. Padding on the left with zeros does not change the value; padding on the right multiplies it.

3. Inverting the bits without adding one

2 marks, and every addition that follows

What not to write

“55 is 0000 01010000\,0101, so −5-5 is 1111 10101111\,1010.”

What to write

“Invert THEN add 11: 1111 10111111\,1011. Check that 5+251=2565+251=256.”

Why: One's complement, without the +1+1, leaves two representations of zero and breaks addition. The check is arithmetic: the pattern of a negative −x-x equals 2n−x2^{n}-x read as unsigned.

4. Diagnosing an overflow from the carry out

2 marks, and the diagnostic question is worth double on the exam

What not to write

“A carry comes out of the most significant bit, so there is an overflow.”

What to write

“In two's complement, overflow is read from the SIGN: two positive operands giving a negative result, or the reverse.”

011001000011001010010110100+ 50= −106both operands are positivethe sign bit has turned to 1overflow: 150 does not fit in a signed byte
Two positive operands, 100100 and 5050, give a pattern whose leading bit is 11: the result reads −106-106, and the overflow is there, with no carry out at all.

Why: The carry out signals an UNSIGNED overflow. In two's complement, it is often perfectly normal, for example when adding two negative numbers whose sum still fits.

5. Confusing the two representable ranges

1 mark, and a wrong conclusion about an overflow

What not to write

“On 88 bits the range is −255-255 to 255255.”

What to write

“Unsigned: from 00 to 255255. Two's complement: from −128-128 to 127127, the range not being symmetric.”

0255unsigned−128127signed
On 88 bits, 256256 patterns: from 00 to 255255 as unsigned, from −128-128 to 127127 in two's complement, one step more on the negative side.

Why: Eight bits encode only 256256 values, whatever the convention. Since zero counts on the positive side, one more value remains on the negative side.

6. Believing that a simple decimal is written exactly in binary

2 marks on the analysis question, and bugs in programming

What not to write

“0.1+0.2=0.30.1+0.2=0.3, the computer will confirm it.”

What to write

“0.10.1 has no terminating binary expansion: the computer returns 0.300000000000000040.30000000000000004, and you always compare with a tolerance.”

Why: A terminating decimal has a terminating binary expansion only if its reduced denominator is a power of 22. One tenth is 110\dfrac{1}{10}, whose denominator contains a 55.

7. Clearing a bit with an OR

1.5 marks, and the program does the opposite of what it claims

What not to write

“To set bit 33 to zero, I use an OR with 0000 10000000\,1000.”

What to write

“OR SETS bits. To clear, you need an AND with the INVERTED mask: 1111 01111111\,0111.”

Why: Each operator has a single role: AND clears, OR sets, EXCLUSIVE OR toggles. An OR with a 11 can never produce a 00.

8. Confusing a hexadecimal digit with a byte

1 mark, and an image size calculation divided by two

What not to write

“The colour FF\text{FF} fits in one hexadecimal digit, so in 88 bits each: three digits for an RGB colour.”

What to write

“One hexadecimal digit is 44 bits; a byte needs TWO. An RGB colour is therefore written with six hexadecimal digits.”

Why: 16=2416=2^{4}: each hexadecimal digit encodes exactly four bits. That is the whole reason for this base, which makes binary readable without distorting it.

9. Forgetting the bias or the implicit 1 in IEEE 754

2 marks, and an absurd order of magnitude

What not to write

“The stored exponent is 1000001110000011, that is 131131, so the number is of the order of 21312^{131}.”

What to write

“The exponent is BIASED by 127127: 131−127=4131-127=4, so the number is of the order of 24=162^{4}=16.”

Why: The bias makes it possible to encode negative exponents without an extra sign bit. And the mantissa is always written with a 11 before the point, which is not stored since it is certain.

Which method to choose

Which bitwise operation for which effect

Read what the program must do to the targeted bits.

  • If set some bits to zero without touching the others → AND with a mask where the bits to clear are 00

    Example: x AND 1111 0111x\ \text{AND}\ 1111\,0111 clears bit 33

  • If set some bits to one → OR with a mask where the bits to set are 11

    Example: x OR 0000 1000x\ \text{OR}\ 0000\,1000 sets bit 33

  • If invert some bits → EXCLUSIVE OR with a mask where the bits to toggle are 11

    Example: applied twice, it gives back the starting value

  • If test whether a bit is 11 → AND with the mask, then compare the result with zero

    Example: x AND 0000 1000x\ \text{AND}\ 0000\,1000 nonzero means bit 33 is set

  • If multiply or divide by a power of two → shift left or right by kk positions

    Example: shifting left by 33 multiplies by 88

    to the right, it is an INTEGER division: the bits pushed out are lost

Write a mask in binary first, then convert it to hexadecimal for the code. Writing it directly in hexadecimal is the source of half of all mask errors.

Which interpretation to give a bit pattern

Read what the question says about the CONTEXT, never the pattern alone.

  • If the question talks about an unsigned integer, a size or a counter → sum of the weights, from 00 to 2n−12^{n}-1

    Example: 1001 01101001\,0110 is 150150

  • If the question talks about a signed integer or two's complement → a leading bit of 11 means negative: the value is pattern−2n\text{pattern}-2^{n}

    Example: 1001 01101001\,0110 is −106-106

  • If the question talks about a floating-point number → split 1+8+231+8+23, remove the bias 127127, restore the implicit 11

    Example: stored exponent 131131 for an actual exponent of 44

  • If the question talks about text, a character or a key → ASCII code, one byte per character for the first 128128

    Example: 0100 00010100\,0001 is the letter A, that is 6565

The same 88-bit pattern can be 150150, −106-106, a character or a colour. Naming the interpretation chosen before computing is worth one mark on every reading question.

How the answer is expected to be written

A marker ticks steps. Here they are in order, with the concluding sentence expected word for word.

Converting by repeated division

When to use it: The question asks you to go from decimal to another base, with or without a fractional part.

  1. 1 Write the divisions one under the other, with the quotient and the remainder on each line.
  2. 2 Stop when the quotient is 00, never before.
  3. 3 Read the remainders from BOTTOM TO TOP for the integer part, and write the result with the base as a subscript.
  4. 4 For the fractional part, multiply repeatedly and read the integer parts from TOP TO BOTTOM.
  5. 5 Check by rebuilding the number from the weights.

Concluding sentence

“13=1101213=1101_{2}, since 8+4+1=138+4+1=13.”

The trap: Stopping the divisions when the quotient is 11: the last remainder, the most significant one, is then lost.

Marking: 1 mark for the divisions set out, 1 mark for the reading direction, 0.5 mark for the weight check.

Adding in two's complement and concluding about overflow

When to use it: The question asks for an addition of nn-bit signed integers, or asks whether the result is correct.

  1. 1 Write both operands on the SAME number of bits, padding on the left with the sign bit.
  2. 2 Add bit by bit with the carries, and ignore the carry that comes out of the leftmost position.
  3. 3 Read the sign bit of the result and compare it with those of the operands.
  4. 4 Conclude: overflow only if both operands have the same sign and the result the opposite sign.

Concluding sentence

“100+50100+50 gives 1001 01101001\,0110, that is −106-106: two positives giving a negative, there is an overflow, and the result is unusable.”

The trap: Concluding that there is an overflow because a carry comes out: that is the unsigned criterion, not the two's complement one.

Marking: 1 mark for the alignment, 1 mark for the addition, 1 mark for the sign criterion, 1 mark for the conclusion.

Check before you hand in

Five minutes of checking recover more marks than one more problem started in a hurry.

The typical problem, taken apart

An 8-bit signed addition that overflows

A sensor adds two readings, 100100 and 5050, in a variable encoded on 88 bits in two's complement.

Write the addition in binary, give the value read by the program, and say whether there is an overflow.

Step 1

100=0110 0100100=0110\,0100 and 50=0011 001050=0011\,0010, both on 88 bits.

Why

Write both operands on the same number of bits before anything else: an addition on different widths makes no sense and loses the alignment mark.

Step 2

Bitwise sum: 0110 0100+0011 0010=1001 01100110\,0100+0011\,0010=1001\,0110.

Why

Binary addition is set out exactly as in decimal, with carries. Here no carry comes out of the leading position, which changes nothing.

Step 3

Unsigned reading: 128+16+4+2=150128+16+4+2=150, which is the expected sum.

Why

The pattern is right: the computer made no calculation error. The problem comes from the interpretation, not from the addition.

Step 4

Signed reading: the leading bit is 11, so the value is 150−256=−106150-256=-106.

Why

In two's complement, a pattern with a leading 11 is read by subtracting 282^{8}. That is the value the program will actually use.

Step 5

Criterion: two positive operands, negative result, so OVERFLOW.

Why

The criterion is about the signs, never about the carry out. Here the signed range stops at 127127, and 150150 exceeds it.

Step 6

Check: −106+256=150-106+256=150, and 150>127150>127.

Why

The first relation confirms the pattern, the second the diagnosis. On 1616 bits, the same addition would not overflow, since the limit would be 32 76732\,767.

The conclusion, written out

“The pattern obtained is 1001 01101001\,0110; read in two's complement on 88 bits, it is −106-106. Two positive operands giving a negative result, there is an overflow: the variable must be encoded on 1616 bits.”

The classic mistake on this problem: Answering 150150 because the pattern is indeed 150150: the question is about the value READ by a program that interprets it in two's complement.

Learn by heart

  • • Repeated division: remainders from BOTTOM TO TOP. Fractional part: integer parts from TOP TO BOTTOM.
  • • One hexadecimal digit is 44 bits, one octal digit 33 bits; always group from the RIGHT.
  • • Two's complement: invert THEN add 11; the pattern of −x-x equals 2n−x2^{n}-x as unsigned.
  • • On nn bits: unsigned from 00 to 2n−12^{n}-1, signed from −2n−1-2^{n-1} to 2n−1−12^{n-1}-1.
  • • Signed overflow: both operands of the SAME sign and a result of the opposite sign. The carry out proves nothing.
  • • AND clears, OR sets, EXCLUSIVE OR toggles. A left shift multiplies, a right shift divides as integers.
  • • IEEE 754: 1+8+231+8+23 bits, exponent biased by 127127, implicit 11, integers exact up to 2242^{24}.

Frequently asked questions

In which direction do you read the remainders of repeated division?

From bottom to top, that is, from the last remainder obtained back to the first. The first remainder corresponds to the least significant digit, since the first division deals with the ones. Only stop when the quotient becomes zero, otherwise the most significant digit is lost.

How do you write a negative number in two's complement?

First write its absolute value in binary on the required number of bits, then invert all the bits, and finally add one. The resulting pattern, read as an unsigned integer, equals two to the power n minus the starting number, which gives an immediate check.

How can you tell whether there is an overflow?

By comparing the signs. A signed overflow occurs when both operands have the same sign and the result has the opposite sign. A carry coming out of the leading position proves nothing in two's complement: it signals an overflow only in the unsigned interpretation.

Why is 0.1 plus 0.2 not exactly 0.3 on a computer?

Because a decimal has a terminating binary expansion only if its reduced denominator is a power of two. One tenth has a five in its denominator, so its binary expansion is infinite and the computer rounds it. The rounding error survives the addition, hence the rule of always comparing two floats within a tolerance.

How many bits is one hexadecimal digit?

Exactly four, since sixteen is two to the power four. A byte therefore needs two hexadecimal digits, and a red green blue colour, which takes three bytes, is written with six digits. Grouping in fours always starts from the right, padding on the left with zeros.

Practise it

Corrected exercises: Number systems in computing, 201-N11

A method is proved on a paper, not on a sheet. The set for the same chapter takes each of these traps into a problem, with the solution written out step by step.

  • 12 corrected exercises
  • 120 points
  • 180 minutes
Do the exercises
Something missing from this sheet? A method, a trap, an exercise to practise it, a mistake you spotted: tell me, I read every request.

Sending goes through your free space: that is how I can answer you and tell you when the exercise is added. An email is enough, no password. Create your space. Rather not create an account? Write to me directly.

What is it about?
I read every request myself, nobody else sees it. No need to write your name or your school.
When your request is dealt with, you hear about it by email and in your space.

By sending, you agree that I read your request to answer it. It is kept with your account, erased with it, and never published: privacy policy.

Previous sheet Proof techniques Next sheet Modular arithmetic

© Ahmed Squalli Houssaini. Revision sheet published at www.letuteurscientifique.ca/en/fiches/cegep-n11-number-systems. Free for personal and classroom use; republishing it elsewhere requires written permission (legal notice).

See also

Looking for a 201-N11 tutor in Montreal?

Get in touch for a first session. With a B.Sc. in computer science from McGill and an M.Sc. in applied computer science from Concordia, I work through number systems on what actually breaks in real programs: silent overflows and floating-point comparisons.

Site by Studio Squalli