Related Articles

# Detect if two integers have opposite signs

• Difficulty Level : Easy
• Last Updated : 12 Oct, 2021

Given two signed integers, write a function that returns true if the signs of given integers are different, otherwise false. For example, the function should return true -1 and +100, and should return false for -100 and -200. The function should not use any of the arithmetic operators.
Let the given integers be x and y. The sign bit is 1 in negative numbers, and 0 in positive numbers. The XOR of x and y will have the sign bit as 1 iff they have opposite sign. In other words, XOR of x and y will be negative number number iff x and y have opposite signs. The following code use this logic.

## C++

 `// C++ Program to Detect``// if two integers have opposite signs.``#include``using` `namespace` `std;` `bool` `oppositeSigns(``int` `x, ``int` `y)``{``    ``return` `((x ^ y) < 0);``}` `int` `main()``{``    ``int` `x = 100, y = -100;``    ``if` `(oppositeSigns(x, y) == ``true``)``    ``cout << ``"Signs are opposite"``;``    ``else``    ``cout << ``"Signs are not opposite"``;``    ``return` `0;``}` `// this code is contributed by shivanisinghss2110`

## C

 `// C++ Program to Detect ``// if two integers have opposite signs.``#include``#include``  ` `bool` `oppositeSigns(``int` `x, ``int` `y)``{``    ``return` `((x ^ y) < 0);``}``  ` `int` `main()``{``    ``int` `x = 100, y = -100;``    ``if` `(oppositeSigns(x, y) == ``true``)``       ``printf` `(``"Signs are opposite"``);``    ``else``      ``printf` `(``"Signs are not opposite"``);``    ``return` `0;``}`

## Java

 `// Java Program to Detect ``// if two integers have opposite signs.``  ` `class` `GFG {``  ` `    ``static` `boolean` `oppositeSigns(``int` `x, ``int` `y)``    ``{``        ``return` `((x ^ y) < ``0``);``    ``}``      ` `    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `x = ``100``, y = -``100``;``        ``if` `(oppositeSigns(x, y) == ``true``)``            ``System.out.println(``"Signs are opposite"``);``        ``else``            ``System.out.println(``"Signs are not opposite"``);``    ``}``}``  ` `// This code is contributed by prerna saini.`

## Python3

 `# Python3 Program to Detect ``# if two integers have ``# opposite signs.``def` `oppositeSigns(x, y):``    ``return` `((x ^ y) < ``0``);``  ` `x ``=` `100``y ``=` `1``  ` `if` `(oppositeSigns(x, y) ``=``=` `True``):``    ``print` `(``"Signs are opposite"``)``else``:``    ``print` `(``"Signs are not opposite"``)``  ` `# This article is contributed by Prerna Saini.`

## C#

 `// C# Program to Detect ``// if two integers have ``// opposite signs.``using` `System;``  ` `class` `GFG {``  ` `    ``// Function to detect signs``    ``static` `bool` `oppositeSigns(``int` `x, ``int` `y)``    ``{``        ``return` `((x ^ y) < 0);``    ``}``      ` `    ``// Driver Code``    ``public` `static` `void` `Main()``    ``{``        ``int` `x = 100, y = -100;``        ``if` `(oppositeSigns(x, y) == ``true``)``            ``Console.Write(``"Signs are opposite"``);``        ``else``            ``Console.Write(``"Signs are not opposite"``);``    ``}``}``  ` `// This code is contributed by Nitin Mittal.`

## PHP

 ``

## Javascript

 ``

Output:

`Signs are opposite`

Source: Detect if two integers have opposite signs
We can also solve this by using two comparison operators. See the following code.

## CPP

 `bool` `oppositeSigns(``int` `x, ``int` `y)``{``    ``return` `(x < 0)? (y >= 0): (y < 0);``}`

## Java

 `class` `GFG{``static` `boolean` `oppositeSigns(``int` `x, ``int` `y)``{``    ``return` `(x < ``0``)? (y >= ``0``): (y < ``0``);``}``}` `// This code contributed by umadevi9616`

## Python3

 `def` `oppositeSigns(x, y):` `    ``return` `(y >``=` `0``) ``if` `(x < ``0``) ``else` `(y < ``0``);` `# This code is contributed by shivanisingjss2110`

## C#

 `using` `System;``class` `GFG{``static` `boo oppositeSigns(``int` `x, ``int` `y)``{``    ``return` `(x < 0)? (y >= 0): (y < 0);``}``}` `// This code contributed by shivanisinghss2110`

## Javascript

 ``

The first method is more efficient. The first method uses a bitwise XOR and a comparison operator. The second method uses two comparison operators and a bitwise XOR operation is more efficient compared to a comparison operation.
We can also use following method. It doesn’t use any comparison operator. The method is suggested by Hongliang and improved by gaurav.

## CPP

 `bool` `oppositeSigns(``int` `x, ``int` `y)``{``    ``return` `((x ^ y) >> 31);``}`

## Java

 `import` `java.io.*;` `class` `GFG {``static` `boolean` `oppositeSigns(``int` `x, ``int` `y)``{``    ``return` `((x ^ y) >> ``31``);``}`` ` `    ``}` `// This code is contributed by shivanisinghss2110`

## Python3

 `def` `oppositeSigns(x, y):` `    ``return` `((x ^ y) >> ``31``)``  ` `# this code is contributed by shivanisinghss2110`

## C#

 `using` `System;` `class` `GFG {``static` `bool` `oppositeSigns(``int` `x, ``int` `y)``{``    ``return` `((x ^ y) >> 31);``}`` ` `    ``}` `// This code is contributed by shivanisinghss2110`

## Javascript

 ``

The function is written only for compilers where size of an integer is 32 bit. The expression basically checks sign of (x^y) using bitwise operator ‘>>’. As mentioned above, the sign bit for negative numbers is always 1. The sign bit is the leftmost bit in binary representation. So we need to checks whether the 32th bit (or leftmost bit) of x^y is 1 or not. We do it by right shifting the value of x^y by 31, so that the sign bit becomes the least significant bit. If sign bit is 1, then the value of (x^y)>>31 will be 1, otherwise 0.

Please write comments if you find any of the above codes/algorithms incorrect, or find other ways to solve the same problem.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up