The easiest way to check if two integers have the same sign?

What is the easiest way to check if two integers have the same sign? Is there a short bitwise trick to do this?

+55
bit-manipulation integer sign
Sep 15 '08 at 20:57
source share
18 answers

Here is a version that works in C / C ++ that does not rely on integer sizes or has an overflow problem (i.e. x * y> = 0 does not work)

bool SameSign(int x, int y) { return (x >= 0) ^ (y < 0); } 

Of course, you can shout out a pattern too:

 template <typename valueType> bool SameSign(typename valueType x, typename valueType y) { return (x >= 0) ^ (y < 0); } 

Note. Since we use an exclusive form, or we want the LHS and RHS to be different when the signs are the same, therefore a different check against zero.

+42
Sep 15 '08 at 21:12
source share

what happened with

 return ((x<0) == (y<0)); 

?

+185
Sep 15 '08 at 22:06
source share
 (a ^ b) >= 0 

will evaluate to 1 if the sign is the same, 0 otherwise.

+18
Sep 15 '08 at 21:06
source share

I would be wary of any bitwise tricks to determine the sign of integers, since then you should make assumptions about how these numbers are represented inside.

Almost 100% of the time, integers will be stored as two compliments , but it is not a good practice to make assumptions about internal components unless you use a data type that guarantees a specific storage format.

In two compliments, you can simply check the last (leftmost) bit in the integer to determine if it is negative, so you can only compare these two bits. This would mean that 0 would have the same sign as a positive number, but this contradicts the sign function implemented in most languages.

Personally, I just use the sign function of your chosen language. It is unlikely that there will be any performance issues with such calculations.

+11
Sep 15 '08 at 21:06
source share

Assuming 32 bit ints:

 bool same = ((x ^ y) >> 31) != 1; 

A bit more concise:

 bool same = !((x ^ y) >> 31); 
+6
Sep 15 '08 at 21:01
source share

I am not sure that I consider "bitwise" and "the simplest" synonym. I see many answers suggesting signed 32-bit integers (although it would be foolish to ask unsigned); I'm not sure if they apply to floating point values.

It seems that the “simplest” test would be to compare how both values ​​compare with 0; this is pretty general, assuming types can be compared:

 bool compare(T left, T right) { return (left < 0) == (right < 0); } 

If the signs are opposite, you become false. If the signs match, you will get the truth.

+5
Sep 15 '08 at 21:25
source share

(integer1 * integer2)> 0

Because when two integers have a sign, the result of the multiplication will always be positive.

You can also do this> = 0 if you want to treat 0 as the same sign no matter what.

+4
Sep 15 '08 at 21:01
source share

Assuming arithmetic arithmetic ( http://en.wikipedia.org/wiki/Two_complement ):

 inline bool same_sign(int x, int y) { return (x^y) >= 0; } 

This can take only two instructions and less than 1 ns on a modern processor with optimization.

Do not assume arithmetic arithmetic:

 inline bool same_sign(int x, int y) { return (x<0) == (y<0); } 

This may require one or two additional instructions and do a little more.

Using multiplication is a bad idea because it is vulnerable to overflow.

+4
Sep 15 '08 at 22:45
source share

if (x * y)> 0 ...

assuming nonzero and such.

+2
15 Sep '08 at 20:58
source share

As a technical note, bit-twiddly solutions will be much more efficient than multiplication even on modern architectures. These are just about three cycles that you save, but you know what they say about the “saved penny” ...

+2
Sep 15 '08 at 21:04
source share

Just from the head ...

 int mask = 1 << 31; (a & mask) ^ (b & mask) < 0; 
+1
Sep 15 '08 at 20:59
source share

For any int size with two arithmetic additions:

 #define SIGNBIT (~((unsigned int)-1 >> 1)) if ((x & SIGNBIT) == (y & SIGNBIT)) // signs are the same 
+1
Sep 15 '08 at 21:15
source share

Assume 32 bit

if ((((x ^ y) and 0x80000000) == 0)

... if (x * y> 0) answer is bad due to overflow

+1
Sep 15 '08 at 21:18
source share

non-redistributable version of C:

 int sameSign(int a, int b) { return ~(a^b) & (1<<(sizeof(int)*8-1)); } 

C ++ pattern for integer types:

 template <typename T> T sameSign(T a, T b) { return ~(a^b) & (1<<(sizeof(T)*8-1)); } 
+1
Jun 02 2018-11-11T00:
source share

if the sign (a * b <0) is different, otherwise the sign is the same (or a or b is zero)

0
Sep 15 '08 at 21:00
source share

Returning to my university days, in most representations of machines, is this not the leftmost bit of the integer a 1 when the number is negative, and 0 when it is positive?

I suppose it depends more on the machine.

0
Sep 15 '08 at 21:01
source share

int same_sign =! ((x → 31) ^ (y → 31));

if (same_sign) ... else ...

0
Sep 15 '08 at 21:04
source share

It is better to use std :: signbit as follows:

 std::signbit(firstNumber) == std::signbit(secondNumber); 

It also supports other base types ( double , float , char , etc.).

0
Dec 01 '15 at 10:16
source share



All Articles