-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheck whether K-th bit is set or not
57 lines (44 loc) · 1.56 KB
/
Check whether K-th bit is set or not
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//Check whether K-th bit is set or not
Given a number N and a bit number K, check if Kth bit of N is set or not. A bit is called set if it is 1. Position of set bit '1' should be indexed starting with 0 from LSB side in binary representation of the number. Consider N = 4(100): 0th bit = 0, 1st bit = 0, 2nd bit = 1.
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consists of two lines. The first line of each test case contain an integer N. The second line of each test case contains an integer K.
Output:
Corresponding to each test case, print "Yes" (without quotes) if Kth bit is set else print "No" (without quotes) in a new line.
Constraints:
1 ≤ T ≤ 200
1 ≤ N ≤ 109
0 ≤ K ≤ floor(log2(N) + 1)
Example:
Input:
3
4
0
4
2
500
3
Output:
No
Yes
No
Explanation:
Testcase 1: Binary representation of 4 is 100, in which 0th bit from LSB is not set. So, answer is No.
/*package whatever //do not write package name here */
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void main (String[] args) throws NumberFormatException, IOException{
//code
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while (t-- > 0) {
// n : size of array
int n=Integer.parseInt(br.readLine());
int k=Integer.parseInt(br.readLine());
if((n&(1<<k))>=1) {
System.out.println("Yes");
}else
System.out.println("No");
}}
}