Detect If Two Integers Have Opposite Signs
In this question, input two numbers and detect if they have opposite signs. We solve using bitwise XOR operator
Table of Contents
In this lesson, we detect if two integers have different signs. Ignore the values of the inputs for this problem and focus on the signs.
Introduction
In this question, input two numbers and detect if they have opposite signs.
Problem statement
We need to write a program to detect if two input integers have opposite signs.
Input: a = 100, b = -1
Output: "Signs are opposite"
Input: a = 100, b = 501
Output: "Signs are not opposite."
Concept
We have already learned about representing/finding a positive/negative number in the NOT
lesson.
Two rules:
If the leading bit on the left side is 0, then it is a positive number.
If the leading bit on the left side is 1, then it is a negative number.
Solution
The XOR rule says the output will be 1
only when two input values are opposite (0 and 1, or 1 and 0).
The above concept clearly says if the leading left-MSB (left-most significant bit) is 1
, then it’s negative.
The following four examples will clearly explain these concepts.
- Consider two numbers whose left MSBs are both
1
, and the XOR of them is0
. - If two numbers both have left MSBs of
0
, then the XOR of them is0
. - Now, when the left MSB of two inputs is different, then the XOR yields to
1
, which is a negative number.
So, when we perform (x^y) < 0
, we get the correct answer.
Java
class DetectIfTwoIntegersHaveOppositeSigns {
private static String oppositeSigns(int x, int y) {
return (x ^ y) < 0 ? "Signs are opposite" : "Signs are not opposite";
}
public static void main(String[] args) {
int x = 100, y = -1;
System.out.println("For inputs " + x + ", " + y + " : " + oppositeSigns(x, y));
int z = 100, p = 501;
System.out.println("For inputs " + z + ", " + p + " : " + oppositeSigns(z, p));
}
}
Python
def oppositeSigns(x,y):
return "signs are opposite" if (x ^ y)< 0 else "signs are not opposite"
x=100
y=-1
print("For inputs ",x,"," ,y ,":" ,oppositeSigns(x,y))
z=100
p=501
print("For inputs ",z,"," ,p ,":" ,oppositeSigns(z,p))
JavaScript
const helper = (a, b) => {
return (a ^ b) < 0 ? 'Signs are opposite' : 'Signs are not opposite';
}
const x = 100, y = -1;
console.log (`For inputs ${x}, ${y} : ${helper (x, y)}`);
const z = 100, p = 501;
console.log (`For inputs ${z}, ${p} : ${helper (z, p)}`);
C++
#include <iostream>
#include <string>
using namespace std;
string oppositeSign(int x, int y) {
return (x ^ y) < 0 ? "Signs are opposite" : "Signs are not opposite";
}
int main() {
int x = 100, y = -1;
cout << "For inputs " << x << " , " << y << " : " << oppositeSign(x, y) << endl;
int z = 100, p = 501;
cout << "For inputs " << z << " , " << p << " : " << oppositeSign(z, p) << endl;
return 0;
}
TypeScript
export const helper = (a: number, b: number): string => {
return (a ^ b) < 0 ? 'Signs are opposite' : 'Signs are not opposite';
}
const x: number = 100;
const y: number = -1;
console.log(`For inputs ${x}, ${y} : ${helper(x, y)}`);
const z: number = 100;
const p: number = 501;
console.log(`For inputs ${z}, ${p} : ${helper(z, p)}`);
Complexity analysis
Time Complexity: We are not running a loop or scaling the inputs. The inputs never change. So, the operation takes a single unit of time, which is O(1)
.
Space Complexity: We didn’t create an extra memory for this. So, space complexity is O(1)
.
Gopi Gorantala Newsletter
Join the newsletter to receive the latest updates in your inbox.