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 if they have opposite sign. In other words, XOR of x and y will be negative number if x and y have opposite signs. The following code use this logic. 
C++
| 
#include<iostream>
 usingnamespacestd;
   booloppositeSigns(intx, inty)
 {
     return((x ^ y) < 0);
 }
   intmain()
 {
     intx = 100, y = -100;
     if(oppositeSigns(x, y) == true)
     cout << "Signs are opposite";
     else
     cout << "Signs are not opposite";
     return0;
 }
   | 
 
 
 
 
C
| 
#include<stdbool.h>
 #include<stdio.h>
   
 booloppositeSigns(intx, inty)
 {
     return((x ^ y) < 0);
 }
   
 intmain()
 {
     intx = 100, y = -100;
     if(oppositeSigns(x, y) == true)
        printf("Signs are opposite");
     else
       printf("Signs are not opposite");
     return0;
 }
 | 
 
 
 
 
Java
| 
  
 classGFG {
   
     staticbooleanoppositeSigns(intx, inty)
     {
         return((x ^ y) < 0);
     }
       
     publicstaticvoidmain(String[] args)
     {
         intx = 100, y = -100;
         if(oppositeSigns(x, y) == true)
             System.out.println("Signs are opposite");
         else
             System.out.println("Signs are not opposite");
     }
 }
   
 | 
 
 
 
 
Python3
| 
defoppositeSigns(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")
   
 | 
 
 
 
 
C#
| 
usingSystem;
   
 classGFG {
   
     
     staticbooloppositeSigns(intx, inty)
     {
         return((x ^ y) < 0);
     }
       
     
     publicstaticvoidMain()
     {
         intx = 100, y = -100;
         if(oppositeSigns(x, y) == true)
             Console.Write("Signs are opposite");
         else
             Console.Write("Signs are not opposite");
     }
 }
   
 | 
 
 
 
 
PHP
| 
<?php
   functionoppositeSigns($x, $y)
 {
     return(($x^ $y) < 0);
 }
       
     $x= 100;
     $y= -100;
     if(oppositeSigns($x, $y) == true)
     echo("Signs are opposite");
     else
     echo("Signs are not opposite");
     
 ?>
 | 
 
 
 
 
Javascript
| 
<script>
     functionoppositeSigns(x, y)
     {
         return((x ^ y) < 0);
     }
         let x = 100, y = -100;
     if(oppositeSigns(x, y) == true)
          document.write("Signs are opposite");
     else
          document.write("Signs are not opposite");
   </script>
 | 
 
 
 
 
 
Output
Signs are opposite
 
Time Complexity: O(1)
Auxiliary Space: O(1)
Source: Detect if two integers have opposite signs
We can also solve this by using two comparison operators. See the following code. 
CPP
| 
booloppositeSigns(intx, inty)
 {
     return(x < 0)? (y >= 0): (y < 0);
 }
 | 
 
 
 
 
C
| 
  #include <stdio.h>
 #include<stdbool.h>
   booloppositeSigns(intx, inty)
 {
     return(x < 0)? (y >= 0): (y < 0);
 }
   | 
 
 
 
 
Java
| 
classGFG{
 staticbooleanoppositeSigns(intx, inty)
 {
     return(x < 0)? (y >= 0): (y < 0);
 }
 }
   | 
 
 
 
 
Python3
| 
defoppositeSigns(x, y):
       return(y >=0) if(x < 0) else(y < 0);
   | 
 
 
 
 
C#
| 
usingSystem;
 classGFG{
 staticboo oppositeSigns(intx, inty)
 {
     return(x < 0)? (y >= 0): (y < 0);
 }
 }
   | 
 
 
 
 
Javascript
| 
<script>
 functionoppositeSigns(x, y)
 {
     return(x < 0)? (y >= 0): (y < 0);
 }
     </script>
 | 
 
 
 
 
 
 
Time Complexity: O(1)
Auxiliary Space: O(1)
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
| 
booloppositeSigns(intx, inty)
 {
     return((x ^ y) >> 31);
 }
 | 
 
 
 
 
C
| 
  #include <stdio.h>
 #include<stdbool.h>
   booloppositeSigns(intx, inty)
 {
     return((x ^ y) >> 31);
 }
   | 
 
 
 
 
Java
| 
importjava.io.*;
   classGFG {
 staticbooleanoppositeSigns(intx, inty)
 {
     return((x ^ y) >> 31);
 }
  
     }
   | 
 
 
 
 
Python3
| 
defoppositeSigns(x, y):
       return((x ^ y) >> 31)
   
 | 
 
 
 
 
C#
| 
usingSystem;
   classGFG {
 staticbooloppositeSigns(intx, inty)
 {
     return((x ^ y) >> 31);
 }
  
     }
   | 
 
 
 
 
Javascript
| 
<script>
 functionoppositeSigns(x, y)
 {
     return((x ^ y) >> 31);
 }
  
   </script>
 | 
 
 
 
 
 
 
Time Complexity: O(1)
Auxiliary Space: O(1)
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. 
C++
| 
#include <iostream>
 usingnamespacestd;
   booloppositeSigns(intx, inty)
 {
     longlongproduct = 1ll * x * y;
     return(product < 0);
 }
   intmain()
 {
     intx = 100, y = -100;
     if(oppositeSigns(x, y) == true)
         cout << "Signs are opposite";
     else
         cout << "Signs are not opposite";
     return0;
 }
   | 
 
 
 
 
C
| 
#include <stdbool.h>
 #include <stdio.h>
   booloppositeSigns(intx, inty)
 {
     longlongproduct = 1ll * x * y;
     return(product < 0);
 }
   intmain()
 {
     intx = 100, y = -100;
     if(oppositeSigns(x, y) == true)
         printf("Signs are opposite");
     else
         printf("Signs are not opposite");
     return0;
 }
   | 
 
 
 
 
Java
| 
importjava.util.*;
   classGFG {
     staticbooleanoppositeSigns(intx, inty)
   {
     longproduct = 1*x*y;
     return(product<0);
   }
     
   publicstaticvoidmain(String[] args)
   {
     intx = 100, y = -100;
     if(oppositeSigns(x, y) == true)
       System.out.print( "Signs are opposite");
     else
       System.out.print("Signs are not opposite");
   }
 }
   | 
 
 
 
 
Python3
| 
  defoppositeSigns(x,y):
     product =x*y
     return(product<0)
   x =100
 y =-100
 if(oppositeSigns(x, y) ==True):
   print("Signs are opposite") 
 else:
   print("Signs are not opposite")
   
 | 
 
 
 
 
C#
| 
usingSystem;
 classGFG{
     staticbooloppositeSigns(intx, inty)
   {
     longproduct = 1*x*y;
     return(product<0);
   }
   publicstaticvoidMain(String[] args)
 {
     intx = 100, y = -100;
     if(oppositeSigns(x, y) == true)
       Console.WriteLine( "Signs are opposite");
     else
       Console.WriteLine("Signs are not opposite");
 }
 }
   | 
 
 
 
 
Javascript
| 
  functionoppositeSigns(x,y)
 {
     const product = Number(x)*Number(y);
     return(product<0);
 }
   let x = 100, y = -100;
 if(oppositeSigns(x, y) == true)
 {
     console.log("Signs are opposite");
 }
 elseconsole.log("Signs are not opposite");
   
 | 
 
 
 
 
 
Output
Signs are opposite
 
Approach: The basic approach is to calculate the product of the two integers, and as we know, two integers having opposite signs will always produce a negative integer, we need to just find out whether the product is negative or not.
Time Complexity: O(1)
Auxiliary Space: O(1)
Please write comments if you find any of the above codes/algorithms incorrect, or find other ways to solve the same problem.