ProgrammingModerate
What value is -x when x is 7 in two's complement, expressed as ~x + 1?
- A. -8
- B. 14
- C. -6
- D. -7
Correct answer: D. -7
Two's complement negation flips every bit and adds 1: ~7 is -8, so ~7 + 1 = -7. The tempting -8 stops after the bit flip, which is always one less than the true negative.
More C Programming questions
- What is the value of (unsigned short)(65535 + 1) on a platform where unsigned short is 16 bits?
- Which bit trick isolates the lowest set bit of an unsigned n?
- What is the parity of the number 0b1101011 (1 for odd count of set bits)?
- What is (-8) >> 1 in C if the platform uses arithmetic right shift for signed values?
- To multiply x by 2^k where x is an unsigned 32-bit value, which code is correct?
- A program uses a modulus of 10^9 + 7 and computes (a - b + MOD) % MOD for a and b in [0, MOD). Why add MOD before the %?