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));
}- A. 0 1 0
- B. 1 1 0
- C. 0 0 1
- 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
- What is printed? (Integer overflow on multiplication.) c #include <limits.h> int main(void) { int a = INTMAX; int b = 2; int result = a b…
- What happens? c int arr[3] = {1, 2, 3}; int p = arr; p += 10; p = 99; printf("done");
- What happens? c int compare(const void a, const void b) { return (int )a - (int )b; } int main(void) { int arr[] = {5, 2, 8, 1, 9}…
- Assume 32-bit int. What is printed? c unsigned int a = 0; a--; printf("%u", a);
- Assume 32-bit int. What is printed? c int flags = 0b1010; int mask = 0b0110; printf("%d %d", flags & mask, flags | mask);
- Assume 32-bit int. What is printed? c int x = 100; int y = x << 1; int z = x >> 1; printf("%d %d", y, z);