CS/EE 220  /  Class 2

Class 2

Number systems and codes

Every conversion this class asks for, named the way the slides name it, worked both ways, with every number computed rather than typed.

2026-09-02Slides (PDF)number systemsbase conversionBCDGray codequiz 1

A digital circuit stores nothing but ones and zeros. Everything else, a temperature, a price, the letter A, has to be written in that alphabet before the hardware can hold it. This class covers the two ways that happens. A number system writes a quantity using place values, so the pattern of bits and the size of the number are tied together by arithmetic. A code just assigns patterns to things, so the pattern means whatever the table says it means. Telling those two apart is most of the work.

Lecture 2 names its conversion methods rather than leaving them to taste, and Problem Set 1 then demands a named method problem by problem. This page follows the deck's own naming.

direction method the deck names slide
any base into decimal power series expansion, also called weightage 13 to 16
decimal into any base, integer part weightage, or successive division 17 to 20
decimal into any base, fraction part weightage, or successive multiplication 17, 21 to 24
decimal into hexadecimal successive division by 16, successive multiplication by 16 26
binary into hexadecimal and back the long method, or the short method 27, 30, 31

01What a positional number system actually says

Write a number as a string of digits and you have written a sum. Position carries the weight , so the string is shorthand for

Every method in this class is that one formula, read forwards or backwards. Read forwards it turns any base into decimal. Read backwards it turns decimal into any base.

The decimal system is the case you already know. In the digits sit at positions , so the number means , which comes to 5374.5. Binary works the same way with and only two digits to choose from. Hexadecimal works the same way with , using through for the digit values ten through fifteen because a single symbol is needed for each.

One number, four ways of writing it

02Any base into decimal: power series expansion

Slide 13 gives this direction one method and one name. Write each digit against its weight, multiply, add. The deck calls it power series expansion, and Problem Set 1 calls the same thing the weightage method. Problem 2 and Problem 4 both demand it by name, and Problem 4 adds "explicitly write the powers of 16", so the working carries the marks.

Worked example 1 ICP 2-1, convert to decimal

Write the positions under the digits first, starting at on the digit left of the point.

Now multiply and add, keeping the zero terms so the working shows every bit.

The answer is 53.750 in decimal.

Worked example 2 ICP 2-2, convert to decimal

Replace the letters with their values first: and . Then apply the same sum with .

03Decimal into any base: the two methods

Slide 17 puts the two methods side by side and gives them their names. Weightage works downwards from the biggest weight. Successive division works upwards from the smallest. They reach the same answer, and Problem Set 1 names which one it wants for each part, so read the instruction line before starting.

Weightage. Find the largest power of the radix that fits inside the number, mark that position with the number of times it fits, subtract, and repeat on what is left. Positions you never used are . In binary a power either fits once or not at all, which is what makes the method quick there.

Successive division. Divide by the radix and keep the remainder. Divide the quotient by the radix and keep that remainder. Stop when the quotient reaches . The remainders are the digits, and they come out least significant first, so the answer is the remainders read bottom to top.

Slide 19 prints the powers of two beside the worked example, because the weightage method needs them at hand.

1 2 6 64
2 4 7 128
3 8 8 256
4 16 9 512
5 32 10 1024

Worked example 3 Slide 19 and slide 20, by both methods

Weightage. Take the largest power of two that fits, subtract, repeat.

The powers used are 32 + 16 + 4 + 1, so those positions carry a and the columns for and stay empty, giving 110101.

Successive division. Divide by and keep every remainder.

Reading the remainders upwards gives 110101, the same answer.

Worked example 4 ICP 2-3, convert to binary

The deck asks for this one straight after the weightage slide, so use weightage. The largest power of two below is .

The powers used are 512 + 64 + 32 + 16 + 1, which lights those columns and leaves the rest at zero: 1001110001. Adding the weights back returns 625.

Worked example 5 ICP 2-4, convert to binary

This one follows the successive division slide, so divide.

Reading the remainders bottom to top gives 1101001. Checking by weightage, the powers used are 64 + 32 + 8 + 1, which add to 105.

04Decimal into hexadecimal

Slide 26 splits this by part. The integer part takes successive division by until the quotient is . The fraction part takes successive multiplication by until the fraction is , or until the question's precision is reached. Weightage works here too, and Problem Set 1 Problem 3 asks for a place-value approach on three of its six parts.

One extra step at the end of either method: a digit above nine becomes a letter, through .

Worked example 6 into hexadecimal by successive division

Reading the remainders upwards gives 7E9. A remainder above nine becomes a letter, which is why the middle one is written .

Check it by place value: .

Worked example 7 ICP 2-6, part one: into hexadecimal

is already larger than , so the answer has two digits.

Reading upwards gives 99.

The digits look like the decimal number ninety-nine and are worth 153 instead. A hexadecimal string means nothing until you say what base it is in.

05Fractions: the same two methods, mirrored

Slide 21 sets the shape of every mixed conversion in three steps: convert the integer part, convert the fraction part, join the two results with a radix point. Slide 22 then gives the fraction its own procedure, and it mirrors the integer one. Division becomes multiplication, and the digits come out in the opposite order.

Successive multiplication. Multiply the fraction by the new radix. The digit that appears to the left of the point is the next digit of the answer. Write it down, discard it, and multiply what remains. Digits come out most significant first.

Weightage. Subtract the largest negative power of the radix that fits, mark that position with a , and repeat on the remainder. Positions whose weight does not fit carry a . Problem Set 1 Problem 5 demands this method by name on four of its eight parts, so it is worth as many marks as the multiplication method.

position weight value
one half 0.5000
one quarter 0.2500
one eighth 0.1250
one sixteenth 0.06250
one thirty-second 0.03125
one sixty-fourth 0.01563

Stop when the fraction reaches zero. It may never reach zero. When a fraction you have already seen comes back, the digits from that point on repeat forever.

Worked example 8 Slide 23, into binary by both methods

Successive multiplication. Multiply, harvest the digit on the left of the point, discard it.

The fraction is now zero, so the process stops and the digits read in the order they appeared: .

Weightage. Subtract the largest negative power that fits, and record which one it was.

The weight 0.25 never fits, so carries a , and the answer is the same .

Adding the weights back confirms it: .

Worked example 9 ICP 2-5, convert to binary

Multiply and harvest.

The leftover at step six is the same leftover the third step started from, so everything between them repeats forever. Written with the block marked, , a block of 4 digits after 2 settled ones.

The first thirteen digits are 1010011001100.

Successive multiplication, every step

Worked example 10 ICP 2-6, part two: into hexadecimal

The same method with in place of .

The fraction reaches zero after two steps, so the answer is . Check it by place value: .

Two steps in hexadecimal did the work of 6 steps in binary, because one hexadecimal digit carries four bits.

06Mixed numbers: convert the halves, join at the point

Slide 21 is explicit that the two halves never mix. Convert the integer part by division or weightage, convert the fraction part by multiplication or weightage, then write them either side of a radix point. The methods for the two halves are chosen separately, and Problem Set 1 Problems 14 and 15 name a different one for each half of the same number.

Worked example 11 ICP 2-6, part three: into hexadecimal

The two halves are already done. The integer part gave 99 and the fraction part gave , so the answer is .

Converting back checks both halves at once: .

Worked example 12 Problem Set 1, problem 15: end to end

The problem names a method for each step, which is the pattern to expect in a quiz.

Integer part by weightage.

The powers used are 128 + 32 + 8 + 4 + 1, giving 10101101.

Fraction part by successive multiplication.

That gives .

Join them. .

Into hexadecimal by the short method. Group outward from the point: 1010 1101 and 1011, which read .

Back to decimal to verify. .

07Binary and hexadecimal: the long method and the short one

Slide 30 names two ways across, and it is worth knowing both. The long method goes through decimal: binary to decimal to hexadecimal one way, hexadecimal to decimal to binary the other. The short method skips the arithmetic, because one hexadecimal digit covers exactly the sixteen values four bits can take. A nibble and a hex digit are the same object written two ways.

Slides 30 and 31 both carry the substitution table and both say to memorise it.

decimal hex binary decimal hex binary
0 0 0000 8 8 1000
1 1 0001 9 9 1001
2 2 0010 10 A 1010
3 3 0011 11 B 1011
4 4 0100 12 C 1100
5 5 0101 13 D 1101
6 6 0110 14 E 1110
7 7 0111 15 F 1111

For the short method, group in fours moving outward from the radix point, leftwards through the integer part and rightwards through the fraction. Pad with zeros at the outer ends only. A number with no point has its point at the far right, which is why plain integers are grouped from the right.

Worked example 13 Slide 30's example, both ways

Short method. Slide 30 groups into and reads off 16CAB. Padding the short leading group to changes nothing, since leading zeros never change a value.

Long method. Expand the binary by weightage to get 93 355 in decimal, then divide by sixteen.

Reading the remainders upwards gives the same 16CAB.

The short method took no arithmetic. The long method took 5 divisions and a sum over seventeen bits. Use the short one, and keep the long one for checking a claim.

Worked example 14 ICP 2-7, convert to binary

Hexadecimal into binary is the substitution run backwards: replace every digit with its four bits and keep them in place.

, , , and after the point .

Written out, . Dropping the leading zeros of the first group and the trailing zeros of the fraction, which change no value, gives .

Checking by the long method: , and successive division by two gives

which reads upwards as 1110100110. The fraction is , which is , so .

Worked example 15 A number with a radix point, going the other way

Take . The integer part groups leftwards as and pads to , giving . The fraction groups rightwards as and pads to , giving . The answer is .

Check the halves separately. , which is , so the integer part is right. And , which is , so the fraction is right too.

08Bits, bytes, nibbles, and the prefixes that lie

A bit is one binary digit. A nibble is four bits, which is one hex digit. A byte is eight bits, which is two hex digits. Within a number, the leftmost bit is the msb and the rightmost the lsb, both lower case. Within a multi-byte value the leftmost byte is the MSB and the rightmost the LSB, both upper case. The slides use both conventions on the same slide, so read the case.

Memory sizes are counted in powers of two, because addresses are binary. The prefixes borrowed from decimal do not line up with them.

prefix power of two value nearest decimal prefix gap
kibi, Ki 1024 kilo, 2.40%
mebi, Mi 1 048 576 mega, 4.86%
gibi, Gi 1 073 741 824 giga, 7.37%
tebi, Ti 1 099 511 627 776 tera, 9.95%

The gap grows with every prefix, and slide 12 records what it cost. Buyers sued the memory card makers for selling cards measured in decimal megabytes while quoting capacities readers expected in binary ones. The suit put the overstatement at four to five percent, and the computation lands at 4.86%.

Worked example 16 8 Mi bits

and , so , which is 8 388 608 bits. Eight million would be 8 000 000, short by 388 608 bits.

09Ranges: how many bits, how many digits, how many values

Four facts settle every question of this shape, and the first two are the ones students swap.

question answer at
how many values do bits hold 65 536
what is the largest value 65 535
how many bits does need smallest with 16 for
how many hex digits for bits 4

Worked example 17 A sixteen bit unsigned word

The smallest value is and the largest is . In hexadecimal that is FFFF, four digits, and expanding each back to returns 1111111111111111, sixteen ones.

Hexadecimal is convenient here because sixteen bits is exactly four nibbles. The conversion is substitution with no arithmetic, nothing is lost, and the written form is four times shorter.

Worked example 18 Crossing a power of the radix

needs 16 bits and 4 hex digits. Add one and needs 17 bits and 5 hex digits, because it is and the new bit forces a fifth digit. One extra unit of value costs a whole extra digit whenever you cross a power of the base.

10Binary codes

A number system ties the bit pattern to the size of the number. A code does not. A code is a mapping from a set of things to a subset of the patterns bits can take, and a pattern means whatever the table says. Unused patterns mean nothing at all.

To label different things you need the smallest with . Seven rainbow colours need 3 bits, and slide 33 leaves unused. Twenty six letters need 5 bits, with 6 patterns spare.

Worked example 19 ICP 2-8, how many bits represent the decimal digits

There are ten digits, through . Three bits give 8 patterns, which is too few. Four bits give 16, which is enough with 6 patterns left over.

The answer is 4 bits. Those six spare patterns are what the rest of this section is about.

Four bits and ten digits leave six patterns over, and slide 34 points out that there are 8008 ways to choose which ten to use. Four of those choices earned names.

decimal BCD, Excess-3 Gray
0 0000 0011 0000 0000
1 0001 0100 0111 0100
2 0010 0101 0110 0101
3 0011 0110 0101 0111
4 0100 0111 0100 0110
5 0101 1000 1011 0010
6 0110 1001 1010 0011
7 0111 1010 1001 0001
8 1000 1011 1000 1001
9 1001 1100 1111 1000

BCD uses the natural weights but stops at nine. Encode each decimal digit on its own, four bits each. The patterns through never appear, which is what makes "illegal in BCD" while staying a perfectly ordinary in binary. The word illegal describes the code, not the bits.

Excess-3 adds three to the BCD value. It is self-complementing: flip every bit of a code word and you get the code for minus that digit. The build checks all ten pairs.

is weighted like BCD, with two of the weights negative. Read as . It is self-complementing too.

Gray has no weights at all. Its defining property is that neighbouring digits differ in exactly one bit, and the column wraps, so nine back round to zero also differs in one bit. Reading it means looking it up.

Worked example 20 396 and 185 in BCD

Slide 35 encodes digit by digit as 0011 1001 0110, which is 12 bits.

Now compare . In BCD it is 0001 1000 0101, again 12 bits. In plain binary it is 10111001, only 8 bits. The two are different lengths and different patterns, because they answer different questions. BCD wastes six of the sixteen patterns in every nibble, so each nibble carries bits of information instead of four.

Alphanumeric codes

Letters, digits and punctuation need one code between them. Slide 36 gives ASCII, laid out with choosing the column and choosing the row. Seven bits give 128 characters, which covers upper case, lower case, the ten digits, punctuation and thirty three control codes.

Read a character off the table by joining its column to its row. Capital sits in column , row , so it is 1000001, or 41 in hexadecimal and 65 in decimal. Lower case letters sit two columns right, which adds 32 to every value, and the ten digit characters start at 30, so the character is 37 rather than .

11What the quiz has asked before

12Check yourself

1A number is written in base . Which digit is the msd, and what weight does carry?

is the most significant digit. carries the weight , the first place after the radix point. In binary that weight is 0.500.

2Convert to decimal by power series expansion, showing every bit's contribution.

.

3Convert to binary by successive division, then check the answer by weightage.

The remainders read upwards give 110000101. The weights that are set are 256 + 128 + 4 + 1, which add to 389.

4Convert to binary by the weightage method.

The powers used are 1024 + 512 + 128 + 64 + 1, giving 11011000001.

5Convert to hexadecimal.

Reading upwards: 3039. Check by place value: .

6Convert to binary by the weightage method, and name the recurring block.

Carrying on, the digits are 01110011001100, and the leftover fraction comes back, so recurring.

7Convert to binary by successive multiplication. Does it terminate?

The leftover comes back at the fifth step, so it never terminates: recurring from the first digit.

8Convert to binary, using weightage for the integer part and successive multiplication for the fraction.

Integer part:

Fraction part:
The integer part uses the powers 8 + 4 + 1, giving 1101. The fraction gives . Joined: .

9Convert to hexadecimal by the short method.

Thirteen bits, so pad the leading group to four: . That reads 1D6B.

10Convert to binary.

Substitute a nibble per digit: , , , so . In decimal that is 2908.

11A four digit hexadecimal number: how many values can it take, and what is the largest?

Four hex digits is sixteen bits, so 65 536 values, the largest being 65 535. The count and the maximum differ by one because counting starts at zero.

12How many bits are in bits, and why is that not eight million?

. Mi means , not , and the two differ by 4.86%.

13Give in BCD and in plain binary, and say why they differ in length.

BCD is 0001 1000 0101, at 12 bits. Binary is 10111001, at 8 bits. BCD spends four bits per decimal digit and leaves six of the sixteen patterns unused in each one.

14Decode 0111 1010 as Excess-3.

Read each nibble as a binary value, 7 and 10, then subtract three from each. The answer is .

15Why is illegal in BCD but not in binary?

BCD encodes one decimal digit per nibble and there are only ten digits, so only 0000 through 1001 are assigned. The remaining 6 patterns encode nothing. In ordinary binary is just 10.

16Capital is at column , row of the ASCII table. Write it in binary, hexadecimal and decimal.

Joining column to row gives 1000001, which is 41 in hexadecimal and 65 in decimal.

Answer out loud before opening one.

13Cheat sheet

Cheat sheet

task method the deck names watch for
any base to decimal power series expansion, first fraction digit is
decimal to binary, integer weightage: largest power of that fits, subtract, repeat write the unused columns as
decimal to binary, integer successive division: divide by , remainders bottom to top finish the step
decimal to hex, integer successive division by , remainders bottom to top remainders above become to
decimal to hex, integer weightage on the coefficient can be up to , unlike binary
decimal fraction, any base weightage: subtract the largest negative power that fits a weight that does not fit still costs a
decimal fraction, any base successive multiplication by , harvest the digit, discard it carry the fraction only, never the whole product
a fraction that recurs a leftover fraction comes back round or truncate to the stated width
mixed number convert the halves separately, join at the point each half can be told to use a different method
binary to hex short method: group four outward from the point pad on the outer ends only
binary to hex long method: binary to decimal to hex only worth it as a check
hex to binary one nibble per digit, straight from the table drop only the outer padding
how many values in bits 65 536 at
largest value in bits 65 535 at
bits needed for smallest with is ones
hex digits for bits a nibble is one hex digit
binary prefixes Ki, Mi, Gi, Ti are , , , gap to kilo, mega, giga, tera grows
BCD four bits per decimal digit, to unused
Excess-3 BCD plus three self-complementing
weighted, two weights negative self-complementing
Gray table lookup, neighbours differ in one bit no weights to compute with
ASCII seven bits, column , row the character is 37, not

Symbols and notation

press G to toggle
symbol say it what it means units
"are" the radix, or base: how many different digits the system has none, a count
"ay sub eye" the digit sitting at position none, a digit
"eye" the position, counted from at the radix point, negative to the right none
"en" how many digits sit left of the radix point none, a count
"em" how many digits sit right of the radix point none, a count
"ay sub en minus one" the most significant digit, msd, the leftmost one none
"ay sub minus em" the least significant digit, lsd, the rightmost one none
"two to the minus one" the weight of the first digit after the point, worth none
"ceiling of ex" round up to the next whole number none
"bee seven down to bee one" the slides' bit numbering for ASCII, most significant none

The subscript is the place. The digit is the value. Keep those separate and the rest of the class follows.