Discussion
If a is an unsigned integer variable whose value is 0x6db7, what is the value of -a?
Answer: D
Correct answer: 0x9249
For an unsigned type, unary minus does not produce a negative number - unsigned arithmetic is defined modulo \(2^{n}\), so the result is whatever value added to a gives zero: \[ -a \equiv 2^{16} - a \pmod{2^{16}}. \] With \( a = \mathtt{0x6db7} = 28087 \) and a 16-bit unsigned int, \[ 65536 - 28087 = 37449 = \mathtt{0x9249}. \] The same result comes out of the bitwise route, which is quicker by hand: negation is complement and add one. Complementing 0x6db7 gives 0x9248, and adding one gives 0x9249. That intermediate value is exactly the trap. This record was keyed to 0x9248 - the one’s complement, with the final increment forgotten - and the key has been corrected. Note also that on a 32-bit int the answer would be 0xffff9249; the two-byte width is what makes the short form right here. The hexadecimal prefixes throughout this item had been corrupted from 0x to hx and are restored.
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development