community/supports #1327
Replies: 4 comments
-
id: 05-Bit Manipulation
Introduction to Bit ManipulationBit Manipulation is a technique used in a variety of problems to get the solution in an optimized way. This technique is very effective from a Competitive Programming point of view. It is all about Bitwise Operators which directly works upon binary numbers or bits of numbers that help the implementation fast. Below are the Bitwise Operators that are used:
All data in computer programs are internally stored as bits, i.e., as numbers 0 and 1. Bit representationIn programming, an n-bit integer is internally stored as a binary number that consists of n bits. For example, the C++ type int is a 32-bit type, which means that every int number consists of 32 bits. The int number 43 = 00000000000000000000000000101011 The bits in the representation are indexed from right to left. To convert a bit representation bk ···b2 b1 b0 into a number, we can use the formula E.g., 1·25+1·23 +1·21 +1·20 = 43. The bit representation of a number is either signed or unsigned. The first bit in a signed representation is the sign of the number, 0 for non-negative numbers and 1 for negative numbers and the remaining n−1 bits contain the magnitude of the number. Two’s complement is used, which means that the opposite number of a number is calculated by first inverting all the bits in the number, and then increasing the number by one. In C++, an unsigned int variable can contain any integer between 0 and 232 −1.
If a number is larger than the upper bound of the bit representation, the number will overflow. In a signed representation, the next number after 2n-1 – 1 is -2n-1, and in an unsigned representation, the next number after 2n -1 is 0. For example, consider the following pseudo-code snippet:
Initially, the value of x is 231 −1. This is the largest value that can be stored in an int variable, so the next number after 231 −1 is −231 . Bitwise Operations:Below is the table to illustrate the result when the operation is performed using Bitwise Operators. Here 0s or 1s mean a sequence of 0 or 1 respectively. | Operators | Operations | Result | Get Bit:This method is used to find the bit at a particular position(say i) of the given number N. The idea is to find the Bitwise AND of the given number and 2i that can be represented as (1 Below is the pseudo-code for the same: ```cpp // Function to get the bit at the // ith position bool getBit(int num, int i) { // Return true if the bit is // set. Otherwise return false return ((num & (1 << i)) != 0); }
Set Bit:This method is used to set the bit at a particular position(say i) of the given number N. The idea is to update the value of the given number N to the Bitwise OR of the given number N and 2i that can be represented as (1 Below is the pseudo-code for the same: ```cpp // Function to set the bit at the // ith position int setBit(int num, int i) { // Sets the ith bit and return // the updated value return num | (1 << i); }
Clear Bit:This method is used to clear the bit at a particular position(say i) of the given number N. The idea is to update the value of the given number N to the Bitwise AND of the given number N and the compliment of 2i that can be represented as ~(1 Below is the pseudo-code for the same: ```cpp // Function to clear the bit at the // ith position int clearBit(int num, int i) { // Create the mask for the ith // bit unset int mask = ~(1 << i);
Below is the program that implements the above functionalities: // C++ program to implement all the #include <bits/stdc++.h> // Function to get the bit at the // Function to set the ith bit of the // Function to clear the ith bit of
} // Driver Code
} Output
Time Complexity: Application of Bitwise Operator
|
Beta Was this translation helpful? Give feedback.
-
please check what is wrong |
Beta Was this translation helpful? Give feedback.
-
done |
Beta Was this translation helpful? Give feedback.
-
Below is the program that implements the above functionalities:
```cpp
// C++ program to implement all the
// above functionalities
#include <bits/stdc++.h>
using namespace std;
// Function to get the bit at the
// ith position
bool getBit(int num, int i)
{
// Return true if the ith bit is
// set. Otherwise return false
return ((num & (1 `<<` i)) != 0);
}
// Function to set the ith bit of the
// given number num
int setBit(int num, int i)
{
// Sets the ith bit and return
// the updated value
return num | (1 `<<` i);
}
// Function to clear the ith bit of
// the given number num
int clearBit(int num, int i)
{
// Create the mask for the ith
// bit unset
int mask = ~(1 `<<` i);
// Return the updated value
return num & mask;
}
// Driver Code
int main()
{
// Given number N
int N = 70;
cout << "The bit at the 3rd position from LSB is: "
<< (getBit(N, 3) ? '1' : '0')
<< endl;
cout << "The value of the given number "
<< "after setting the bit at "
<< "LSB is: "
<< setBit(N, 0) << endl;
cout << "The value of the given number "
<< "after clearing the bit at "
<< "LSB is: "
<< clearBit(N, 0) << endl;
return 0;
}
``` this is write code for you because you used onlyBelow is the program that implements the above functionalities:
// C++ program to implement all the
// above functionalities
#include <bits/stdc++.h>
using namespace std;
// Function to get the bit at the
// ith position
bool getBit(int num, int i)
{
// Return true if the ith bit is
// set. Otherwise return false
return ((num & (1 `<<` i)) != 0);
}
// Function to set the ith bit of the
// given number num
int setBit(int num, int i)
{
// Sets the ith bit and return
// the updated value
return num | (1 `<<` i);
}
// Function to clear the ith bit of
// the given number num
int clearBit(int num, int i)
{
// Create the mask for the ith
// bit unset
int mask = ~(1 `<<` i);
// Return the updated value
return num & mask;
}
// Driver Code
int main()
{
// Given number N
int N = 70;
cout << "The bit at the 3rd position from LSB is: "
<< (getBit(N, 3) ? '1' : '0')
<< endl;
cout << "The value of the given number "
<< "after setting the bit at "
<< "LSB is: "
<< setBit(N, 0) << endl;
cout << "The value of the given number "
<< "after clearing the bit at "
<< "LSB is: "
<< clearBit(N, 0) << endl;
return 0;
} |
Beta Was this translation helpful? Give feedback.
-
community/supports
Hello and welcome to CodeHarborHub Support! Whether you're a user, learner, contributor, or just curious about our platform, we're here to assist you every step of the way.
https://codeharborhub.github.io/community/supports
Beta Was this translation helpful? Give feedback.
All reactions