Skip to content

Commit

Permalink
Added a program to check if two integers are of opposite signs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravi Mandliya committed Apr 9, 2017
1 parent 74f84c9 commit f28c1f3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ Include contains single header implementation of data structures and some algori
| Given a number x and two positions (from right side) in binary representation of x, write a function that swaps n right bits at given two positions and returns the result. It is also given that the two sets of bits do not overlap.|[swapSetOfBits.cpp](bit_manipulation/swapSetOfBits.cpp)|
| Add two numbers without using any arithmetic operators | [addition_without_operators.cpp](bit_manipulation/addition_without_operators.cpp)|
|Louise and Richard play a game. They have a counter set to N. Louise gets the first turn and the turns alternate thereafter. In the game, they perform the following operations: <ul><li>If N is not a power of 2, reduce the counter by the largest power of 2 less than N.</li></ul><ul><li>If N is a power of 2, reduce the counter by half of N.</li></ul> The resultant value is the new N which is again used for subsequent operations.The game ends when the counter reduces to 1, i.e., N == 1, and the last person to make a valid move wins. <ul><li> Given N, your task is to find the winner of the game. If they set counter to 1, Richard wins, because its Louise' turn and she cannot make a move.</li></ul><ul><li>Input Format : -The first line contains an integer T, the number of testcases. T lines follow. Each line contains N, the initial number set in the counter.</ul></li> |[counter_game.cpp](bit_manipulation/counter_game.cpp)|
|Determine if two integers are of opposite signs|[check_opposite_signs.cpp](bit_manipulation/check_opposite_signs.cpp)|


### Cracking the coding interview problems
Expand Down
35 changes: 35 additions & 0 deletions bit_manipulation/check_opposite_signs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Given two integers, using bit manipulations determine if they are of opposite signs.
* Most Significant Bit (MSB) of number represents sign of the number. If it is 1, it represents
* a negative value, if it is 0, it represents a positive value.
* If MSB of two numbers are different, their XOR would be 1, otherwise it would be 0.
* Thus, result of XOR of two numbers will have MSB 1 if they are of opposite signs,
* 0 other wise, in other words, XOR of two numbers would be negative (MSB 1) if they are of
* opposite signs.
* Source : http://graphics.stanford.edu/~seander/bithacks.html
*/

#include <iostream>

bool are_of_different_signs(int a, int b)
{
return ((a ^ b) < 0);
}

int main()
{
int a, b;
std::cout << "Enter number 1: ";
std::cin >> a;
std::cout << "Enter number 2:";
std::cin >> b;
if (are_of_different_signs(a, b))
{
std::cout << a << " and " << b << " are of different signs" << std::endl;
}
else
{
std::cout << a << " and " << b << " are of same signs" << std::endl;
}
return 0;
}

0 comments on commit f28c1f3

Please sign in to comment.