-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion2.java
More file actions
42 lines (37 loc) · 836 Bytes
/
Question2.java
File metadata and controls
42 lines (37 loc) · 836 Bytes
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
import java.util.*;
// to find ith bit is set bit or not.
public class Question2 {
public static boolean kthBit(int n, int k) {
int count = 0, i = 1, a =1;
while(n>1) {
a = n%2;
n /=2;
if(count == k) {
break;
}
count++ ;
}
// while(i <= (count-k)) {
// a = n%2;
// if(i == (count-k)) {
// break;
// }
// n /= 2;
// i++;
// }
if(a == 1) {
return true;
} else {
return false;
}
public static void main(String args[]) {
Scanner sc =new Scanner(System.in);
System.out.println("Enter the number:");
int n = sc.nextInt();
System.out.println("Enter the kth bit from LSB:");
int k = sc.nextInt();
boolean bit = kthBit(n,k);
System.out.println(bit);
}
}
}