<p>Which is the simplest way to check if two integers have same sign? Is there any short bitwise trick to do this?</p>

[{'answer_id': 66892, 'author': 'Yes - that Jake.', 'author_id': 5287, 'author_profile': 'https://Stackoverflow.com/users/5287', 'pm_score': 2, 'selected': False, 'text': '<p>if (x * y) > 0...</p>\n\n<p>assuming non-zero and such.</p>\n'}, {'answer_id': 66908, 'author': 'Daniel Spiewak', 'author_id': 9815, 'author_profile': 'https://Stackoverflow.com/users/9815', 'pm_score': 1, 'selected': False, 'text': '<p>Just off the top of my head...</p>\n\n<pre><code>int mask = 1 &lt;&lt; 31;\n(a &amp; mask) ^ (b &amp; mask) &lt; 0;\n</code></pre>\n'}, {'answer_id': 66915, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>if (a*b &lt; 0) sign is different, else sign is the same (or a or b is zero)</p>\n'}, {'answer_id': 66928, 'author': 'Patrick', 'author_id': 429, 'author_profile': 'https://Stackoverflow.com/users/429', 'pm_score': 3, 'selected': False, 'text': '<p>Assuming 32 bit ints:</p>\n\n<pre><code>bool same = ((x ^ y) &gt;&gt; 31) != 1;\n</code></pre>\n\n<p>Slightly more terse:</p>\n\n<pre><code>bool same = !((x ^ y) &gt;&gt; 31);\n</code></pre>\n'}, {'answer_id': 66929, 'author': 'Dana', 'author_id': 7856, 'author_profile': 'https://Stackoverflow.com/users/7856', 'pm_score': 0, 'selected': False, 'text': "<p>Thinking back to my university days, in most machine representations, isn't the left-most bit of a integer a 1 when the number is negative, and 0 when it's positive?</p>\n\n<p>I imagine this is rather machine-dependent, though.</p>\n"}, {'answer_id': 66931, 'author': 'Benjamin Autin', 'author_id': 1440933, 'author_profile': 'https://Stackoverflow.com/users/1440933', 'pm_score': 3, 'selected': False, 'text': '<p>(integer1 * integer2) > 0</p>\n\n<p>Because when two integers share a sign, the result of multiplication will always be positive.</p>\n\n<p>You can also make it >= 0 if you want to treat 0 as being the same sign no matter what.</p>\n'}, {'answer_id': 66953, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>int same_sign = !( (x >> 31) ^ (y >> 31) );</p>\n\n<p>if ( same_sign ) ...\nelse             ...</p>\n'}, {'answer_id': 66960, 'author': 'Daniel Spiewak', 'author_id': 9815, 'author_profile': 'https://Stackoverflow.com/users/9815', 'pm_score': 2, 'selected': False, 'text': '<p>As a technical note, bit-twiddly solutions are going to be much more efficient than multiplication, even on modern architectures.  It\'s only about 3 cycles that you\'re saving, but you know what they say about a "penny saved"...</p>\n'}, {'answer_id': 66968, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 5, 'selected': False, 'text': '<pre><code>(a ^ b) &gt;= 0\n</code></pre>\n\n<p>will evaluate to 1 if the sign is the same, 0 otherwise.</p>\n'}, {'answer_id': 66972, 'author': 'SpoonMeiser', 'author_id': 1577190, 'author_profile': 'https://Stackoverflow.com/users/1577190', 'pm_score': 4, 'selected': False, 'text': '<p>I would be wary of any bitwise tricks to determine the sign of integers, as then you have to make assumptions about how those numbers are represented internally.</p>\n\n<p>Almost 100% of the time, integers will be stored as <a href="http://en.wikipedia.org/wiki/Twos_Compliment" rel="noreferrer">two\'s compliment</a>, but it\'s not good practice to make assumptions about the internals of a system unless you are using a datatype that guarentees a particular storage format.</p>\n\n<p>In two\'s compliment, you can just check the last (left-most) bit in the integer to determine if it is negative, so you can compare just these two bits. This would mean that 0 would have the same sign as a positive number though, which is at odds with the sign function implemented in most languages.</p>\n\n<p>Personally, I\'d just use the sign function of your chosen language. It is unlikely that there would be any performance issues with a calculation such as this.</p>\n'}, {'answer_id': 67020, 'author': 'Torlack', 'author_id': 5243, 'author_profile': 'https://Stackoverflow.com/users/5243', 'pm_score': 7, 'selected': True, 'text': "<p>Here is a version that works in C/C++ that doesn't rely on integer sizes or have the overflow problem (i.e. x*y>=0 doesn't work)</p>\n\n<pre><code>bool SameSign(int x, int y)\n{\n    return (x &gt;= 0) ^ (y &lt; 0);\n}\n</code></pre>\n\n<p>Of course, you can geek out and template:</p>\n\n<pre><code>template &lt;typename valueType&gt;\nbool SameSign(typename valueType x, typename valueType y)\n{\n    return (x &gt;= 0) ^ (y &lt; 0);\n}\n</code></pre>\n\n<p>Note: Since we are using exclusive or, we want the LHS and the RHS to be different when the signs are the same, thus the different check against zero.</p>\n"}, {'answer_id': 67041, 'author': 'Mark Ransom', 'author_id': 5987, 'author_profile': 'https://Stackoverflow.com/users/5987', 'pm_score': 1, 'selected': False, 'text': "<p>For any size of int with two's complement arithmetic:</p>\n\n<pre><code>#define SIGNBIT (~((unsigned int)-1 &gt;&gt; 1))\nif ((x &amp; SIGNBIT) == (y &amp; SIGNBIT))\n    // signs are the same\n</code></pre>\n"}, {'answer_id': 67061, 'author': 'ugasoft', 'author_id': 10120, 'author_profile': 'https://Stackoverflow.com/users/10120', 'pm_score': 1, 'selected': False, 'text': '<p>assuming 32 bit</p>\n\n<p><code>if(((x^y) &amp; 0x80000000) == 0)</code></p>\n\n<p>... the answer <code>if(x*y&gt;0)</code> is bad due to overflow</p>\n'}, {'answer_id': 67133, 'author': 'OwenP', 'author_id': 2547, 'author_profile': 'https://Stackoverflow.com/users/2547', 'pm_score': 3, 'selected': False, 'text': '<p>I\'m not really sure I\'d consider "bitwise trick" and "simplest" to be synonymous.  I see a lot of answers that are assuming signed 32-bit integers (though it <em>would</em> be silly to ask for unsigned); I\'m not certain they\'d apply to floating-point values.</p>\n\n<p>It seems like the "simplest" check would be to compare how both values compare to 0; this is pretty generic assuming the types can be compared:</p>\n\n<pre><code>bool compare(T left, T right)\n{\n    return (left &lt; 0) == (right &lt; 0);\n}\n</code></pre>\n\n<p>If the signs are opposite, you get false.  If the signs are the same, you get true.</p>\n'}, {'answer_id': 67498, 'author': 'Rik', 'author_id': 5409, 'author_profile': 'https://Stackoverflow.com/users/5409', 'pm_score': 8, 'selected': False, 'text': "<p>What's wrong with</p>\n<pre><code>return ((x&lt;0) == (y&lt;0));  \n</code></pre>\n<p>?</p>\n"}, {'answer_id': 67710, 'author': 'user10315', 'author_id': 10315, 'author_profile': 'https://Stackoverflow.com/users/10315', 'pm_score': 2, 'selected': False, 'text': '<p>Assuming twos complement arithmetic (<a href="http://en.wikipedia.org/wiki/Two_complement" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Two_complement</a>):</p>\n\n<pre><code>inline bool same_sign(int x, int y) {\n    return (x^y) &gt;= 0;\n}\n</code></pre>\n\n<p>This can take as little as two instructions and less than 1ns on a modern processor with optimization.</p>\n\n<p>Not assuming twos complement arithmetic:</p>\n\n<pre><code>inline bool same_sign(int x, int y) {\n    return (x&lt;0) == (y&lt;0);\n}\n</code></pre>\n\n<p>This may require one or two extra instructions and take a little longer.</p>\n\n<p>Using multiplication is a bad idea because it is vulnerable to overflow.</p>\n'}, {'answer_id': 6215769, 'author': 'CAFxX', 'author_id': 414813, 'author_profile': 'https://Stackoverflow.com/users/414813', 'pm_score': 1, 'selected': False, 'text': '<p>branchless C version:</p>\n\n<pre><code>int sameSign(int a, int b) {\n    return ~(a^b) &amp; (1&lt;&lt;(sizeof(int)*8-1));\n}\n</code></pre>\n\n<p>C++ template for integer types:</p>\n\n<pre><code>template &lt;typename T&gt; T sameSign(T a, T b) {\n    return ~(a^b) &amp; (1&lt;&lt;(sizeof(T)*8-1));\n}\n</code></pre>\n'}, {'answer_id': 34031263, 'author': 'ashiquzzaman33', 'author_id': 2317535, 'author_profile': 'https://Stackoverflow.com/users/2317535', 'pm_score': 0, 'selected': False, 'text': '<p>Better way using <a href="http://en.cppreference.com/w/cpp/numeric/math/signbit" rel="nofollow">std::signbit</a> as follows:</p>\n\n<pre><code>std::signbit(firstNumber) == std::signbit(secondNumber);\n</code></pre>\n\n<p>It also support other basic types (<code>double</code>, <code>float</code>, <code>char</code> etc).</p>\n'}, {'answer_id': 67649969, 'author': 'Krazzy4Code', 'author_id': 16000816, 'author_profile': 'https://Stackoverflow.com/users/16000816', 'pm_score': 0, 'selected': False, 'text': '<pre><code>#include&lt;stdio.h&gt;\n\nint checksign(int a, int b)\n{\n return (a ^ b); \n}\n\nvoid  main()\n{\n    int a=-1, b = 0;\n\n    if(checksign(a,b)&lt;0)\n    {\n        printf(&quot;Integers have the opposite sign&quot;);\n    }\n    else\n    {\n        printf(&quot;Integers have the same sign&quot;);\n    }\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/66882', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']