Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added BitTrick program that contains all small-small tricks of Bit Manipulation #1269

Merged
merged 1 commit into from
Nov 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions BitTrick.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <bits/stdc++.h>
using namespace std;
int main()
{
/**
* @brief Check the number is Power of 2 or not
*
*/
int n;
cin >> n;
if (n & (n - 1))
{
cout << "NO";
}
else
{
cout << "YES";
}
/**
* @brief Convert UpperCase to LowerCase and Viceversa
*
*/
char ch;
cin >> ch;
ch |= 1 << 5; // UpperCase to LowerCase
std::cout << ch << std::endl;
ch ^= 1 << 5; // LowerCase to UpperCase
std::cout << ch << std::endl;
/**
* @brief Find the number of set bits in a number
*
*/
int n;
cin >> n;
std::cout << __builtin_popcount(n) << std::endl;
/**
* @brief Find the number which is not Repeated in the array
*
*/
vector<int> arr = {1, 5, 2, 1, 3, 4, 5, 8, 4, 8, 3};
int ans = 0;
for (size_t i = 0; i < arr.size(); i++)
{
ans ^= arr[i];
}
std::cout << ans << std::endl;
/**
* @brief Odd Even using bits
*
*/
int n;
cin >> n;
if (n & 1)
{
cout << "Odd";
}
else
{
cout << "Even";
}
/**
* @brief multiply by two and divide by two
*
*/
int n;
cin >> n;
cout << (n << 1) << endl; // multiply by two
cout << (n >> 1) << endl; // divide by two
return 0;
}