←
Class 11 Computer Science
2. Encoding Schemes and Number Systems · 2 / 11
⌕
Chapter 2 of 11

2. Encoding Schemes and Number Systems

6 sections · 7,352 words

About this chapter

203 words · 1 min read

Big idea: Computers store text and numbers as bits. Encoding and bases let people read those bits.

This is an original lesson that follows the Class 11 Computer Science syllabus. Read the key words first. Cover a worked answer and try the steps yourself before you look. For a program, say what it will print before you read the real output.

  • Explain how text becomes numbers and then bits.
  • Compare ASCII, ISCII, and Unicode, including UTF-8.
  • Name decimal, binary, octal, and hexadecimal systems.
  • Convert whole numbers both ways among those bases.
  • Convert fractions, add and subtract in binary, and use complements.
  • Say why hexadecimal is used for addresses, colours, and MAC codes.

Early machines in the 1940s and 1950s used private character codes. Text from one machine could fail on another. In 1963 a 7-bit English table called ASCII was published. In the 1980s ISCII added codes for several Indian scripts while keeping ASCII in the lower 128 slots. Separate 8-bit tables still clashed across languages. The Unicode Consortium formed in 1991. Unicode now gives one code point to letters, marks, and symbols. UTF-8 later became the usual way to store those points as bytes.

2.1 Encoding schemes

1,758 words · 8 min read

A computer cannot store letter shapes. It stores a number for each character.

Circuits have two states. We write them as 0 and 1.

People use letters, digits, and marks. A key does not send the printed shape.

An encoding scheme is a table of rules. Each character gets one number.

That number is then stored as bits.

Without a shared table, each machine could pick its own numbers. A file would then look like noise on another screen.

A key sends a letterA table gives a numberThe number becomes bitsMemory stores those bits
A letter becomes a number, then bits, then stored signals.

How to read the figure. Start at the top. A key is pressed. A table maps it to a number. The number becomes bits. Memory stores the bits.

When text returns to the screen, software reverses the path. Bits become a number. The number selects a glyph from a font.

The machine stores a number. It does not store ink shapes.

Encoding matters because files move between machines. A shared table keeps the same letter on every screen.

ASCII means American Standard Code for Information Interchange.

It used 7 bits at first. Seven bits give 27=1282^7 = 128 patterns.

So 7-bit ASCII has 128 codes, from 0 to 127.

English letters, digits, and common marks fit in that set. That is why 7 bits were enough for early English text.

A modern byte has 8 bits. ASCII often sits in a byte with a leading 0. Decimal 65 is 01000001 in 8 bits.

Codes 0 to 31, and 127, are control codes. They tell a device to move or beep.

Codes 32 to 126 are printable. Letters, digits, space, and marks sit here.

CharacterDecimal code7-bit binary
Space320100000
0480110000
1490110001
9570111001
A651000001
B661000010
C671000011
Z901011010
a971100001
b981100010
c991100011
z1221111010

How to read the table. Digits run from 48 to 57. Capitals run from 65 to 90. Small letters run from 97 to 122.

A small letter is 32 above its capital. The bit that means 32 flips.

That gap of 32 is useful. Adding 32 to 65 gives 97. A becomes a.

Digits are also in order. “0” is 48. “9” is 57. You can test a digit by its code range.

Space is 32. It is a real character. It is not “nothing”.

Line feed is code 10. It is a control code. It does not print a letter.

Control jobDecimal code
Bell / alert7
Line feed10
Carriage return13
Space (printable)32

How to read the table. Control codes sit below 32, except space. Space prints a blank. It still has a code.

SchemeBits per codeCharacter count
ASCII7128
ISCII8256
UTF-3232one code, four bytes

How to read the table. More bits mean more codes. Count is 2bits2^{\text{bits}} for a fixed width.

A keyboard sends the word CODE in capitals. Give the ASCII decimal code and the 7-bit form of each letter.

Goal. Find four decimal codes and four 7-bit strings.

Rule. Use the ASCII table. Then write each code in binary.

Steps. C is 67. O is 79. D is 68. E is 69.

Arithmetic. 67 = 1000011. 79 = 1001111. 68 = 1000100. 69 = 1000101.

Check. Each string has 7 bits. Each starts with 100, as capitals do.

Result: CODE is 67 79 68 69, or 1000011 1001111 1000100 1000101.

A machine receives 1001000 then 1001001. Which two letters is that?

Goal. Turn two 7-bit codes into letters.

Rule. Expand each bit by a power of 2. Then look up ASCII.

Arithmetic. First: 1×26+1×23=721 \times 2^6 + 1 \times 2^3 = 72. That is H.

Arithmetic. Second: 64+8+1=7364 + 8 + 1 = 73. That is I.

Result: The letters are H then I.

ISCII means Indian Script Code for Information Interchange.

It uses 8 bits. Eight bits give 28=2562^8 = 256 codes, from 0 to 255.

  • Codes 0 to 127 match ASCII. English stays the same.
  • Codes 128 to 255 hold letters and marks from several Indian scripts.

Scripts that share a sound can share a code. A font change can then switch the look.

ISCII still uses one byte per character. That fits old 8-bit files. It cannot hold every script at once.

The lower half is the bridge. English tools keep working. Extra letters sit above 127.

Old 8-bit tables clashed. The same number meant different letters.

Unicode gives one number to each character. That number is a code point.

A code point is written as U+ then hex digits. Capital A is U+0041.

U+0041 is 4×16+1=654 \times 16 + 1 = 65 in decimal. That matches ASCII.

The first 128 Unicode points match ASCII. A plain ASCII file is already Unicode.

A 16-bit field can hold 216=655362^{16} = 65536 values. Early Unicode used 16 bits for a large set of everyday characters.

Later, Unicode grew past that. Emoji and historic scripts needed more room. Code points now go far beyond 16 bits.

You still write a code point as U+ and hex. The U+ form is the name. The UTF form is the stored bytes.

Letter AU+0041Byte 41Unicode names the number. UTF stores the bits.ASCII A is still decimal 65.
A code point is a number. UTF-8 stores that number as bytes.

How to read the figure. The left box is the glyph. The middle box is the code point. The right box is how UTF-8 stores it for A.

Unicode names the numbers. A UTF says how to store the bits.

FormatBytes per characterUse
UTF-81 to 4ASCII uses 1 byte. Common on the web.
UTF-162 or 4Used inside some runtimes.
UTF-32always 4Easy to index. Heavy for English.

In UTF-8, English ASCII uses 1 byte each. Other characters may use 2, 3, or 4 bytes.

If the code point is 0 to 127, UTF-8 stores one byte. That byte equals the ASCII code.

If the code point is larger, UTF-8 uses extra bytes. The first bits of those bytes mark the length.

UTF-16 uses two bytes for many characters. Some characters need a pair of two-byte units, so four bytes.

UTF-32 is simple. Every character is four bytes. Indexing is easy. English files become four times heavier.

Do not treat UTF-8 as always one byte. That is true only for ASCII.

A web page is often English tags plus a few extra letters. UTF-8 stays small for the tags. It still allows any letter.

UTF-8 is also backward friendly. A file of only ASCII bytes is already valid UTF-8.

That is one reason the web chose UTF-8. Old English files still open. New letters still fit.

A file has 500 English ASCII characters. Find the size in UTF-8 and in UTF-32.

Goal. Two sizes in bytes, then a ratio.

Rule. UTF-8 uses 1 byte for each ASCII character. UTF-32 uses 4 bytes for each character.

Substitute. UTF-8: 500×1=500500 \times 1 = 500 bytes.

Substitute. UTF-32: 500×4=2000500 \times 4 = 2000 bytes.

Arithmetic. 2000 ÷ 500 = 4.

Result: 500 bytes in UTF-8. 2000 bytes in UTF-32. Four times larger.

ord() takes a character and returns its code. chr() does the reverse.

Python
>>> ord("A")
65
>>> ord("a")
97
>>> chr(65)
A
>>> chr(97)
a

The printed values match the ASCII table. 65 is A. 97 is a.

Python uses Unicode code points. For A, that is still 65. For many other characters, ord() returns a larger integer.

If you print chr(97), you get a. If you print ord("A"), you get 65. Try both before an exam.

The functions do not print binary. They print the integer code. You still convert that integer if the question asks for bits.

Exam questions may ask for the code, the bits, or the letter. Read which one is wanted. Then pick ord, chr, or a base conversion.

A string of four letters is four codes. Do not add the codes together. Keep them in order.

A key press becomes a code. ASCII names English codes. ISCII keeps those codes and adds more in the upper byte.

Unicode names every character. UTF-8 is one way to store the name as bytes. UTF-32 is another way, always four bytes.

If the question says “code of A”, the answer is 65. If it says “bits”, write 1000001. If it says “storage”, count bytes.

A mixed language file needs Unicode. A plain English file is already valid UTF-8. That is why UTF-8 won on the web.

ISCII solved a regional 8-bit need. Unicode solves a worldwide need. Both still sit on top of bits.

Remember the sizes. ASCII: 128. ISCII: 256. UTF-8: 1 to 4 bytes per character.

If two machines disagree on the table, the bits can be perfect and the letters still look wrong. Fix the table, not the bits.

  • Encoding: a table that pairs characters with numbers.
  • Code point: the Unicode number of one character.
  • ASCII: a 7-bit English code with 128 characters.
  • ISCII: an 8-bit code that keeps ASCII and adds Indian scripts.
  • Unicode: one number for every character, worldwide.
  • UTF-8: a variable-length store. ASCII uses one byte.

Cover the answers. Try each item on paper first.

Watch out: Do not mix a glyph with its code. The file stores a number. The font draws the shape.

⚡ Quick Check Flashcards5 questions
Question 1
Why does a computer need an encoding table?
Try answering before revealing!
Question 2
How many characters can 7-bit ASCII hold?
Try answering before revealing!
Question 3
What decimal ASCII code is capital C?
Try answering before revealing!
Question 4
What do ISCII codes 0 to 127 keep?
Try answering before revealing!
Question 5
A page is mostly English HTML plus one extra letter. Why is UTF-8 often smaller than UTF-32?
Try answering before revealing!

2.2 Number systems

1,995 words · 9 min read

A number system is a set of digits and place values. The same quantity can have many spellings.

We count with digits. Different systems use different digit sets.

A number system is a method for writing quantities.

The base, also called the radix, is how many digit symbols it has.

Everyday counting uses ten digits 0 to 9. Its base is 10.

Same value(45) base 10(101101) base 2(55) base 8(2D) base 16
The same quantity in four bases. The centre is the value. The boxes are spellings.

How to read the figure. The centre is one quantity. Each box is that quantity in another base. (45)₁₀, (101101)₂, (55)₈, and (2D)₁₆ match.

Check 45 quickly. Octal 55 is 5×8+5=455 \times 8 + 5 = 45. Hex 2D is 2×16+13=452 \times 16 + 13 = 45. Binary 101101 is 32+8+4+1=4532 + 8 + 4 + 1 = 45.

If those four checks agree, the wheel is consistent. Use the same check on any exam conversion.

These systems are positional. A digit’s value depends on its column.

In 555, the three fives are not equal. Place makes them 500, 50, and 5.

  • The rightmost digit is position 0. Weight is base to the power 0.
  • Move left. The power goes up by 1 each column.
  • Digits after a point use negative powers: −1, −2, −3.

The same rule works in every base. Only the column weights change.

In binary the weights are 1, 2, 4, 8. In octal they are 1, 8, 64. In hex they are 1, 16, 256.

A point is not only a decimal point. In binary it is a binary point. Columns to its right are halves, quarters, eighths.

Expand (462)₁₀ with powers of 10. Then add.

Goal. Write the place-value sum and the total.

Rule. Digit × base to the power of its position.

Steps. 2 is position 0. 6 is position 1. 4 is position 2.

Substitute. 4×1024 \times 10^2, 6×1016 \times 10^1, 2×1002 \times 10^0.

Arithmetic. 400+60+2=462400 + 60 + 2 = 462.

Result: Expansion: 4×102+6×101+2×1004 \times 10^2 + 6 \times 10^1 + 2 \times 10^0. Total 462.

We mark the base with a subscript. Write (75)₁₀ or (1011)₂.

SystemBaseDigitsExample
Decimal100 to 9(245)₁₀
Binary20, 1(1101)₂
Octal80 to 7(367)₈
Hexadecimal160 to 9 and A to F(4B2)₁₆

How to read the table. A digit must be smaller than the base. Octal never uses 8. Hex never uses G.

Never write a digit that is greater than or equal to the base.

(89)₈ is invalid because of 8. (210)₂ is invalid because of 2. (H1)₁₆ is invalid because of H.

A valid octal string uses only 0–7. A valid hex string uses 0–9 and A–F. Case of A–F does not change the value.

Binary uses 0 and 1. Each digit is a bit.

A transistor is on or off. Two states match two digits.

High voltage can mean 1. Low voltage can mean 0. That pair is reliable in silicon.

Column weights are powers of 2: 1, 2, 4, 8, 16, 32, and so on.

One extra bit doubles the set. Two bits give 4 values. Three bits give 8 values. Eight bits give 256 values.

Each extra bit doubles the count of values. The y values are 2 to the power n.

How to read the figure. The x-axis is bit count. The y-axis is how many values you can write. Each extra bit doubles the height.

Four bits give 16 values, 0 to 15. That is why one hex digit matches one nibble. Eight bits give 256 values, 0 to 255. That is one byte.

If a question gives n bits, the unsigned count is 2n2^n. The unsigned range is 0 to 2n−12^n - 1.

Long bit strings are hard to copy. Shorthand helps.

Octal is base 8. Digits are 0 to 7. 8=238 = 2^3, so three bits make one octal digit.

Hexadecimal is base 16. It needs sixteen symbols.

  • A is 10. B is 11. C is 12.
  • D is 13. E is 14. F is 15.

16=2416 = 2^4. Four bits make one hex digit. Four bits are a nibble. One byte is 8 bits, so two hex digits.

FF means both nibbles are 1111. That byte is 255. 00 means both nibbles are 0000.

You can convert hex to bits in your head once the 0–F table is known. That is faster than going through decimal.

A nibble is half a byte. Two nibbles make one byte. People write that byte as two hex digits.

Example: 1101 0110 is D6. You do not need decimal 214 on the way, unless the question asks for it.

DecimalBinary (4 bits)OctalHexadecimal
0000000
1000111
2001022
3001133
4010044
5010155
6011066
7011177
81000108
91001119
10101012A
11101113B
12110014C
13110115D
14111016E
15111117F

How to read the table. One hex symbol matches one 4-bit group. One octal digit matches the first eight binary rows.

Group bits. Do not convert through decimal when the bases are 2, 8, or 16.

Hex is short binary. That is why addresses and colour codes use it.

Memory is a row of cells. Each cell has an address. The CPU puts that address on the address bus.

A 16-bit address has 4 hex digits. 2A3F is easier than 16 bits.

A 32-bit address is eight hex digits. A 64-bit address is sixteen hex digits. Debuggers almost always show hex.

Screens mix red, green, and blue. Each channel uses 8 bits, values 0 to 255.

Three channels give 224=167772162^{24} = 16777216 colours. Web pages write #RRGGBB in hex.

Colour(R, G, B)Hex
Black(0, 0, 0)#000000
White(255, 255, 255)#FFFFFF
Red(255, 0, 0)#FF0000
Green(0, 255, 0)#00FF00
Blue(0, 0, 255)#0000FF
Yellow(255, 255, 0)#FFFF00
Cyan(0, 255, 255)#00FFFF
Magenta(255, 0, 255)#FF00FF

A MAC address is a 48-bit hardware id. That is 12 hex digits, often in pairs.

Pairs such as 3C:22:FB are still hex. The colons are only for reading. They are not extra digits.

#FF0000 is full red. #00FF00 is full green. #0000FF is full blue. Mixing two channels makes yellow, cyan, or magenta.

The hex address 2A3F is how many bits? Write the 16-bit form.

Goal. Bit length, then the bit string.

Rule. One hex digit is 4 bits.

Arithmetic. 4×4=164 \times 4 = 16 bits.

Steps. 2 → 0010. A → 1010. 3 → 0011. F → 1111.

Result: 16 bits: 0010 1010 0011 1111.

The colour #103050. Find decimal red, green, and blue, each from 0 to 255.

Goal. Three decimal channel values.

Rule. Each pair is two hex digits. Convert with powers of 16.

Arithmetic. Red 10: 1×16+0=161 \times 16 + 0 = 16.

Arithmetic. Green 30: 3×16+0=483 \times 16 + 0 = 48.

Arithmetic. Blue 50: 5×16+0=805 \times 16 + 0 = 80.

Result: Red 16, green 48, blue 80.

An unsigned integer uses every bit for size. It cannot be negative.

With 8 bits, unsigned values run from 0 to 28−1=2552^8 - 1 = 255.

A signed integer must show plus or minus. One common rule is two's complement.

In 8-bit two's complement the range is -128 to 127.

The same eight bits can mean two different numbers. The type tells you which reading to use.

8-bit patternUnsigned readingTwo's complement reading
0000000000
0000010155
01111111127127
10000000128-128
11111011251−5
11111111255−1

How to read the table. Look at the left bit. If you treat the pattern as unsigned, that bit is just 128. If you treat it as two's complement, that bit marks a negative value.

-8-4047100000000111Sign bit 1 means negative in two's complement.
Four-bit two's complement. The left end is the most negative value.

How to read the figure. Zero is the middle tick. Negative values sit to the left. In four bits, 1000 is −8. 0111 is 7. 0000 is 0.

The one's complement of a bit string flips every bit. 0 becomes 1. 1 becomes 0.

The two's complement is one's complement plus 1. CPUs use it for signed integers.

The left bit is the sign bit in two's complement. 1 means negative. 0 means zero or positive.

One's complement has two zeros: 00000000 and 11111111. Two's complement has one zero. That is why CPUs prefer two's complement.

To add a negative, add its two's complement. Keep the agreed width. Drop a carry that leaves the width.

To check −n in 8 bits, compute 28−n2^8 - n. Write that unsigned value in binary.

Write −5 in 8-bit two's complement.

Goal. An 8-bit pattern for −5.

Rule. Write +5. Flip the bits. Add 1.

Steps. +5 is 00000101.

Steps. One's complement is 11111010.

Arithmetic. Add 1: 11111011.

Check. Unsigned value of that pattern is 251. Then 256−5=251256 - 5 = 251.

Result: −5 is 11111011 in 8-bit two's complement.

Base tells you the digit set. Place tells you the weight. Binary matches hardware. Hex shortens binary by fours. Octal shortens binary by threes.

Unsigned means only size. Signed means a sign bit and a rule. Two's complement is the usual rule in processors.

The same pattern 11111011 is 251 if unsigned. It is −5 if 8-bit two's complement. The question must say which type.

Colours, addresses, and MAC codes are hex because people can read them. The machine still uses bits.

On the 4-bit line, 0111 is 7. 1000 is −8. There is no +8 in four-bit two's complement. The range is not symmetric.

Eight-bit two's complement stops at 127 on the plus side and −128 on the minus side. That extra negative comes from the single zero.

  • Base: how many digit symbols a system has.
  • Bit: one binary digit, 0 or 1.
  • Nibble: four bits. One hex digit.
  • Unsigned: a whole number that cannot be negative.
  • Two's complement: a signed encoding: flip bits, then add 1.

Pause here. Answer before you scroll to the answers.

Watch out: Do not read a signed pattern as unsigned. The same bits can mean 251 or −5.

⚡ Quick Check Flashcards5 questions
Question 1
What is the base of octal, and what is its largest digit?
Try answering before revealing!
Question 2
What decimal value is the hex digit E?
Try answering before revealing!
Question 3
How many bits does one hex digit stand for?
Try answering before revealing!
Question 4
In 8-bit unsigned form, what is the largest value?
Try answering before revealing!
Question 5
How do you form the 8-bit two's complement of a positive integer?
Try answering before revealing!

2.3 Conversions between number systems

2,617 words · 12 min read

You can change base by division, by place-value sums, or by grouping bits. Binary add uses carry.

People think in decimal. Machines use binary. Octal and hex shorten the bits.

You must convert both ways. Whole numbers first. Then fractions. Then binary arithmetic.

Pick a method, then stick to it. Division makes bits from decimal. Place values make decimal from bits. Grouping jumps between 2, 8, and 16.

Use repeated division by the new base.

  • Divide by the base. Keep the remainder.
  • Divide the quotient again.
  • Stop when the quotient is 0.
  • Read remainders from bottom to top.
Start with a positive decimal integerDivide by the new base. Keep the remainderIf the quotient is not 0, divide againRead remainders from bottom to top
Repeated division. Remainders become the new digits.

How to read the figure. Each box is one step. Remainders are the new digits. The last remainder is the left digit.

45 ÷ 2 → 22 remainder 122 ÷ 2 → 11 remainder 011 ÷ 2 → 5 remainder 1Keep going until quotient 0Later, read remainders upward.
A divide-and-remainder ladder. Each remainder is one binary digit.

How to read the figure. The top row starts at 45. The remainder 1 is the units bit. Keep dividing the quotient.

The first remainder is the least weight. It belongs on the right. The last remainder is the greatest weight. It belongs on the left.

Whole-number remainders are read upward. Fraction digits are read downward.

The first remainder is the units digit. That is why the reading order is bottom to top.

Convert 45 to binary by dividing by 2.

Goal. The binary form of 45.

Rule. Divide by 2. Remainders are 0 or 1. Read bottom to top.

Steps. 45 ÷ 2 = 22 remainder 1.

Steps. 22 ÷ 2 = 11 remainder 0.

Steps. 11 ÷ 2 = 5 remainder 1.

Steps. 5 ÷ 2 = 2 remainder 1.

Steps. 2 ÷ 2 = 1 remainder 0.

Steps. 1 ÷ 2 = 0 remainder 1. Stop.

Check. Remainders upward: 101101.

Result: (45)₁₀ = (101101)₂.

Value÷ 2QuotientRemainder
4545 ÷ 2221
2222 ÷ 2110
1111 ÷ 251
55 ÷ 221
22 ÷ 210
11 ÷ 201

How to read the table. The last row has quotient 0. Start there and move up the remainder column.

Convert 77 to binary by dividing by 2.

Goal. The binary form of 77.

Rule. Same division ladder. Remainders 0 or 1. Read upward.

Steps. 77 ÷ 2 = 38 remainder 1.

Steps. 38 ÷ 2 = 19 remainder 0.

Steps. 19 ÷ 2 = 9 remainder 1.

Steps. 9 ÷ 2 = 4 remainder 1.

Steps. 4 ÷ 2 = 2 remainder 0.

Steps. 2 ÷ 2 = 1 remainder 0.

Steps. 1 ÷ 2 = 0 remainder 1.

Check. Remainders upward: 1001101.

Result: (77)₁₀ = (1001101)₂.

Convert 125 to octal by dividing by 8.

Goal. The octal form of 125.

Rule. Divide by 8. Remainders are 0 to 7. Read bottom to top.

Steps. 125 ÷ 8 = 15 remainder 5.

Steps. 15 ÷ 8 = 1 remainder 7.

Steps. 1 ÷ 8 = 0 remainder 1.

Check. Remainders upward: 175.

Result: (125)₁₀ = (175)₈.

Value÷ 8QuotientRemainder
125125 ÷ 8155
1515 ÷ 817
11 ÷ 801

Convert 350 to octal by dividing by 8.

Goal. The octal form of 350.

Rule. Divide by 8. Read remainders upward.

Steps. 350 ÷ 8 = 43 remainder 6.

Steps. 43 ÷ 8 = 5 remainder 3.

Steps. 5 ÷ 8 = 0 remainder 5.

Check. Remainders upward: 536.

Result: (350)₁₀ = (536)₈.

Convert 188 to hexadecimal by dividing by 16.

Goal. The hex form of 188.

Rule. Divide by 16. Remainders 10 to 15 become A to F.

Steps. 188 ÷ 16 = 11 remainder 12. 12 is C.

Steps. 11 ÷ 16 = 0 remainder 11. 11 is B.

Check. Remainders upward: BC.

Result: (188)₁₀ = (BC)₁₆.

Value÷ 16RemainderHex digit
188188 ÷ 1612C
1111 ÷ 1611B

Convert 431 to hexadecimal by dividing by 16.

Goal. The hex form of 431.

Rule. Remainders 10 to 15 become A to F. Read upward.

Steps. 431 ÷ 16 = 26 remainder 15. 15 is F.

Steps. 26 ÷ 16 = 1 remainder 10. 10 is A.

Steps. 1 ÷ 16 = 0 remainder 1.

Check. Remainders upward: 1AF.

Result: (431)₁₀ = (1AF)₁₆.

Use positional expansion. Multiply each digit by the base to that power. Then add.

Convert (110101)₂ to decimal.

Goal. The decimal value.

Rule. Each 1 adds a power of 2.

Substitute. 1×25+1×24+0×23+1×22+0×21+1×201 \times 2^5 + 1 \times 2^4 + 0 \times 2^3 + 1 \times 2^2 + 0 \times 2^1 + 1 \times 2^0.

Arithmetic. 32+16+0+4+0+1=5332 + 16 + 0 + 4 + 0 + 1 = 53.

Result: (110101)₂ = (53)₁₀.

Convert (10111)₂ to decimal.

Goal. The decimal value of five bits.

Rule. Add the powers of 2 where the bit is 1.

Arithmetic. 16+0+4+2+1=2316 + 0 + 4 + 2 + 1 = 23.

Result: (10111)₂ = (23)₁₀.

Convert (372)₈ to decimal.

Goal. The decimal value of the octal number.

Rule. Digit × 8 to the power of its place.

Substitute. 3×82+7×81+2×803 \times 8^2 + 7 \times 8^1 + 2 \times 8^0.

Arithmetic. 3×64=1923 \times 64 = 192. 7×8=567 \times 8 = 56. 2×1=22 \times 1 = 2. Sum 250.

Result: (372)₈ = (250)₁₀.

Convert (146)₈ to decimal.

Goal. The decimal value of three octal digits.

Rule. Digit × powers of 8.

Arithmetic. 1×64+4×8+6=1021 \times 64 + 4 \times 8 + 6 = 102.

Result: (146)₈ = (102)₁₀.

Convert (2B6)₁₆ to decimal.

Goal. The decimal value. B means 11.

Rule. Digit × 16 to the power of its place.

Substitute. 2×162+11×161+6×1602 \times 16^2 + 11 \times 16^1 + 6 \times 16^0.

Arithmetic. 2×256=5122 \times 256 = 512. 11×16=17611 \times 16 = 176. Plus 6. Sum 694.

Result: (2B6)₁₆ = (694)₁₀.

Convert (E5)₁₆ to decimal.

Goal. The decimal value. E means 14.

Rule. 14×161+5×16014 \times 16^1 + 5 \times 16^0.

Arithmetic. 224+5=229224 + 5 = 229.

Result: (E5)₁₆ = (229)₁₀.

Binary, octal, and hex share powers of 2. You can group bits.

Octal: groups of 3, from the right. Hex: groups of 4, from the right. Pad the left group with zeros.

Why 3 and 4? Because 23=82^3 = 8 and 24=162^4 = 16. Each group is exactly one digit in the larger base.

Never drop zeros inside a group. Octal 2 is 010, not 10. Hex 3 is 0011, not 11.

Octal digit3 bitsHex digit4 bits
000000000
100181000
201091001
3011A1010
4100B1011
5101C1100
6110D1101
7111F1111

How to read the table. Match a group of bits to one digit. Do not skip zeros inside a group.

Convert (1101101)₂ to octal by grouping.

Goal. Octal digits from 3-bit groups.

Rule. Group from the right. Pad the left group.

Steps. 1 101 101. Pad: 001 101 101.

Steps. 001 is 1. 101 is 5. 101 is 5.

Check. (155)₈ = 109 in decimal. The binary value is the same.

Result: (1101101)₂ = (155)₈.

Convert (111010110)₂ to hexadecimal by grouping.

Goal. Hex digits from 4-bit groups.

Rule. Group from the right. Pad the left group.

Steps. 1 1101 0110. Pad: 0001 1101 0110.

Steps. 0001 is 1. 1101 is D. 0110 is 6.

Result: (111010110)₂ = (1D6)₁₆.

Octal to binary: replace each digit with 3 bits. Hex to binary: replace each digit with 4 bits.

Convert (634)₈ to binary.

Goal. Expand each octal digit to 3 bits.

Rule. Use the 0–7 bit groups. Keep inner zeros.

Steps. 6 is 110. 3 is 011. 4 is 100.

Steps. Join: 110 011 100.

Result: (634)₈ = (110011100)₂.

Convert (3A9)₁₆ to binary.

Goal. A bit string.

Rule. Each hex digit becomes 4 bits.

Steps. 3 is 0011. A is 1010. 9 is 1001.

Steps. Join: 0011 1010 1001.

Result: (3A9)₁₆ = (001110101001)₂.

A decimal fraction uses repeated multiplication by 2.

  • Multiply the fraction by 2.
  • The integer part is the next bit.
  • Keep the new fraction and multiply again.
  • Read those integer parts from top to bottom.

Convert 0.375 to binary.

Goal. Bits after the binary point.

Rule. Multiply by 2. Record the integer part. Read top to bottom.

Steps. 0.375 × 2 = 0.75. Bit 0. Fraction 0.75.

Steps. 0.75 × 2 = 1.50. Bit 1. Fraction 0.50.

Steps. 0.50 × 2 = 1.00. Bit 1. Fraction 0. Stop.

Check. 0×0.5+1×0.25+1×0.125=0.3750 \times 0.5 + 1 \times 0.25 + 1 \times 0.125 = 0.375.

Result: (0.375)₁₀ = (0.011)₂.

Convert 0.625 to binary.

Goal. Bits after the binary point.

Rule. Multiply by 2. Record the integer part. Read top to bottom.

Steps. 0.625 × 2 = 1.25. Bit 1. Fraction 0.25.

Steps. 0.25 × 2 = 0.50. Bit 0. Fraction 0.50.

Steps. 0.50 × 2 = 1.00. Bit 1. Fraction 0. Stop.

Check. 1×0.5+0×0.25+1×0.125=0.6251 \times 0.5 + 0 \times 0.25 + 1 \times 0.125 = 0.625.

Result: (0.625)₁₀ = (0.101)₂.

To read a binary fraction, use negative powers of 2. 2−1=0.52^{-1} = 0.5, 2−2=0.252^{-2} = 0.25, 2−3=0.1252^{-3} = 0.125.

Convert (0.101)₂ to decimal.

Goal. A decimal fraction.

Rule. Use negative powers of 2.

Substitute. 1×2−1+0×2−2+1×2−31 \times 2^{-1} + 0 \times 2^{-2} + 1 \times 2^{-3}.

Arithmetic. 0.5+0+0.125=0.6250.5 + 0 + 0.125 = 0.625.

Result: (0.101)₂ = (0.625)₁₀.

Some decimal fractions never end in binary. Then you stop after the asked number of bits, or when a loop repeats.

Convert (1011.11)₂ to decimal.

Goal. One mixed decimal value.

Rule. Split at the point. Integer powers, then negative powers.

Arithmetic. Integer 1011: 8+0+2+1=118 + 0 + 2 + 1 = 11.

Arithmetic. Fraction 11: 0.5+0.25=0.750.5 + 0.25 = 0.75.

Arithmetic. 11+0.75=11.7511 + 0.75 = 11.75.

Result: (1011.11)₂ = (11.75)₁₀.

For a fraction, group bits left to right from the point. Pad zeros on the far right, not the left.

Convert (10110.1011)₂ to octal and to hex by grouping.

Goal. One octal mixed number and one hex mixed number.

Rule. Integer groups go right to left. Fraction groups go left to right.

Steps. Integer 10110 → 010 110 → 2 6.

Steps. Fraction .1011 → 101 100 after one trailing zero → 5 4.

Steps. Integer 10110 → 0001 0110 → 1 6.

Steps. Fraction .1011 is already 4 bits → B.

Result: (10110.1011)₂ = (26.54)₈ = (16.B)₁₆.

The integer pad goes on the left. The fraction pad goes on the right. Mixing those pads is a common error.

Add column by column. Bits are 0 or 1. A sum of 2 writes 0 and carries 1.

carry11001011+ 0110= 10001
Add bits in one column. A carry of 1 moves left.

How to read the figure. The top row is carry. Add carry plus the two bits in that column. The bottom row is the sum.

Add (1011)₂ and (0110)₂. Show each carry.

Goal. The binary sum, then a decimal check.

Rule. 0+0=0. 1+0=1. 1+1=0 carry 1. 1+1+1=1 carry 1.

Steps. Units: 1+0=1. Carry 0.

Steps. Twos: 1+1=0 carry 1.

Steps. Fours: 0+1+carry 1=0 carry 1.

Steps. Eights: 1+0+carry 1=0 carry 1.

Steps. The last carry makes a new 1 on the left.

Check. 11 + 6 = 17. And (10001)₂ = 17.

Result: (1011)₂ + (0110)₂ = (10001)₂ = (17)₁₀.

Subtract (0101)₂ from (1101)₂.

Goal. The binary difference.

Rule. Subtract in each column. If needed, borrow 2 from the left.

Steps. Units: 1−1=0.

Steps. Twos: 0−0=0.

Steps. Fours: 1−1=0.

Steps. Eights: 1−0=1.

Check. 13 − 5 = 8. Binary 1000.

Result: (1101)₂ − (0101)₂ = (1000)₂ = (8)₁₀.

To subtract with two's complement, add the two's complement of the subtrahend. Keep the bit width.

Example in 8 bits: 13 − 5 is 13 + (−5). 13 is 00001101. −5 is 11111011.

Add in decimal first: 13+251=26413 + 251 = 264. Then drop the 256: 264 − 256 = 8.

The low 8 bits are 00001000, which is 8. That matches 13 − 5.

Overflow is different from carry. Carry is an extra bit leaving the column. Overflow means a signed result does not fit the width.

If you add two positives and the sign bit becomes 1, the signed result overflowed. The unsigned wrapping can still be valid.

Write carries above the columns. That keeps 1+1 from being forgotten. Then add the carry into the next column on the left.

Subtraction borrow means “take 2 from the left”.

A 0 that must subtract 1 becomes 10 in binary. That 10 means 2. The left bit then drops by 1.

bin, oct, and hex print prefixes 0b, 0o, and 0x. int(text, base) reads a string.

The prefix is not part of the exam subscript form. 0b101101 is the same bits as (101101)₂.

int needs the text without a prefix if you pass the base argument. int("BC", 16) reads B as 11 and C as 12.

Python hex() prints lowercase. Exams often accept A–F in either case. The value is the same.

Python
>>> bin(45)
0b101101
>>> oct(125)
0o175
>>> hex(188)
0xbc
>>> int("101101", 2)
45
>>> int("175", 8)
125
>>> int("BC", 16)
188

Decimal to another base: divide. Read remainders up. Decimal fraction to binary: multiply. Read bits down.

Another base to decimal: expand place values. Binary to octal or hex: group bits. Octal or hex to binary: expand digits.

Add bits with carry. Subtract bits with borrow. Or subtract by adding two's complement and keep the width.

Python can check your paper work. It does not replace the method. Exams still want the ladder and the groups.

If a conversion and its reverse disagree, stop. A remainder may have been read the wrong way. A group may have lost an inner zero.

Write every carry. Write every remainder. Then convert back once. That check catches most slips.

Keep subscripts on every exam answer. (1011) without a base is incomplete. State 2, 8, 10, or 16.

  • Repeated division: divide by the base; remainders are the new digits.
  • Positional expansion: digit times base to the power of its place, then add.
  • Bit grouping: 3 bits for octal, 4 bits for hex.
  • Carry: a 1 sent to the next column when two bits sum to 2.

Write a short answer. Then check the notes below.

Watch out: For whole numbers, read remainders up. For fractions, read bits down. Mixing the two directions fails.

⚡ Quick Check Flashcards5 questions
Question 1
When you convert a whole decimal number by division, which way do you read remainders?
Try answering before revealing!
Question 2
How many bits make one octal digit, and how many make one hex digit?
Try answering before revealing!
Question 3
Convert (0.11)₂ to decimal.
Try answering before revealing!
Question 4
Add (1001)₂ and (0011)₂. Give binary and decimal.
Try answering before revealing!
Question 5
In 4-bit two's complement, what decimal value is 1000?
Try answering before revealing!

Summary

174 words · 1 min read
  • A computer stores characters as numbers, then as bits. An encoding table names each number.
  • ASCII uses 7 bits and 128 codes. ISCII uses 8 bits and keeps ASCII in the lower half.
  • Unicode gives each character a code point. UTF-8 stores ASCII in one byte and grows for other letters.
  • Base is the digit count: 10, 2, 8, or 16. Hex uses A to F for 10 to 15.
  • Unsigned n-bit values run from 0 to 2ⁿ − 1. Two's complement stores negatives with a sign bit.
  • Decimal to another base: divide and read remainders upward. The last remainder is the left digit.
  • Any base to decimal: multiply digits by place weights and add.
  • Group 3 bits for octal and 4 bits for hex. Pad a short left group with zeros.
  • Fractions use multiply-by-2, reading bits downward. Group fractional bits left to right.
  • Binary add uses carry. Subtract by borrow, or add a two's complement and keep the width.
Explain each important word in your own words. Then say why the rule works.

Exercises

605 words · 3 min read

These questions are new. Show your thinking. For programs, write your answer before you run the code.

2.1 Why do digital computers use binary rather than decimal?

2.2 How many characters can 7-bit ASCII represent, and how many can 8-bit ISCII represent?

2.3 Why did a single worldwide character table replace many 8-bit tables?

2.4 A file has 400 English characters. Compare UTF-8 size and UTF-32 size in bytes.

2.5 State the base and the valid symbols for binary, octal, and hexadecimal.

2.6 Which of these are invalid, and why? (108)₈, (4G2)₁₆, (1012)₂.

2.7 Convert 54 to binary by repeated division by 2. Show remainders.

2.8 Convert 142 to octal by repeated division by 8.

2.9 Convert 250 to hexadecimal by repeated division by 16.

2.10 Convert (101011)₂ to decimal by place values.

2.11 Convert (453)₈ to decimal.

2.12 Convert (1E4)₁₆ to decimal. E is 14.

2.13 Convert (11010111)₂ to octal and to hex by grouping.

2.14 Convert (527)₈ to binary by expanding each digit to 3 bits.

2.15 Convert (A6C)₁₆ to binary by expanding each digit to 4 bits.

2.16 Convert 0.875 to binary by multiplying by 2.

2.17 Convert (110.101)₂ to decimal.

2.18 Add (1110)₂ and (0101)₂. Then write −6 in 8-bit two's complement.

2.19 Why is #00FF00 easier than 24 bits for a full green colour?

Answers

  1. 2.1: A transistor has two stable states, on and off. High and low voltage map to 1 and 0. Base 2 matches that hardware. Decimal would need ten reliable levels in the same circuit.
  2. 2.2: ASCII: 27=1282^7 = 128 (0 to 127). ISCII: 28=2562^8 = 256 (0 to 255). ISCII has twice as many codes.
  3. 2.3: The same number meant different letters in different 8-bit tables. A file then looked scrambled on another machine. Unicode gives one code point to each character, so the number does not clash.
  4. 2.4: UTF-8: 400×1=400400 \times 1 = 400 bytes. UTF-32: 400×4=1600400 \times 4 = 1600 bytes. UTF-32 is four times larger.
  5. 2.5: Binary: base 2, {0, 1}. Octal: base 8, {0–7}. Hexadecimal: base 16, {0–9, A–F}.
  6. 2.6: All three. Octal has no 8. Hex has no G. Binary has no 2.
  7. 2.7: 54÷2=27 rem 0; 27÷2=13 rem 1; 13÷2=6 rem 1; 6÷2=3 rem 0; 3÷2=1 rem 1; 1÷2=0 rem 1. Bottom to top: (110110)₂.
  8. 2.8: 142÷8=17 rem 6; 17÷8=2 rem 1; 2÷8=0 rem 2. Bottom to top: (216)₈.
  9. 2.9: 250÷16=15 rem 10 (A); 15÷16=0 rem 15 (F). Bottom to top: (FA)₁₆.
  10. 2.10: 1×25+0×24+1×23+0×22+1×21+1×20=431 \times 2^5 + 0 \times 2^4 + 1 \times 2^3 + 0 \times 2^2 + 1 \times 2^1 + 1 \times 2^0 = 43.
  11. 2.11: 4×82+5×81+3×80=256+40+3=2994 \times 8^2 + 5 \times 8^1 + 3 \times 8^0 = 256 + 40 + 3 = 299.
  12. 2.12: 1×162+14×161+4×160=256+224+4=4841 \times 16^2 + 14 \times 16^1 + 4 \times 16^0 = 256 + 224 + 4 = 484.
  13. 2.13: Octal groups of 3: 011 010 111 → (327)₈. Hex groups of 4: 1101 0111 → (D7)₁₆.
  14. 2.14: 5 → 101, 2 → 010, 7 → 111. So (527)₈ = (101010111)₂.
  15. 2.15: A → 1010, 6 → 0110, C → 1100. So (A6C)₁₆ = (101001101100)₂.
  16. 2.16: 0.875×2=1.75 (1); 0.75×2=1.50 (1); 0.50×2=1.00 (1). Top to bottom: (0.111)₂.
  17. 2.17: Integer 6. Fraction 0.5+0+0.125=0.6250.5 + 0 + 0.125 = 0.625. Total 6.625.
  18. 2.18: Sum: 1110 is 14 and 0101 is 5, so 14+5=19 = (10011)₂. Carry occurs when 1+1 meets in a column. −6: write 00000110, flip to 11111001, add 1 to get 11111010. Check: 250 unsigned is that pattern, and the sign bit is 1.
  19. 2.19: Each channel is one byte, so two hex digits. #00FF00 means red off, green 255, blue off. Twenty-four bits would be 00000000 11111111 00000000, which is hard to copy.
Up next
Continue to the next chapter