Skip to content

Commit

Permalink
write function in cpp to clear bits in range (i, j)
Browse files Browse the repository at this point in the history
  • Loading branch information
justanindieguy committed Jan 30, 2023
1 parent f358196 commit 7d84abb
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Bit-manipulationTechniques/ClearBitsInRange/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <iostream>

using namespace std;

void clearBitsInRange(int &n, int i, int j)
{
int a = -1 << (j + 1);
int b = (1 << i) - 1;
int mask = a | b;
n = n & mask;
}

int main()
{
int n = 31, i = 1, j = 3;

clearBitsInRange(n, i, j);
cout << n << endl;

return 0;
}

0 comments on commit 7d84abb

Please sign in to comment.