mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-02-11 14:36:25 +08:00
Added bit manipulation algorithms.
This commit is contained in:
40
bit_manipulation/hamming_distance.cpp
Normal file
40
bit_manipulation/hamming_distance.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief Returns the Hamming distance between two integers
|
||||
*
|
||||
* @details
|
||||
* To find hamming distance between two integers, we take their xor, which will
|
||||
* have a set bit iff those bits differ in the two numbers.
|
||||
* Hence, we return the number of such set bits.
|
||||
*
|
||||
* @author [Ravishankar Joshi](https://github.com/ravibitsgoa)
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
using std::cout, std::endl;
|
||||
|
||||
unsigned int bitCount(unsigned int value) {
|
||||
unsigned int count = 0;
|
||||
while (value) { // until all bits are zero
|
||||
if (value & 1) { // check lower bit
|
||||
count++;
|
||||
}
|
||||
value >>= 1; // shift bits, removing lower bit
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
unsigned int hamming_distance(int a, int b) {
|
||||
if (a < 0 || b < 0) {
|
||||
throw "Both arguments must be >=0 for finding hamming distance.";
|
||||
}
|
||||
return bitCount(a ^ b);
|
||||
}
|
||||
|
||||
int main() {
|
||||
int a = 11; // 1011 in binary
|
||||
int b = 2; // 0010 in binary
|
||||
|
||||
std::cout << "Hamming distance between " << a << " and " << b << " is "
|
||||
<< hamming_distance(a, b) << std::endl;
|
||||
}
|
||||
27
bit_manipulation/power_of_2.cpp
Normal file
27
bit_manipulation/power_of_2.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief Finding if a number is a power of 2.
|
||||
*
|
||||
* @details
|
||||
* To find if a number is a power of 2, we can check if bitwise AND of number
|
||||
* and number - 1 is zero or not. Only for the numbers which are a power of 2,
|
||||
* bitwise AND(x, x-1) is zero.
|
||||
*
|
||||
* @author [Ravishankar Joshi](https://github.com/ravibitsgoa)
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
using std::vector;
|
||||
|
||||
bool ispower2(int x) { return x > 0 && (x & (x - 1)) == 0; }
|
||||
|
||||
int main() {
|
||||
vector<int> arr{1, 2, 3, 4, 8, 10};
|
||||
for (int x : arr) {
|
||||
cout << x << " is " << (ispower2(x) ? "" : "not ") << "a power of 2."
|
||||
<< endl;
|
||||
}
|
||||
}
|
||||
19
bit_manipulation/xor_swap.cpp
Normal file
19
bit_manipulation/xor_swap.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief Swapping two integers using xor.
|
||||
*
|
||||
* @author [Ravishankar Joshi](https://github.com/ravibitsgoa)
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
int main() {
|
||||
int a = 1, b = 2;
|
||||
cout << "a = " << a << ", b = " << b << endl;
|
||||
a ^= b;
|
||||
b ^= a;
|
||||
a ^= b;
|
||||
cout << "a = " << a << ", b = " << b << endl;
|
||||
}
|
||||
Reference in New Issue
Block a user