Skip to content

Commit f28c1f3

Browse files
author
Ravi Mandliya
committed
Added a program to check if two integers are of opposite signs.
1 parent 74f84c9 commit f28c1f3

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Include contains single header implementation of data structures and some algori
7070
| 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)|
7171
| Add two numbers without using any arithmetic operators | [addition_without_operators.cpp](bit_manipulation/addition_without_operators.cpp)|
7272
|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)|
73+
|Determine if two integers are of opposite signs|[check_opposite_signs.cpp](bit_manipulation/check_opposite_signs.cpp)|
7374

7475

7576
### Cracking the coding interview problems
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Given two integers, using bit manipulations determine if they are of opposite signs.
3+
* Most Significant Bit (MSB) of number represents sign of the number. If it is 1, it represents
4+
* a negative value, if it is 0, it represents a positive value.
5+
* If MSB of two numbers are different, their XOR would be 1, otherwise it would be 0.
6+
* Thus, result of XOR of two numbers will have MSB 1 if they are of opposite signs,
7+
* 0 other wise, in other words, XOR of two numbers would be negative (MSB 1) if they are of
8+
* opposite signs.
9+
* Source : http://graphics.stanford.edu/~seander/bithacks.html
10+
*/
11+
12+
#include <iostream>
13+
14+
bool are_of_different_signs(int a, int b)
15+
{
16+
return ((a ^ b) < 0);
17+
}
18+
19+
int main()
20+
{
21+
int a, b;
22+
std::cout << "Enter number 1: ";
23+
std::cin >> a;
24+
std::cout << "Enter number 2:";
25+
std::cin >> b;
26+
if (are_of_different_signs(a, b))
27+
{
28+
std::cout << a << " and " << b << " are of different signs" << std::endl;
29+
}
30+
else
31+
{
32+
std::cout << a << " and " << b << " are of same signs" << std::endl;
33+
}
34+
return 0;
35+
}

0 commit comments

Comments
 (0)