-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbit-mainpulation.py
66 lines (53 loc) · 1.33 KB
/
bit-mainpulation.py
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
def isPowerOfTwo(x):
return (x != 0) and ((x & (x - 1)))
print(isPowerOfTwo(2))
a = 8
print(bin(a), a)
print(bin(a - 1), a - 1)
print(a & (a - 1))
print(2 & 1, 3 & 1)
# for i in range(6):
# print(i, i& i-1)
# bool isPowerOfTwo(int x)
# {
# // x will check if x == 0 and !(x & (x - 1)) will check if x is a power of 2 or not
# return (x && !(x & (x - 1)));
# }
# int count_one (int n)
# {
# while( n )
# {
# n = n&(n-1);
# count++;
# }
# return count;
# }
# void possibleSubsets(char A[], int N)
# {
# for(int i = 0;i < (1 << N); ++i)
# {
# for(int j = 0;j < N;++j)
# if(i & (1 << j))
# cout << A[j] << ‘ ‘;
# cout << endl;
# }
# }
# long largest_power(long N)
# {
# //changing all right side bits to 1.
# N = N| (N>>1);
# N = N| (N>>2);
# N = N| (N>>4);
# N = N| (N>>8);
# //as now the number is 2 * x-1, where x is required answer, so adding 1 and dividing it by
# 2.
# return (N+1)>>1;
# }
def possible_subsets(arr):
for i in range(1 << len(arr)):
for j in range(len(arr)):
if i & (1 << j):
print(arr[j], end=" ")
print("\n")
print("possible subsets")
possible_subsets([1, 2, 3])