Machine-translated by Google. Questions are authored in English.
Top

Discussion

If a is an unsigned integer variable whose value is 0x6db7, what is the value of -a?

  • A.0 1100001
  • B.0x001
  • C.0x248
  • D.0x9249

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.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.