2. Encoding Schemes and Number Systems
About this chapter
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
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.
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.
ASCII means American Standard Code for Information Interchange.
It used 7 bits at first. Seven bits give 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.
| Character | Decimal code | 7-bit binary |
|---|---|---|
| Space | 32 | 0100000 |
| 0 | 48 | 0110000 |
| 1 | 49 | 0110001 |
| 9 | 57 | 0111001 |
| A | 65 | 1000001 |
| B | 66 | 1000010 |
| C | 67 | 1000011 |
| Z | 90 | 1011010 |
| a | 97 | 1100001 |
| b | 98 | 1100010 |
| c | 99 | 1100011 |
| z | 122 | 1111010 |
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 job | Decimal code |
|---|---|
| Bell / alert | 7 |
| Line feed | 10 |
| Carriage return | 13 |
| Space (printable) | 32 |
How to read the table. Control codes sit below 32, except space. Space prints a blank. It still has a code.
| Scheme | Bits per code | Character count |
|---|---|---|
| ASCII | 7 | 128 |
| ISCII | 8 | 256 |
| UTF-32 | 32 | one code, four bytes |
How to read the table. More bits mean more codes. Count is 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: . That is H.
Arithmetic. Second: . 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 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 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 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.
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.
| Format | Bytes per character | Use |
|---|---|---|
| UTF-8 | 1 to 4 | ASCII uses 1 byte. Common on the web. |
| UTF-16 | 2 or 4 | Used inside some runtimes. |
| UTF-32 | always 4 | Easy 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: bytes.
Substitute. UTF-32: 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.
>>> ord("A")
65
>>> ord("a")
97
>>> chr(65)
A
>>> chr(97)
aThe 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.
2.2 Number systems
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.
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 . Hex 2D is . Binary 101101 is .
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. , , .
Arithmetic. .
Result: Expansion: . Total 462.
We mark the base with a subscript. Write (75)₁₀ or (1011)₂.
| System | Base | Digits | Example |
|---|---|---|---|
| Decimal | 10 | 0 to 9 | (245)₁₀ |
| Binary | 2 | 0, 1 | (1101)₂ |
| Octal | 8 | 0 to 7 | (367)₈ |
| Hexadecimal | 16 | 0 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.
A line graph of distinct values for 1 to 8 bits. The line rises from 2 to 256.
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 . The unsigned range is 0 to .
Long bit strings are hard to copy. Shorthand helps.
Octal is base 8. Digits are 0 to 7. , 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.
. 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.
| Decimal | Binary (4 bits) | Octal | Hexadecimal |
|---|---|---|---|
| 0 | 0000 | 0 | 0 |
| 1 | 0001 | 1 | 1 |
| 2 | 0010 | 2 | 2 |
| 3 | 0011 | 3 | 3 |
| 4 | 0100 | 4 | 4 |
| 5 | 0101 | 5 | 5 |
| 6 | 0110 | 6 | 6 |
| 7 | 0111 | 7 | 7 |
| 8 | 1000 | 10 | 8 |
| 9 | 1001 | 11 | 9 |
| 10 | 1010 | 12 | A |
| 11 | 1011 | 13 | B |
| 12 | 1100 | 14 | C |
| 13 | 1101 | 15 | D |
| 14 | 1110 | 16 | E |
| 15 | 1111 | 17 | F |
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.
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 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. 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: .
Arithmetic. Green 30: .
Arithmetic. Blue 50: .
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 .
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 pattern | Unsigned reading | Two's complement reading |
|---|---|---|
| 00000000 | 0 | 0 |
| 00000101 | 5 | 5 |
| 01111111 | 127 | 127 |
| 10000000 | 128 | -128 |
| 11111011 | 251 | −5 |
| 11111111 | 255 | −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.
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.
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 .
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.
2.3 Conversions between number systems
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.
How to read the figure. Each box is one step. Remainders are the new digits. The last remainder is the left 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.
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 | ÷ 2 | Quotient | Remainder |
|---|---|---|---|
| 45 | 45 ÷ 2 | 22 | 1 |
| 22 | 22 ÷ 2 | 11 | 0 |
| 11 | 11 ÷ 2 | 5 | 1 |
| 5 | 5 ÷ 2 | 2 | 1 |
| 2 | 2 ÷ 2 | 1 | 0 |
| 1 | 1 ÷ 2 | 0 | 1 |
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 | ÷ 8 | Quotient | Remainder |
|---|---|---|---|
| 125 | 125 ÷ 8 | 15 | 5 |
| 15 | 15 ÷ 8 | 1 | 7 |
| 1 | 1 ÷ 8 | 0 | 1 |
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 | ÷ 16 | Remainder | Hex digit |
|---|---|---|---|
| 188 | 188 ÷ 16 | 12 | C |
| 11 | 11 ÷ 16 | 11 | B |
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. .
Arithmetic. .
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. .
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. .
Arithmetic. . . . Sum 250.
Result: (372)₈ = (250)₁₀.
Convert (146)₈ to decimal.
Goal. The decimal value of three octal digits.
Rule. Digit × powers of 8.
Arithmetic. .
Result: (146)₈ = (102)₁₀.
Convert (2B6)₁₆ to decimal.
Goal. The decimal value. B means 11.
Rule. Digit × 16 to the power of its place.
Substitute. .
Arithmetic. . . Plus 6. Sum 694.
Result: (2B6)₁₆ = (694)₁₀.
Convert (E5)₁₆ to decimal.
Goal. The decimal value. E means 14.
Rule. .
Arithmetic. .
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 and . 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 digit | 3 bits | Hex digit | 4 bits |
|---|---|---|---|
| 0 | 000 | 0 | 0000 |
| 1 | 001 | 8 | 1000 |
| 2 | 010 | 9 | 1001 |
| 3 | 011 | A | 1010 |
| 4 | 100 | B | 1011 |
| 5 | 101 | C | 1100 |
| 6 | 110 | D | 1101 |
| 7 | 111 | F | 1111 |
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. .
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. .
Result: (0.625)₁₀ = (0.101)₂.
To read a binary fraction, use negative powers of 2. , , .
Convert (0.101)₂ to decimal.
Goal. A decimal fraction.
Rule. Use negative powers of 2.
Substitute. .
Arithmetic. .
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: .
Arithmetic. Fraction 11: .
Arithmetic. .
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.
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: . 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.
>>> bin(45)
0b101101
>>> oct(125)
0o175
>>> hex(188)
0xbc
>>> int("101101", 2)
45
>>> int("175", 8)
125
>>> int("BC", 16)
188Decimal 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.
Summary
- 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.
Exercises
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
- 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: ASCII: (0 to 127). ISCII: (0 to 255). ISCII has twice as many codes.
- 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.
- 2.4: UTF-8: bytes. UTF-32: bytes. UTF-32 is four times larger.
- 2.5: Binary: base 2, {0, 1}. Octal: base 8, {0–7}. Hexadecimal: base 16, {0–9, A–F}.
- 2.6: All three. Octal has no 8. Hex has no G. Binary has no 2.
- 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)₂.
- 2.8: 142÷8=17 rem 6; 17÷8=2 rem 1; 2÷8=0 rem 2. Bottom to top: (216)₈.
- 2.9: 250÷16=15 rem 10 (A); 15÷16=0 rem 15 (F). Bottom to top: (FA)₁₆.
- 2.10: .
- 2.11: .
- 2.12: .
- 2.13: Octal groups of 3: 011 010 111 → (327)₈. Hex groups of 4: 1101 0111 → (D7)₁₆.
- 2.14: 5 → 101, 2 → 010, 7 → 111. So (527)₈ = (101010111)₂.
- 2.15: A → 1010, 6 → 0110, C → 1100. So (A6C)₁₆ = (101001101100)₂.
- 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)₂.
- 2.17: Integer 6. Fraction . Total 6.625.
- 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.
- 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.