Skip to content

Commit

Permalink
write function to get ith bit in cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
justanindieguy committed Jan 24, 2023
1 parent 81b79fb commit db436e0
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Bit-manipulationTechniques/GetIthBit/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Write a function to:
* - get ith bit
*/
#include <iostream>

using namespace std;

int getIthBit(int n, int i)
{
int mask = 1 << i;
return (n & mask) > 0 ? 1 : 0;
}

int main()
{
int i, n = 5;
cin >> i;

cout << "Bit at " << i << "th position: " << getIthBit(n, i) << endl;

return 0;
}

0 comments on commit db436e0

Please sign in to comment.