Unit 2 · Number Systems / 進位制

Start Here / 開始做題

  1. Identify the base first. 中文:先找出進位制的底數。
  2. Read every digit from left to right when converting to decimal. 中文:轉十進位時由左至右計算。
  3. For decimal to another base, record remainders from right to left. 中文:除法餘數要倒序讀取。
  4. Check your answer by converting it back once. 中文:可反向轉換檢查答案。

Unit Dashboard / 單元地圖

SystemBaseDigitsTypical use
Decimal / 十進位100–9everyday counting
Binary / 二進位20, 1computer bits
Octal / 八進位80–7compact binary grouping
Hexadecimal / 十六進位160–9, A–Fcolours, memory addresses

Core Idea / 核心概念

NumberExpanded formDecimal value
1011₂1×2³ + 0×2² + 1×2¹ + 1×2⁰11
157₈1×8² + 5×8¹ + 7×8⁰111
2F₁₆2×16¹ + 15×16⁰47

Fast Conversion / 快速轉換

DirectionMethodExample
binary → decimalmultiply running value by 2, then add digit1011₂ → 11
octal → decimalmultiply running value by 8, then add digit157₈ → 111
hexadecimal → decimalmultiply running value by 16, then add digit2F₁₆ → 47
decimal → base Bdivide by B repeatedly; read remainders upward26₁₀11010₂
binary ↔ octalgroup bits in threes from the right111101₂75₈
binary ↔ hexadecimalgroup bits in fours from the right11111111₂FF₁₆

C++ Syntax / C++ 語法

int remainder = n % base;  // last digit in the target base
n /= base;                 // remove the last digit
value = value * base + digit;  // read one digit into decimal value

Yellow Syntax Guide / 黃色語法重點

SyntaxMeaning
n % baseget the current rightmost digit
n /= basemove to the next digit
value = value * base + digitconvert a base-B string to decimal
ch - '0'turn character '0''9' into a number
ch - 'A' + 10turn 'A''F' into 10–15

Common Errors / 常見錯誤

MistakeFix
Writing 102₂Binary only uses 0 and 1.
Reading decimal-to-binary remainders top to bottomRead the remainders in reverse order.
Treating A as 1 in hexadecimalA means 10, then B=11 … F=15.
Forgetting N = 0Its representation is 0, not an empty line.

Examples / 例題

Example 2-1 · Binary to Decimal

ItemDetails
Problem Statement / 題目說明Given a binary number B, print its decimal value.
中文:把二進位數轉為十進位。
Input Format / 輸入格式One binary string B.
中文:一個二進位字串。
Output Format / 輸出格式Print one decimal integer.
中文:輸出一個十進位整數。
Constraints / 限制B contains only 0 and 1; length ≤ 30

Sample Input / 範例輸入

101101

Sample Output / 範例輸出

45

C++ focus / 語法重點:For each digit: value = value * 2 + digit.

Reference Answer / 參考答案

#include <iostream>
#include <string>
using namespace std;

int main() {
    string b;
    cin >> b;
    int value = 0;
    for (char ch : b) value = value * 2 + (ch - '0');
    cout << value << endl;
    return 0;
}

Example 2-2 · Decimal to Binary

ItemDetails
Problem Statement / 題目說明Given a non-negative decimal integer N, print its binary representation.
中文:把十進位整數轉為二進位。
Input Format / 輸入格式One integer N.
中文:一個非負整數。
Output Format / 輸出格式Print the binary representation without leading zeros.
中文:輸出沒有前導零的二進位。
Constraints / 限制0 ≤ N ≤ 10^9

Sample Input / 範例輸入

45

Sample Output / 範例輸出

101101

C++ focus / 語法重點:Repeatedly take n % 2, then reverse the digits.

Reference Answer / 參考答案

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() {
    int n;
    cin >> n;
    if (n == 0) { cout << 0 << endl; return 0; }
    string result;
    while (n > 0) { result += char('0' + n % 2); n /= 2; }
    reverse(result.begin(), result.end());
    cout << result << endl;
    return 0;
}

Example 2-3 · Octal to Decimal

ItemDetails
Problem Statement / 題目說明Given an octal number O, print its decimal value.
中文:把八進位數轉為十進位。
Input Format / 輸入格式One octal string O.
中文:一個八進位字串。
Output Format / 輸出格式Print one decimal integer.
中文:輸出一個十進位整數。
Constraints / 限制O contains digits 0 to 7; length ≤ 10

Sample Input / 範例輸入

157

Sample Output / 範例輸出

111

C++ focus / 語法重點:Change the base from 2 to 8: value = value * 8 + digit.

Reference Answer / 參考答案

#include <iostream>
#include <string>
using namespace std;

int main() {
    string o;
    cin >> o;
    int value = 0;
    for (char ch : o) value = value * 8 + (ch - '0');
    cout << value << endl;
    return 0;
}

Example 2-4 · Decimal to Octal

ItemDetails
Problem Statement / 題目說明Given a non-negative decimal integer N, print its octal representation.
中文:把十進位整數轉為八進位。
Input Format / 輸入格式One integer N.
中文:一個非負整數。
Output Format / 輸出格式Print the octal representation.
中文:輸出八進位表示。
Constraints / 限制0 ≤ N ≤ 10^9

Sample Input / 範例輸入

111

Sample Output / 範例輸出

157

C++ focus / 語法重點:Use n % 8 for the last octal digit and n /= 8 to continue.

Reference Answer / 參考答案

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() {
    int n;
    cin >> n;
    if (n == 0) { cout << 0 << endl; return 0; }
    string result;
    while (n > 0) { result += char('0' + n % 8); n /= 8; }
    reverse(result.begin(), result.end());
    cout << result << endl;
    return 0;
}

Example 2-5 · Hex Digit Value

ItemDetails
Problem Statement / 題目說明Given one hexadecimal digit (09 or AF), print its decimal value.
中文:把一個十六進位數字轉為十進位。
Input Format / 輸入格式One uppercase hexadecimal character.
中文:一個大寫十六進位字元。
Output Format / 輸出格式Print one decimal integer.
中文:輸出一個十進位整數。
Constraints / 限制Input is 09 or AF

Sample Input / 範例輸入

B

Sample Output / 範例輸出

11

C++ focus / 語法重點:Digits A to F represent 10 to 15.

Reference Answer / 參考答案

#include <iostream>
using namespace std;

int main() {
    char ch;
    cin >> ch;
    if (ch >= '0' && ch <= '9') cout << ch - '0' << endl;
    else cout << ch - 'A' + 10 << endl;
    return 0;
}

Example 2-6 · Hexadecimal to Decimal

ItemDetails
Problem Statement / 題目說明Given an uppercase hexadecimal number H, print its decimal value.
中文:把十六進位數轉為十進位。
Input Format / 輸入格式One uppercase hexadecimal string H.
中文:一個大寫十六進位字串。
Output Format / 輸出格式Print one decimal integer.
中文:輸出一個十進位整數。
Constraints / 限制length ≤ 7; characters are 09, AF

Sample Input / 範例輸入

2F

Sample Output / 範例輸出

47

C++ focus / 語法重點:For every digit, multiply the current answer by 16 first.

Reference Answer / 參考答案

#include <iostream>
#include <string>
using namespace std;

int main() {
    string h;
    cin >> h;
    int value = 0;
    for (char ch : h) {
        int digit = (ch <= '9') ? ch - '0' : ch - 'A' + 10;
        value = value * 16 + digit;
    }
    cout << value << endl;
    return 0;
}

Example 2-7 · Decimal to Hexadecimal

ItemDetails
Problem Statement / 題目說明Given a non-negative decimal integer N (at most 65535), print its uppercase hexadecimal representation.
中文:把十進位整數轉為大寫十六進位。
Input Format / 輸入格式One integer N.
中文:一個非負整數。
Output Format / 輸出格式Print the uppercase hexadecimal representation.
中文:輸出大寫十六進位表示。
Constraints / 限制0 ≤ N ≤ 65535

Sample Input / 範例輸入

47

Sample Output / 範例輸出

2F

C++ focus / 語法重點:Use n % 16; values 10–15 become AF.

Reference Answer / 參考答案

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() {
    int n;
    cin >> n;
    if (n == 0) { cout << 0 << endl; return 0; }
    string result, digits = "0123456789ABCDEF";
    while (n > 0) { result += digits[n % 16]; n /= 16; }
    reverse(result.begin(), result.end());
    cout << result << endl;
    return 0;
}

Example 2-8 · Binary Addition

ItemDetails
Problem Statement / 題目說明Given two binary numbers A and B, print their sum in decimal.
中文:輸入兩個二進位數,輸出十進位總和。
Input Format / 輸入格式Two binary strings A and B.
中文:兩個二進位字串。
Output Format / 輸出格式Print one decimal integer.
中文:輸出一個十進位整數。
Constraints / 限制Each length ≤ 20

Sample Input / 範例輸入

101 11

Sample Output / 範例輸出

8

C++ focus / 語法重點:Convert each binary number, then add the two decimal values.

Reference Answer / 參考答案

#include <iostream>
#include <string>
using namespace std;

int valueOf(string s) {
    int value = 0;
    for (char ch : s) value = value * 2 + (ch - '0');
    return value;
}

int main() {
    string a, b;
    cin >> a >> b;
    cout << valueOf(a) + valueOf(b) << endl;
    return 0;
}

Example 2-9 · Four Binary Bits to Hex

ItemDetails
Problem Statement / 題目說明Given exactly four binary bits, print the matching uppercase hexadecimal digit.
中文:把四位二進位轉為一個十六進位數字。
Input Format / 輸入格式One binary string of length 4.
中文:一個四位二進位字串。
Output Format / 輸出格式Print one uppercase hexadecimal digit.
中文:輸出一個大寫十六進位字元。
Constraints / 限制Input contains only 0 and 1

Sample Input / 範例輸入

1101

Sample Output / 範例輸出

D

C++ focus / 語法重點:Four bits have values 8, 4, 2, 1; use a hex digit string to look up the result.

Reference Answer / 參考答案

#include <iostream>
#include <string>
using namespace std;

int main() {
    string b, digits = "0123456789ABCDEF";
    cin >> b;
    int value = 0;
    for (char ch : b) value = value * 2 + (ch - '0');
    cout << digits[value] << endl;
    return 0;
}

Example 2-10 · Display in Three Bases

ItemDetails
Problem Statement / 題目說明Given a decimal integer N, print its value in binary, octal and uppercase hexadecimal, one per line.
中文:輸出同一數值的二、八、十六進位。
Input Format / 輸入格式One integer N.
中文:一個整數。
Output Format / 輸出格式Line 1: binary. Line 2: octal. Line 3: uppercase hexadecimal.
中文:依序輸出二、八、十六進位。
Constraints / 限制0 ≤ N ≤ 10^6

Sample Input / 範例輸入

26

Sample Output / 範例輸出

11010
32
1A

C++ focus / 語法重點:The stream manipulators oct and hex change output base; uppercase makes A–F uppercase.

Reference Answer / 參考答案

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    int n;
    cin >> n;
    int bit = 1;
    while (bit <= n / 2) bit *= 2;
    if (n == 0) cout << 0 << endl;
    else {
        for (; bit > 0; bit /= 2) cout << (n / bit) % 2;
        cout << endl;
    }
    cout << oct << n << endl;
    cout << uppercase << hex << n << endl;
    return 0;
}

ClassWork / 課後練習

Complete each task independently. Submit your C++ source file and one screenshot of successful output. 中文:獨立完成,提交程式碼及成功輸出截圖。

ClassWork 2-1 · Decimal to Binary

ItemDetails
Problem Statement / 題目說明Given a non-negative decimal integer N, print its binary representation.
中文:把十進位轉為二進位。
Input Format / 輸入格式One integer N.
中文:一個非負整數。
Output Format / 輸出格式Print one binary number.
中文:輸出一個二進位數。
Constraints / 限制0 ≤ N ≤ 10^9

Sample Input / 範例輸入

64

Sample Output / 範例輸出

1000000

C++ focus / 語法重點:Use repeated division by 2. Remember the special case N = 0.

ClassWork 2-2 · Decimal to Octal

ItemDetails
Problem Statement / 題目說明Given a non-negative decimal integer N, print its octal representation.
中文:把十進位轉為八進位。
Input Format / 輸入格式One integer N.
中文:一個非負整數。
Output Format / 輸出格式Print one octal number.
中文:輸出一個八進位數。
Constraints / 限制0 ≤ N ≤ 10^9

Sample Input / 範例輸入

83

Sample Output / 範例輸出

123

C++ focus / 語法重點:Repeatedly use % 8 and /= 8, then reverse the collected digits.

ClassWork 2-3 · Decimal to Hex

ItemDetails
Problem Statement / 題目說明Given a non-negative decimal integer N, print its uppercase hexadecimal representation.
中文:把十進位轉為大寫十六進位。
Input Format / 輸入格式One integer N.
中文:一個非負整數。
Output Format / 輸出格式Print one uppercase hexadecimal number.
中文:輸出一個大寫十六進位數。
Constraints / 限制0 ≤ N ≤ 10^6

Sample Input / 範例輸入

255

Sample Output / 範例輸出

FF

C++ focus / 語法重點:Prepare 0123456789ABCDEF for digit lookup.

ClassWork 2-4 · Binary to Decimal

ItemDetails
Problem Statement / 題目說明Given a binary string B, print its decimal value.
中文:把二進位轉為十進位。
Input Format / 輸入格式One binary string B.
中文:一個二進位字串。
Output Format / 輸出格式Print one decimal integer.
中文:輸出一個十進位整數。
Constraints / 限制length ≤ 30

Sample Input / 範例輸入

100000

Sample Output / 範例輸出

32

C++ focus / 語法重點:Read from left to right: answer = answer × 2 + current digit.

ClassWork 2-5 · Octal to Decimal

ItemDetails
Problem Statement / 題目說明Given an octal string O, print its decimal value.
中文:把八進位轉為十進位。
Input Format / 輸入格式One octal string O.
中文:一個八進位字串。
Output Format / 輸出格式Print one decimal integer.
中文:輸出一個十進位整數。
Constraints / 限制length ≤ 10

Sample Input / 範例輸入

70

Sample Output / 範例輸出

56

C++ focus / 語法重點:The base is 8, so multiply the running value by 8.

ClassWork 2-6 · Hex to Decimal

ItemDetails
Problem Statement / 題目說明Given an uppercase hexadecimal string H, print its decimal value.
中文:把十六進位轉為十進位。
Input Format / 輸入格式One uppercase hexadecimal string H.
中文:一個大寫十六進位字串。
Output Format / 輸出格式Print one decimal integer.
中文:輸出一個十進位整數。
Constraints / 限制length ≤ 7

Sample Input / 範例輸入

1A

Sample Output / 範例輸出

26

C++ focus / 語法重點:Convert AF into values 10–15 before adding.

ClassWork 2-7 · Count One Bits

ItemDetails
Problem Statement / 題目說明Given a non-negative decimal integer N, print how many 1 digits appear in its binary representation.
中文:計算二進位表示中 1 的數量。
Input Format / 輸入格式One integer N.
中文:一個非負整數。
Output Format / 輸出格式Print one integer.
中文:輸出一個整數。
Constraints / 限制0 ≤ N ≤ 10^9

Sample Input / 範例輸入

13

Sample Output / 範例輸出

3

C++ focus / 語法重點:When n % 2 == 1, increase the counter; then use n /= 2.

ClassWork 2-8 · Last Binary Digit

ItemDetails
Problem Statement / 題目說明Given a positive decimal integer N, print its last binary digit.
中文:輸出二進位的最後一位。
Input Format / 輸入格式One positive integer N.
中文:一個正整數。
Output Format / 輸出格式Print 0 or 1.
中文:輸出 0 或 1。
Constraints / 限制1 ≤ N ≤ 10^9

Sample Input / 範例輸入

18

Sample Output / 範例輸出

0

C++ focus / 語法重點:The last binary digit is the same as N % 2.

ClassWork 2-9 · Hex Digit to Binary

ItemDetails
Problem Statement / 題目說明Given one uppercase hexadecimal digit, print its 4-bit binary representation, including leading zeros.
中文:把一位十六進位轉成四位二進位,必須保留前導零。
Input Format / 輸入格式One character from 09 or AF.
中文:一個十六進位字元。
Output Format / 輸出格式Print exactly four binary digits.
中文:輸出剛好四位二進位。
Constraints / 限制Input is uppercase hexadecimal

Sample Input / 範例輸入

A

Sample Output / 範例輸出

1010

C++ focus / 語法重點:First convert the digit to 0–15, then print bit values 8, 4, 2, 1.

ClassWork 2-10 · Same Value Check

ItemDetails
Problem Statement / 題目說明Given a decimal integer D and a binary string B, print Same if they represent the same value; otherwise print Different.
中文:判斷十進位及二進位是否代表同一數值。
Input Format / 輸入格式Line 1: decimal integer D. Line 2: binary string B.
中文:十進位數,再輸入二進位數。
Output Format / 輸出格式Print Same or Different.
中文:輸出 Same 或 Different。
Constraints / 限制0 ≤ D ≤ 10^6; binary length ≤ 30

Sample Input / 範例輸入

10
1010

Sample Output / 範例輸出

Same

C++ focus / 語法重點:Convert B to decimal first, then compare with ==.

ClassWork 2-11 · Bytes in Hex

ItemDetails
Problem Statement / 題目說明Given a decimal number of bytes N (0 to 255), print it as exactly two uppercase hexadecimal digits.
中文:把一個位元組輸出為兩位大寫十六進位。
Input Format / 輸入格式One integer N.
中文:一個整數。
Output Format / 輸出格式Print exactly two hexadecimal digits.
中文:輸出剛好兩位十六進位。
Constraints / 限制0 ≤ N ≤ 255

Sample Input / 範例輸入

10

Sample Output / 範例輸出

0A

C++ focus / 語法重點:The first digit is N / 16; the second is N % 16.

ClassWork 2-12 · Binary Place Value

ItemDetails
Problem Statement / 題目說明Given an 8-bit binary string B, print the decimal contribution of its leftmost bit and its rightmost bit.
中文:輸出最左及最右位元所代表的十進位值。
Input Format / 輸入格式One binary string of length 8.
中文:一個 8 位二進位字串。
Output Format / 輸出格式Print two integers separated by one space.
中文:以一個空格分隔輸出兩個整數。
Constraints / 限制B contains only 0 and 1

Sample Input / 範例輸入

10000001

Sample Output / 範例輸出

128 1

C++ focus / 語法重點:The leftmost bit has place value 128; the rightmost bit has place value 1.

Next Step / 下一步

You should now be able to convert one value between decimal, binary, octal and hexadecimal. Continue to Unit 3 when you can explain why remainders are read in reverse order. 中文:能解釋為何餘數要倒序讀取後,再進入 Unit 3。

Built with LogoFlowershow