From 6e4ecfa2ff546c58b4cc42a8b9244f95f7c9d3e1 Mon Sep 17 00:00:00 2001 From: "yogeshcjadhaav@gmail.com" Date: Fri, 29 Oct 2021 20:21:20 +0530 Subject: [PATCH] added BitTrick program that contains all small-small tricks of Bit Manupulation --- BitTrick.cpp | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 BitTrick.cpp diff --git a/BitTrick.cpp b/BitTrick.cpp new file mode 100644 index 0000000000..53f30e9046 --- /dev/null +++ b/BitTrick.cpp @@ -0,0 +1,70 @@ +#include +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 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; +} \ No newline at end of file