ProgrammingHardHCL · Tech Mahindra pattern

What is printed? (Check if two integers have opposite signs, without using multiplication.)

What is printed? (Check if two integers have opposite signs, without using multiplication.)
int oppositeSigns(int a, int b) {
    return (a ^ b) < 0;
}
int main(void) {
    printf("%d %d %d", oppositeSigns(5, -3), oppositeSigns(-5, -3), oppositeSigns(5, 3));
}
  1. A. 0 1 0
  2. B. 1 1 0
  3. C. 0 0 1
  4. D. 1 0 0

Correct answer: D. 1 0 0

XOR-ing two numbers with opposite signs sets the result's sign bit (since their sign bits differ, XOR gives 1 there), making the XOR negative: 5^(-3) is negative -> true (1). Two negatives or two positives have matching sign bits, so XOR's sign bit is 0, giving a non-negative result -> false (0) for both other cases.

More C Programming questions

Exams that ask this topic