Skip to content

Commit f3a388c

Browse files
Create K-th_bit_is_set_or_not.cpp
1 parent b5160a6 commit f3a388c

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Given a number N and a bit number K, check if the Kth bit of N is set or not.Position of set bit '1' should be indexed starting with 0 from the LSB side in the binary representation of the number.
2+
3+
// Example
4+
// Input: N = 4, K = 0
5+
// Output: No
6+
7+
// Time Complexity: O(LogN)
8+
// Auxiliary Space: O(1)
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
bool checkKthBit(int n, int k)
14+
{
15+
if((1<<k)&n)
16+
return true;
17+
else
18+
return false;
19+
}
20+
int main()
21+
{
22+
int t;
23+
cin>>t;//taking testcases
24+
while(t--)
25+
{
26+
long long n;
27+
cin>>n;//input n
28+
int k;
29+
cin>>k;//bit number k
30+
31+
if(checkKthBit(n, k))
32+
cout << "Yes" << endl;
33+
else
34+
cout << "No" << endl;
35+
}
36+
return 0;
37+
}

0 commit comments

Comments
 (0)