- Notifications
You must be signed in to change notification settings - Fork 0
BitManipulation
Bitwise operations are fundamental to many low-level programming tasks, and C++ provides a comprehensive set of operators to manipulate individual bits in an integer. Here, we'll explore the primary bitwise operators and demonstrate their usage through various code examples. Each code snippet will include a brief explanation to aid understanding.
Certainly! Here's how you can structure the content for a GitHub wiki, complete with a table of contents:
Bitwise operations are fundamental to many low-level programming tasks, and C++ provides a comprehensive set of operators to manipulate individual bits in an integer. Here, we'll explore the primary bitwise operators and demonstrate their usage through various code examples. Each code snippet will include a brief explanation to aid understanding.
- Bitwise Operators
- Examples and Explanations
- Basic Bitwise Operations
- Binary to Decimal Conversion
- Check if the ith Bit is Set
- Check if a Number is a Power of 2
- Clear the ith Bit of a Number
- Count Number of Set Bits
- Decimal to Binary Conversion
- Check if a Number is Odd or Even
- Remove the Last Set Bit
- Set the ith Bit of a Number
- Swap Two Numbers Using Bitwise Operations
- XOR of All Numbers from 1 to n
- Removing the Last Set Bit Using Different Techniques
- cp-question
- AND (
&) - OR (
|) - XOR (
^) - Left Shift (
<<) - Right Shift (
>>) - NOT (
~)
#include<iostream>usingnamespacestd;intmain() {
int a = 0, b = -6;
cout << (a & b) << endl; // AND operation
cout << (a | b) << endl; // OR operation
cout << (a ^ b) << endl; // XOR operation
cout << (b >> 1) << endl; // Right shift
cout << (b << 1) << endl; // Left shift
cout << ~b << endl; // NOT operationreturn0;
}Explanation:
a & bperforms a bitwise AND.a | bperforms a bitwise OR.a ^ bperforms a bitwise XOR.b >> 1shiftsbright by 1 bit.b << 1shiftsbleft by 1 bit.~binverts all the bits ofb.
#include<iostream>
#include<string>
#include<cmath>usingnamespacestd;intmain() {
int ans = 0, p = 0;
string bin = "100";
while (!bin.empty()) {
ans += (bin.back() - '0') * pow(2, p);
p++;
bin.pop_back();
}
cout << ans;
return0;
}Explanation:
- Converts a binary string
"100"to its decimal equivalent.
#include<iostream>usingnamespacestd;intmain() {
int num, i;
cin >> num >> i;
cout << (num & (1 << i));
return0;
}Explanation:
- Checks if the ith bit of
numis set (1) or not (0).
#include<iostream>usingnamespacestd;intmain() {
int num;
cin >> num;
if ((num & (num - 1)) == 0) {
cout << "Power of 2: " << num;
} else {
cout << "Not a power of 2: " << num;
}
return0;
}Explanation:
- Uses the property that powers of 2 have exactly one bit set.
#include<iostream>usingnamespacestd;intmain() {
int num, i;
cin >> num >> i;
cout << (num & ~(1 << i));
return0;
}Explanation:
- Clears (sets to 0) the ith bit of
num.
#include<iostream>usingnamespacestd;intmain() {
int num;
cin >> num;
int count = 0;
while (num) {
num &= num - 1;
count++;
}
cout << count;
return0;
}Explanation:
- Counts the number of 1s in the binary representation of
num.
#include<iostream>
#include<string>usingnamespacestd;intmain() {
string s = "";
int n;
cin >> n;
while (n) {
s = to_string(n % 2) + s;
n /= 2;
}
cout << s;
return0;
}Explanation:
- Converts a decimal number to its binary representation.
#include<iostream>usingnamespacestd;intmain() {
int num;
cin >> num;
cout << (num & 1);
return0;
}Explanation:
- Uses the last bit to determine if
numis odd (1) or even (0).
#include<iostream>usingnamespacestd;voidforloop(int num) {
int mask, ans;
for (int i = 0; i < 32; i++) {
mask = 1 << i;
ans = num >> i;
if (ans & 1 != 0) {
cout << (num & ~mask);
return;
}
}
}
voidwhileloop(int num) {
int temp = num, count = 0;
while (temp) {
if (temp & 1 != 0) break;
count++;
temp >>= 1;
}
temp = 1 << count;
cout << (num & ~temp);
}
voidbitwisemanipulation(int num) {
cout << (num & (num - 1));
}
intmain() {
int num = 5;
// forloop(num);// whileloop(num);bitwisemanipulation(num);
return0;
}Explanation:
- Shows different methods to remove the last set bit from
num.
#include<iostream>usingnamespacestd;intmain() {
int n, i;
cin >> n >> i;
n |= 1 << i;
cout << n;
return0;
}Explanation:
- Sets the ith bit of
nto 1.
#include<iostream>usingnamespacestd;intmain() {
int a = 5, b = 6;
cout << a << "" << b << endl;
a = a ^ b;
b = a ^ b;
a = a ^ b;
cout << a << "" << b << endl;
return0;
}Explanation:
- Swaps the values of
aandbwithout using a temporary variable.
#include<iostream>usingnamespacestd;intxorn(int num) {
int temp = num % 4;
if (temp == 0) return num;
elseif (temp == 1) return1;
elseif (temp == 2) return num + 1;
elsereturn0;
}
intmain() {
int num;
cin >> num;
cout << xorn(num);
return0;
}Explanation:
- Computes the XOR of all numbers from 1 to
num.
#include<iostream>usingnamespacestd;voidforloop(int num) {
int mask, ans;
for (int i = 0; i < 32; i++) {
mask = 1 << i;
ans = num >> i;
if (ans & 1 != 0) {
cout << (num & ~mask);
return;
}
}
}
voidwhileloop(int num) {
int temp = num, count = 0;
while (temp) {
if (temp & 1 != 0) break;
count++;
temp >>= 1;
}
temp = 1 << count;
cout << (num & ~temp);
}
voidbitwisemanipulation(int num) {
cout << (num & (num - 1));
}
intmain() {
int num = 5;
// forloop(num);// whileloop(num);bitwisemanipulation(num);
return0;
}Explanation:
- Demonstrates three different ways to remove the last set bit of
num.
These examples cover a broad spectrum of bitwise operations, illustrating how to manipulate and query individual bits efficiently in C++.
- Twitter: @AlgoDocHub
- Facebook: AlgoDocHub
- Instagram: @AlgoDocHub
Contact Us: Have questions, suggestions, or feedback? Don't hesitate to reach out! You can contact the maintainers of AlgoDocHub by opening an issue or joining our Discord community.
Happy coding, and may your algorithms always run efficiently! *