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.
Signs are opposite
We can also solve this by using two comparison operators. See the following code.
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.
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.
- Maximum subset sum such that no two elements in set have same digit in them
- Compute the minimum or maximum of two integers without branching
- How to sum two integers without using arithmetic operators in C/C++?
- Compare two integers without using any Comparison operator
- Divide two integers without using multiplication, division and mod operator
- Bitwise recursive addition of two integers
- Hamming distance between two Integers
- Number of mismatching bits in the binary representation of two integers
- Find two integers A and B such that A ^ N = A + N and B ^ N = B + N
- Compute maximum of two integers in C/C++ using Bitwise Operators
- Numbers formed by flipping common set bits in two given integers
- Find k numbers which are powers of 2 and have sum N | Set 1
- Count pairs in an array which have at least one digit common
- Minimum flips in a Binary array such that XOR of consecutive subarrays of size K have different parity
- Smallest of three integers without comparison operators
- Find maximum XOR of given integer in a stream of integers
- Check if given four integers (or sides) make rectangle
- Number of integers with odd number of set bits
- Find the missing element in an array of integers represented in binary format
- Find a number which give minimum sum when XOR with every number of array of integers