-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPRactice1.java
More file actions
120 lines (90 loc) · 2.46 KB
/
PRactice1.java
File metadata and controls
120 lines (90 loc) · 2.46 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package BitManipulation;
import java.util.ArrayList;
import java.util.Scanner;
public class PRactice1 {
public static void swap(int a , int b){
a = a ^ b;
b = a ^ b;
a = b ^ a;
System.out.println(a);
System.out.println(b);
}
public static boolean isSet(int n , int k){
if((n & 1 << k) != 0){
return true;
}
return false;
}
public static ArrayList<ArrayList<Integer>> generate(int arr[] , int n){
ArrayList<ArrayList<Integer>> ans = new ArrayList<>();
if(n == 0){
return ans;
}
int subsets = 1 << n;
for(int i = 0; i < subsets; i++){
ArrayList<Integer> row = new ArrayList<>();
for(int j = 0; j < n ; j++){
if((i & (1 << j)) != 0){
row.add(arr[j]);
}
}
ans.add(row);
}
return ans;
}
public static boolean isPower(int n){
if((n & n - 1) == 0){
return true;
}
return false;
}
public static void setKthbit(int n, int k){
int newn = n | (1 << k);
System.out.println(newn);
}
public static void clearBit(int n , int k){
int newn = n & ~(1 << k);
System.out.println(newn);
}
public static void countBits(int n){
int count = 0;
while(n > 0){
if((n & 1) == 1){
count++;
}
n = n >> 1;
}
System.out.println(count);
}
public static void countBits2(int n){
int count = 0;
while(n > 0){
n = (n & n - 1);
count++;
}
System.out.println(count);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int arr[] = new int[n];
for(int i = 0; i < n; i++){
arr[i] = sc.nextInt();
}
swap(n, m);
System.out.println(isSet(n, m));
ArrayList<ArrayList<Integer>> ans = generate(arr,n);
for(int i = 0; i < ans.size(); i++){
for(int it : ans.get(i)){
System.out.print(it);
}
System.out.println();
}
System.out.println(isPower(n));
setKthbit(n, m);
clearBit(n, m);
countBits(n);
countBits2(n);
}
}