IPA FE Subject A sample Question 1
A question IPA Japan published in 基本情報技術者試験 科目A サンプル問題 問1, translated into English. ITPEC is based on the same exams.
When negative numbers are represented in two’s complement, which of the following is the expression that obtains −n for an 8-bit positive binary number n? Here, + represents addition, OR represents the bitwise logical sum, and XOR represents the bitwise exclusive logical sum.
Explanation
XOR with all 1s, then add 1 — negating a two’s complement number means inverting every bit and adding 1.
To get −n in two’s complement, take the one’s complement of n (flip all 8 bits) and add 1.
XOR with 1 flips a bit and XOR with 0 keeps it, so n XOR 11111111 flips all eight bits. Adding 00000001 completes the negation.
Check with n = 00000011 (3): 00000011 XOR 11111111 = 11111100, plus 1 = 11111101, which is −3 in 8-bit two’s complement.
Answer (d)
Why not others:
- •(a) — OR with
10000000only forces the sign bit to 1: for 3 it gives10000011+ 1 =10000100, which is −124 - •(b) — OR with
11111110sets seven bits to 1 whatever n is: for 3 it gives11111111+11111111=11111110, which is −2 - •(c) — XOR with
10000000flips only the sign bit: for 3 it gives10000011+11111111=10000010, which is −126
Key rule: In two’s complement, −n = (bitwise NOT n) + 1, and a bitwise NOT is an XOR with all 1s.
Basic Theory · 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.