Dados dos enteros con signo, escriba una función que devuelva verdadero si los signos de los enteros dados son diferentes, de lo contrario, falso. Por ejemplo, la función debería devolver verdadero -1 y +100 y debería devolver falso para -100 y -200. La función no debe usar ninguno de los operadores aritméticos.
Sean x e y los enteros dados. El bit de signo es 1 en números negativos y 0 en números positivos. El XOR de xey tendrá el bit de signo 1 si tienen signo opuesto. En otras palabras, XOR de xey será un número negativo si xey tienen signos opuestos. El código siguiente utiliza esta lógica.
C++
// C++ Program to Detect // if two integers have opposite signs. #include<iostream> 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<stdbool.h> #include<stdio.h> 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
<?php // PHP Program to Detect if two // integers have opposite signs. function oppositeSigns($x, $y) { return (($x ^ $y) < 0); } // Driver Code $x = 100; $y = -100; if (oppositeSigns($x, $y) == true) echo ("Signs are opposite"); else echo ("Signs are not opposite"); // This code is contributed by vt_m. ?>
Javascript
<script> // Javascript Program to Detect // if two integers have opposite signs. function oppositeSigns(x, y) { return ((x ^ y) < 0); } // Driver Code let x = 100, y = -100; if (oppositeSigns(x, y) == true) document.write("Signs are opposite"); else document.write("Signs are not opposite"); </script>
CPP
bool oppositeSigns(int x, int y) { return (x < 0)? (y >= 0): (y < 0); }
C
//C program to detect whether two integers //have different sign or not #include <stdio.h> #include<stdbool.h> bool oppositeSigns(int x, int y) { return (x < 0)? (y >= 0): (y < 0); } //this code is contributed by shruti456rawal
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
<script> function oppositeSigns(x, y) { return (x < 0)? (y >= 0): (y < 0); } // This code contributed by shivanisinghss2110 </script>
CPP
bool oppositeSigns(int x, int y) { return ((x ^ y) >> 31); }
C
//C program to detect whether two integers //have different sign or not #include <stdio.h> #include<stdbool.h> bool oppositeSigns(int x, int y) { return ((x ^ y) >> 31); } //this code is contributed by shruti456rawal
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
<script> function oppositeSigns(x, y) { return ((x ^ y) >> 31); } // This code is contributed by shivanisinghss2110 </script>
C++
// C++ Program to detect if two integers have opposite // signs. #include <iostream> using namespace std; bool oppositeSigns(int x, int y) { long long product = 1ll * x * y; return (product < 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 Aditya Kumar (adityakumar129)
C
// C Program to detect // if two integers have opposite signs. #include <stdbool.h> #include <stdio.h> bool oppositeSigns(int x, int y) { long long product = 1ll * x * y; return (product < 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; } // This code is contributed by Aditya Kumar (adityakumar129)
Java
// Java program for the above approach import java.util.*; class GFG { static boolean oppositeSigns(int x, int y) { long product = 1*x*y; return (product<0); } // Driver Code public static void main(String[] args) { int x = 100, y = -100; if (oppositeSigns(x, y) == true) System.out.print( "Signs are opposite"); else System.out.print("Signs are not opposite"); } } // This code is contributed by sanjoy_62.
Python3
# Python Program to detect # if two integers have opposite signs. def oppositeSigns(x,y): product = x*y return (product<0) # driver code x = 100 y = -100 if(oppositeSigns(x, y) == True): print("Signs are opposite") else : print("Signs are not opposite") # this code is contributed by shinjanpatra
C#
// C# program for the above approach using System; class GFG{ static bool oppositeSigns(int x, int y) { long product = 1*x*y; return (product<0); } // Driver Code public static void Main(String[] args) { int x = 100, y = -100; if (oppositeSigns(x, y) == true) Console.WriteLine( "Signs are opposite"); else Console.WriteLine("Signs are not opposite"); } } // This code is contributed by avijitmondal1998.
Javascript
// JavaScript Program to detect // if two integers have opposite signs. function oppositeSigns(x,y) { const product = Number(x)*Number(y); return (product<0); } // driver code let x = 100, y = -100; if(oppositeSigns(x, y) == true) { console.log("Signs are opposite"); } else console.log("Signs are not opposite"); // this code is contributed by shinjanpatra
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA