Open In App

Detect if two integers have opposite signs

Last Updated : 12 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given two integers a and b, the task is to determine whether they have opposite signs. Return true if the signs of the two numbers are different and false otherwise.

Examples:

Input: a = -5, b = 10
Output: true
Explanation: One number is negative and the other is positive, so their signs are different.

Input: a = 7, b = 15
Output: false
Explanation: Both numbers are positive, so their signs are the same.

Input: a = 99, b = -112
Output: true
Explanation: One number is negative and the other is positive, so their signs are different.

Using XOR Operator - O(1) Time and O(1) Space

The idea is that XOR (^) between two numbers with opposite signs will always have its leftmost (sign) bit set to 1. This means that if (a ^ b) < 0, then a and b have opposite signs.

C++
// C++ program to check if two integers have opposite signs
#include <bits/stdc++.h>
using namespace std;

// Function to check if two numbers have opposite signs
bool haveOppositeSigns(int a, int b) {
    
    // Using XOR to check if signs are different
    return (a ^ b) < 0;
}

int main() {
    
    int a = -5, b = 10;
    
    if (haveOppositeSigns(a, b)) {
        cout << "true" << endl;
    } else {
        cout << "false" << endl;
    }

    return 0;
}
Java
// Java program to check if two integers have opposite signs
class GfG {

    // Function to check if two numbers have opposite signs
    static boolean haveOppositeSigns(int a, int b) {
        
        // Using XOR to check if signs are different
        return (a ^ b) < 0;
    }

    public static void main(String[] args) {
        
        int a = -5, b = 10;
        
        if (haveOppositeSigns(a, b)) {
            System.out.println("true");
        } else {
            System.out.println("false");
        }
    }
}
Python
# Python program to check if two integers have opposite signs

# Function to check if two numbers have opposite signs
def haveOppositeSigns(a, b):
    
    # Using XOR to check if signs are different
    return (a ^ b) < 0

if __name__ == "__main__":
    
    a, b = -5, 10
    
    if haveOppositeSigns(a, b):
        print("true")
    else:
        print("false")
C#
// C# program to check if two integers have opposite signs
using System;

class GfG {

    // Function to check if two numbers have opposite signs
    static bool haveOppositeSigns(int a, int b) {
        
        // Using XOR to check if signs are different
        return (a ^ b) < 0;
    }

    public static void Main() {
        
        int a = -5, b = 10;
        
        if (haveOppositeSigns(a, b)) {
            Console.WriteLine("true");
        } else {
            Console.WriteLine("false");
        }
    }
}
JavaScript
// JavaScript program to check if two integers have opposite signs

// Function to check if two numbers have opposite signs
function haveOppositeSigns(a, b) {
    
    // Using XOR to check if signs are different
    return (a ^ b) < 0;
}

let a = -5, b = 10;

if (haveOppositeSigns(a, b)) {
    console.log("true");
} else {
    console.log("false");
}

Output
true

Using Relational Operator - O(1) Time and O(1) Space

The idea is to use a ternary condition to explicitly check whether one number is negative and the other is non-negative. If a is negative, we check if b is non-negative; otherwise, we check if b is negative.

C++
// C++ program to check if two integers have opposite signs
#include <bits/stdc++.h>
using namespace std;

// Function to check if two numbers have opposite signs
bool haveOppositeSigns(int a, int b) {
    
    // Checking signs using relational operator
    return (a < 0) ? (b >= 0) : (b < 0);
}

int main() {
    
    int a = -5, b = 10;
    
    if (haveOppositeSigns(a, b)) {
        cout << "true" << endl;
    } else {
        cout << "false" << endl;
    }

    return 0;
}
Java
// Java program to check if two integers have opposite signs
class GfG {

    // Function to check if two numbers have opposite signs
    static boolean haveOppositeSigns(int a, int b) {
        
        // Checking signs using relational operator
        return (a < 0) ? (b >= 0) : (b < 0);
    }

    public static void main(String[] args) {
        
        int a = -5, b = 10;
        
        if (haveOppositeSigns(a, b)) {
            System.out.println("true");
        } else {
            System.out.println("false");
        }
    }
}
Python
# Function to check if two numbers have opposite signs
def haveOppositeSigns(a, b):
    
    # Checking signs using relational operator
    return (a < 0) if (b >= 0) else (b < 0)

if __name__ == "__main__":
    
    a, b = -5, 10
    
    if haveOppositeSigns(a, b):
        print("true")
    else:
        print("false")
C#
// C# program to check if two integers have opposite signs
using System;

class GfG {

    // Function to check if two numbers have opposite signs
    static bool haveOppositeSigns(int a, int b) {
        
        // Checking signs using relational operator
        return (a < 0) ? (b >= 0) : (b < 0);
    }

    public static void Main() {
        
        int a = -5, b = 10;
        
        if (haveOppositeSigns(a, b)) {
            Console.WriteLine("true");
        } else {
            Console.WriteLine("false");
        }
    }
}
JavaScript
// Function to check if two numbers have opposite signs
function haveOppositeSigns(a, b) {
    
    // Checking signs using relational operator
    return (a < 0) ? (b >= 0) : (b < 0);
}

let a = -5, b = 10;

if (haveOppositeSigns(a, b)) {
    console.log("true");
} else {
    console.log("false");
}

Output
true

Using XOR + Right Shift Operator - O(1) Time and O(1) Space

The idea is to first compute a ^ b, which results in a negative number if a and b have opposite signs. Then, we right shift it by 31 (for 32-bit integers) to check the sign bit. If the result is 1, it means the numbers have opposite signs; otherwise, they do not.

C++
// C++ program to check if two integers have opposite signs
#include <bits/stdc++.h>
using namespace std;

// Function to check if two numbers have opposite signs
bool haveOppositeSigns(int a, int b) {
    
    // Checking signs using bitwise XOR and right shift
    return ((a ^ b) >> 31);
}

int main() {
    
    int a = -5, b = 10;
    
    if (haveOppositeSigns(a, b)) {
        cout << "true" << endl;
    } else {
        cout << "false" << endl;
    }

    return 0;
}
Java
// Java program to check if two integers have opposite signs
class GfG {

    // Function to check if two numbers have opposite signs
    static boolean haveOppositeSigns(int a, int b) {
        
        // Checking signs using bitwise XOR and right shift
        return ((a ^ b) >> 31) != 0;
    }

    public static void main(String[] args) {
        
        int a = -5, b = 10;
        
        if (haveOppositeSigns(a, b)) {
            System.out.println("true");
        } else {
            System.out.println("false");
        }
    }
}
Python
# Python Program to check if two numbers have 
# opposite signs
def haveOppositeSigns(a, b):
    
    # Checking signs using bitwise XOR and right shift
    return ((a ^ b) >> 31) != 0

if __name__ == "__main__":
    
    a, b = -5, 10
    
    if haveOppositeSigns(a, b):
        print("true")
    else:
        print("false")
C#
// C# program to check if two integers have opposite signs
using System;

class GfG {

    // Function to check if two numbers have opposite signs
    static bool haveOppositeSigns(int a, int b) {
        
        // Checking signs using bitwise XOR and right shift
        return ((a ^ b) >> 31) != 0;
    }

    public static void Main() {
        
        int a = -5, b = 10;
        
        if (haveOppositeSigns(a, b)) {
            Console.WriteLine("true");
        } else {
            Console.WriteLine("false");
        }
    }
}
JavaScript
// Javascript Program  to check if two numbers 
// have opposite signs
function haveOppositeSigns(a, b) {
    
    // Checking signs using bitwise XOR and right shift
    return ((a ^ b) >> 31) !== 0;
}

let a = -5, b = 10;

if (haveOppositeSigns(a, b)) {
    console.log("true");
} else {
    console.log("false");
}

Output
true

Using Multiplication Operator - O(1) Time and O(1) Space

The idea is to use multiplication to check the signs of two integers. The product of two numbers will be negative if and only if one number is positive and the other is negative. By casting the integers to a larger data type (long long, long, BigInt, etc.), we avoid overflow issues. If the product is less than zero, it means the numbers have opposite signs; otherwise, they have the same sign.

C++
// C++ program to check if two integers have opposite signs
#include <bits/stdc++.h>
using namespace std;

// Function to check if two numbers have opposite signs
bool haveOppositeSigns(int a, int b) {
    
    // Checking signs using multiplication and type casting
    long long product = (long long) a * b;
    return (product < 0);
}

int main() {
    
    int a = -5, b = 10;
    
    if (haveOppositeSigns(a, b)) {
        cout << "true" << endl;
    } else {
        cout << "false" << endl;
    }

    return 0;
}
Java
// Java program to check if two integers have opposite signs
class GfG {

    // Function to check if two numbers have opposite signs
    static boolean haveOppositeSigns(int a, int b) {
        
        // Checking signs using multiplication and type casting
        long long product = (long long) a * b;
        return (product < 0);
    }

    public static void main(String[] args) {
        
        int a = -5, b = 10;
        
        if (haveOppositeSigns(a, b)) {
            System.out.println("true");
        } else {
            System.out.println("false");
        }
    }
}
Python
# Python program to check if two integers
# have opposite signs

def haveOppositeSigns(a, b):
    
    # Checking signs using multiplication and type casting
    product = int(a) * int(b)
    return product < 0

if __name__ == "__main__":
    
    a, b = -5, 10
    
    if haveOppositeSigns(a, b):
        print("true")
    else:
        print("false")
C#
// C# program to check if two integers have opposite signs
using System;

class GfG {

    // Function to check if two numbers have opposite signs
    static bool haveOppositeSigns(int a, int b) {
        
        // Checking signs using multiplication and type casting
        long product = (long) a * b;
        return (product < 0);
    }

    public static void Main() {
        
        int a = -5, b = 10;
        
        if (haveOppositeSigns(a, b)) {
            Console.WriteLine("true");
        } else {
            Console.WriteLine("false");
        }
    }
}
JavaScript
// Javscript program to check if two 
// integers have opposite signs

function haveOppositeSigns(a, b) {
    
    // Checking signs using multiplication and type casting
    let product = BigInt(a) * BigInt(b);
    return product < 0;
}

let a = -5, b = 10;

if (haveOppositeSigns(a, b)) {
    console.log("true");
} else {
    console.log("false");
}

Output
true

Next Article
Article Tags :
Practice Tags :

Similar Reads