-
Notifications
You must be signed in to change notification settings - Fork 0
/
bits.java
60 lines (51 loc) · 1.01 KB
/
bits.java
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
58
59
60
package Bit_masking;
public class bits {
public static void main(String[] args) {
// TODO Auto-generated method stub
int x=16;
int next= x<<3;
System.out.println(next);
System.out.println(power(23));
System.out.println(extract_bit(5, 2));
System.out.println(right_most_set_bit(128));
System.out.println(count_set_bits(36));
}
public static boolean power(int num) {
if(num!=0 && num!=1) {
if((num & (num-1))==0) {
return true;
}
}
return false;
}
public static int extract_bit(int num, int pos) {
int mask=1;
mask= mask<<pos-1;
if((num&mask)==0) {
return 0;
}
else {
return 1;
}
}
public static int count_set_bits(int num) {
int count=0;
int mask=1;
while(num!=0) {
if((num&mask)!=0) {
count++;
}
num=num>>1;
}
return count;
}
public static int right_most_set_bit(int num) {
int pos=1;
int mask=1;
while((num&mask)==0) {
mask=mask<<1;
pos++;
}
return pos;
}
}