Skip to content

Instantly share code, notes, and snippets.

@arrbxr
Last active June 2, 2020 08:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arrbxr/b6ca185dcc93089bcf36663913904032 to your computer and use it in GitHub Desktop.
Save arrbxr/b6ca185dcc93089bcf36663913904032 to your computer and use it in GitHub Desktop.
Bit Manipulation Interview Questions and Practice Problems

Detect if two integers have opposite sign on not.

// 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;
}
// 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