// Detect if two integers have opposite sign on not.
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
int x = 4;
int y = -8;
cout << x << " in binary is " << bitset<32>(x) << endl;
cout << y << " in binary is " << bitset<32>(y) << endl;
// true if x and y have opposite signs
bool isOpposite = ((x ^ y) < 0);
if (isOpposite)
cout << x << " and " << y << " have opposite signs";
else
cout << x << " and " << y << " don't have opposite signs";
return 0;
}
Last active
June 2, 2020 08:00
-
-
Save arrbxr/b6ca185dcc93089bcf36663913904032 to your computer and use it in GitHub Desktop.
Bit Manipulation Interview Questions and Practice Problems
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Check if a given integer is even or odd. | |
/* | |
n = 5 = 101(binary) | n = 6 = 0110 | |
1 = 001(binary) | 1 = 0001 | |
------------ | -------- | |
& - 001 == 1 (true) | &- 0000 == 0 (false) | |
*/ | |
#include<iostream> | |
using namespace std; | |
int main() { | |
int n = 5; | |
if(n & 1) | |
cout<<n<<" is odd number"<<endl; | |
else | |
cout<<n<<" is even number"<<endl; | |
return 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment