{"id":25723,"date":"2017-10-25T20:12:03","date_gmt":"2017-10-25T14:42:03","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25723"},"modified":"2017-10-25T20:12:03","modified_gmt":"2017-10-25T14:42:03","slug":"detect-two-integers-opposite-signs","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/detect-two-integers-opposite-signs\/","title":{"rendered":"Detect if two integers have opposite signs"},"content":{"rendered":"<p>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.<\/p>\n<p>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.<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%23include%3Cstdbool.h%3E%0A%23include%3Cstdio.h%3E%0A%20%0Abool%20oppositeSigns(int%20x%2C%20int%20y)%0A%7B%0A%20%20%20%20return%20((x%20%5E%20y)%20%3C%200)%3B%0A%7D%0A%20%0Aint%20main()%0A%7B%0A%20%20%20%20int%20x%20%3D%20100%2C%20y%20%3D%20-100%3B%0A%20%20%20%20if%20(oppositeSigns(x%2C%20y)%20%3D%3D%20true)%0A%20%20%20%20%20%20%20printf%20(%22Signs%20are%20opposite%22)%3B%0A%20%20%20%20else%0A%20%20%20%20%20%20printf%20(%22Signs%20are%20not%20opposite%22)%3B%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201dc\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output:<\/strong><\/p>\n<pre>Signs are opposite<\/pre>\n<p><strong>Source:<\/strong> Detect if two integers have opposite signs<\/p>\n[ad type=\u201dbanner\u201d]\n<p>We can also solve this by using two comparison operators. See the following code.<\/p>\n[pastacode lang=\u201dc\u201d manual=\u201dbool%20oppositeSigns(int%20x%2C%20int%20y)%0A%7B%0A%20%20%20%20return%20(x%20%3C%200)%3F%20(y%20%3E%3D%200)%3A%20(y%20%3C%200)%3B%0A%7D\u201d message=\u201dc\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>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.<\/p>\n[ad type=\u201dbanner\u201d]\n<p>We can also use following method. It doesn\u2019t use any comparison operator. The method is suggested by Hongliang and improved by gaurav.<\/p>\n[pastacode lang=\u201dc\u201d manual=\u201dbool%20oppositeSigns(int%20x%2C%20int%20y)%0A%7B%0A%20%20%20%20return%20((x%20%5E%20y)%20%3E%3E%2031)%3B%0A%7D\u201d message=\u201dc\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>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 \u2018>>\u2019. 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.<\/p>\n[ad type=\u201dbanner\u201d]\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Detect if two integers have opposite signs &#8211; Bit Algorithm &#8211; Given two signed integers, write a function that returns true if the signs of given integers.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,74852,83562],"tags":[74923,74442,74910,74918,74456,74916,74450,74449,74925,74452,74912,74441,74931,74932,74915,74914,74439,74926,74435,74432,74618,74911,74451,74434,74445,74436,74919,74437,74929,74920,74924,74930,74444,74429,74917,74913,74933,74447,74438,74909,74454,74908,74443,74446,74440,74921,74922,74433,74927,74928],"class_list":["post-25723","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-bit-algorithms","category-integers","tag-add-numbers","tag-adding-and-subtracting-negative-numbers","tag-adding-and-subtracting-negative-numbers-worksheets","tag-adding-integers-calculator","tag-adding-integers-rules","tag-adding-integers-worksheet","tag-addition-of-integers","tag-addition-of-integers-on-number-line","tag-addition-of-integers-worksheet","tag-calculator-integers","tag-different-signs-in-maths","tag-examples-of-integers","tag-grade-7-integers-test","tag-how-to-add-integers-with-different-signs","tag-how-to-add-positive-and-negative-numbers","tag-how-to-solve-integers","tag-integer","tag-integer-addition-and-subtraction-worksheet","tag-integer-math","tag-integer-numbers","tag-integer-problems","tag-integer-problems-and-answers","tag-integer-rules","tag-integergames","tag-integers-examples","tag-integers-in-math","tag-integers-numbers-examples","tag-integers-questions-and-answers","tag-integers-quiz-worksheet","tag-integers-test-grade-7","tag-integers-worksheet","tag-math-integer-games","tag-math-integers","tag-multiplication-of-integers","tag-negative-and-positive-numbers","tag-ordering-integers-worksheet","tag-positive-and-negative-numbers","tag-rules-for-subtracting-integers","tag-rules-of-integers","tag-rules-of-subtraction","tag-rules-of-subtraction-of-integers","tag-signs-of-add","tag-subtracting-integers","tag-subtraction-of-integers","tag-what-are-integers-in-math","tag-what-is-an-integer-number","tag-what-is-integer-number","tag-what-is-integers-in-math","tag-worksheet-on-integers","tag-worksheets-for-integers"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25723","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25723"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25723\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25723"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25723"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25723"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}