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 notbool isOppositeSign(int num1, int num2){if ((num1 ^ num2) < 0)return true;return false;}int main(){int num1 = -10, num2 = 5;if (isOppositeSign(num1, num2)){cout << "Opposite Sign\n";}elsecout << "Same Sign\n";return 0;}
No comments:
Post a Comment