Check if two integers have opposite signs

Given two signed integers, write a function that returns true if the signs of given integers are different, otherwise false. 

The XOR of x and y will have the sign bit is 1 if and only if they have opposite sign.

Example:

Input:  num1=-10, num2=5
Output: Opposite Sign

Approach

C++

#include <bits/stdc++.h>
using namespace std;

//function to check if two numbers
//are of same sign or not
bool isOppositeSign(int num1int num2)
{
    if ((num1 ^ num2) < 0)
        return true;
    return false;
}
int main()
{
    int num1 = -10num2 = 5;

    if (isOppositeSign(num1num2))
    {
        cout << "Opposite Sign\n";
    }
    else
        cout << "Same Sign\n";

    return 0;
}


No comments:

Post a Comment