> ## Content Index
> Fetch the complete content index at: https://www.javahandbook.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Detect If Two Integers Have Opposite Signs
- URL: https://www.javahandbook.com/bitmask/detect-if-two-integers-have-opposite-signs/
- Published: 2025-08-07T03:25:12.000Z
- Updated: 2025-08-07T03:25:12.000Z
- Description: In this question, input two numbers and detect if they have opposite signs. We solve using bitwise XOR operator
- Author: Gopi Gorantala
- Tags: XOR Operator, #bitmask, #docs

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.

```json
Input: a = 100, b = -1 

Output: "Signs are opposite"
```

```json
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 is `0`.
- If two numbers both have left MSBs of `0`, then the XOR of them is `0`.
- 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

```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

```py
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

```js
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++

```cpp
#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

```ts
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)`.