CLOSE
🛠️ Settings

Problem Statement

Determining whether two integers have opposite signs is a common requirement. Opposite signs mean that one integer is positive while the other is negative. The task is to create an algorithm that efficiently identifies whether two given integers have opposite signs or not.

Problem Understanding

Let's consider the problem with an example:

Suppose we have two integers, a and b. We need to determine if they have opposite signs. Mathematically, two numbers have opposite signs if the product of the two numbers is negative.

For example:

  • a = 5, b = -5: These two integers have opposite signs because their product, 5 * (-5), is negative (-25).
  • a = 2, b = 8: These two integers do not have opposite signs as their product, 2 * 8, is positive (16).

Solutions

Multiplication Solution:

The most straightforward solution involves multiplying the two integers and checking the sign of the product. If the product is negative, the numbers have opposite signs; otherwise, they do not.

bool haveOppositeSigns(int a, int b) {
    return (a * b) < 0;
}

Bitwise XOR Solution:

Another interesting solution utilizes bitwise XOR (^) to determine if the signs are different. When two numbers have opposite signs, their most significant bit (sign bit) will be different. The XOR operator returns 1 for bits that are different and 0 for bits that are the same. When XOR is applied to the sign bits of two integers, the result will be negative if the signs are opposite.

Example:

Here's a step-by-step breakdown of the bitwise XOR operation for this example:

  • Binary representation of num1 (5): 0000 0000 0000 0000 0000 0000 0000 0101
  • Binary representation of num2 (-8): 1111 1111 1111 1111 1111 1111 1111 1000

Now let's perform bitwise XOR:

0000 0000 0000 0000 0000 0000 0000 0101   (num1)
1111 1111 1111 1111 1111 1111 1111 1000   (num2)
-------------------------------------
1111 1111 1111 1111 1111 1111 1111 1101   (result)

The resulting binary representation 1111 1111 1111 1111 1111 1111 1111 1101 is the two's complement representation of -3. The most significant bit is 1, indicating a negative number.

Therefore, the condition (num1 ^ num2) < 0 evaluates to true, and the program should output “The integers have opposite signs.”

bool haveOppositeSigns(int a, int b) {
    return (a ^ b) < 0;
}

Code

Let's combine the solutions into a concise C++ code snippet.

#include <iostream>

bool haveOppositeSignsMultiplication(int a, int b) {
    return (a * b) < 0;
}

bool haveOppositeSignsBitwiseXOR(int a, int b) {
    return (a ^ b) < 0;
}

int main() {
    // Example usage
    int num1 = 5, num2 = -3;
    
    if (haveOppositeSignsMultiplication(num1, num2)) {
        std::cout << num1 << " and " << num2 << " have opposite signs.\n";
    } else {
        std::cout << num1 << " and " << num2 << " do not have opposite signs.\n";
    }

    if (haveOppositeSignsBitwiseXOR(num1, num2)) {
        std::cout << num1 << " and " << num2 << " have opposite signs.\n";
    } else {
        std::cout << num1 << " and " << num2 << " do not have opposite signs.\n";
    }

    return 0;
}