IPA FE Subject B sample Question 16
A question IPA Japan published in 基本情報技術者試験 科目B サンプル問題 問16, translated into English. ITPEC is based on the same exams.
From the answer group below, select the correct answer to be inserted into blank in the program. The same answer goes into both blanks blank. Here, the array index starts at 1.
The program converts a Unicode code point to a UTF-8 encoding. In this question, “(16)” after a numerical value indicates a hexadecimal value.
Each Unicode character is given an integer value that is known as a code point. UTF-8 is one of the methods of encoding Unicode characters, and a character with a code point that ranges from 800(16) to FFFF(16) is encoded to a 3-byte value as below.
Let the bit pattern with a 3-byte length be 1110xxxx 10xxxxxx 10xxxxxx. The code point represented in binary is stored right-justified in the underlined “x” positions in the bit pattern, and 0 is stored in any leftover “x” positions. This 3-byte value is the UTF-8 encoding.
For example, when 3042(16), the code point of the hiragana character “あ”, is represented in binary, it is 11000001000010. When this is stored right-justified in the “x” positions in the bit pattern shown above, it is 1110xx11 10000001 10000010. When 0 is stored in the two leftover “x” positions, the UTF-8 encoding of “あ”, 11100011 10000001 10000010, is obtained.
The function encode converts a Unicode code point that is passed as an argument to UTF-8 encoding, and returns an integer-type array that stores it one byte per element in order from the start. It is assumed that only an integer value that ranges from 800(16) to FFFF(16) is passed to encode as an argument.
Answer group
Explanation
64 — each byte after the first carries 6 bits of the code point, so the loop takes them off the right end with 26 = 64.
In 1110xxxx 10xxxxxx 10xxxxxx the last two bytes hold six x bits each and the first byte four. Going from i = 3 down to 1, cp mod 64 is the low 6 bits, added to the byte's base (128 = 10000000, 224 = 11100000), and the quotient of cp ÷ 64 shifts the rest down for the next byte.
For “あ”, 3042(16) = 12354: 12354 mod 64 = 2, byte 130; 193 mod 64 = 1, byte 129; 3 mod 64 = 3, byte 227. The result {227, 129, 130} is 11100011 10000001 10000010, as in the example, and 64 gives real UTF-8 for every code point from 800(16) to FFFF(16).
Answer (h)
Why not others:
- •(f) 6 — divides by the number of bits instead of by 26; “あ” gives
{225, 129, 128} - •(g) 16 — 24 fits the first byte's 4 bits, not the others' 6; “あ” gives
{224, 132, 130} - •(i) 256 — a whole byte, but
10takes 2 bits of each; “あ” gives{224, 176, 194} - •(e) 2 — moves one bit per byte; “あ” gives
{224, 129, 128} - •(a), (b), (c) and (d) — change the divisor with
i, though both bytes filled first hold 6 bits; none gives{227, 129, 130}for “あ”
Key rule: To cut a number into bit fields from the right, take mod 2k for the low k bits and the quotient by 2k for the rest.
Math & Numbers · AI-written, may contain errors
This question comes from the past questions IPA Japan publishes, translated into English by ITPEC Practice. ITPEC Practice is an independent study tool and is not affiliated with IPA or ITPEC. See the original paper (PDF) or Report an issue.