-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitManipulation.java
More file actions
166 lines (128 loc) · 3.67 KB
/
BitManipulation.java
File metadata and controls
166 lines (128 loc) · 3.67 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import java.util.*;
public class BitManipulation {
// Odd or Even
public static void oddOrEven(int n){
int bitMask = 1;
if((n & bitMask) == 0){
System.out.println("Even");
} else {
System.out.println("Odd");
}
}
// Get Int Bit
public static int getIthBit(int n, int i){
int bitMask = 1<<i;
if((n & bitMask) == 0) {
return 0;
} else {
return 1;
}
}
// Set Ith Bit
public static int setIthBit(int n, int i){
int bitMask = 1<<i;
return n | bitMask;
}
// Clear Ith Bit
public static int ClearIthBit(int n, int i){
int bitMask = ~(1<<i);
return n & bitMask;
}
// Update Ith Bit
public static int updateIthBit(int n, int i, int newBit){
if(newBit == 0){
return ClearIthBit(n, i);
} else {
return setIthBit(n, i);
}
// Clear Ith Bit
// n = ClearIthBit(n, i);
// int bitMask = newBit<<i;
// return n | bitMask;
}
// Clear I Bits
public static int clearIBits(int n,int i){
int bitMask = (~0)<<i;
return n & bitMask;
}
// Clear Bits In Range
public static int clearBitsInRange(int n, int i, int j){
int a = (~0)<<(j+1);
int b = (1<<i) - 1;
int bitMask = a | b;
return n & bitMask;
}
// Check if Power of Two
public static boolean isPowerOfTwo(int n) {
if((n&(n-1)) == 0 && n != 0 && n > 0) {
return true;
}else {
return false;
}
}
// Count Set Bits
public static int countSetBits(int n) {
int count = 0;
while (n > 0) {
count += (n & 1);
n = n >> 1;
}
return count;
}
// Fast Exponentiation
public static int fastExpo(int a, int n){
int ans = 1;
while(n > 0){
if((n & 1) != 0){
ans = ans * a;
}
a = a * a;
n = n>>1;
}
return ans;
}
// Swapping without temp variable
public static void swap(int a, int b){
System.out.println("Before Swap: a = " + a + " b = " + b);
a = a ^ b;
b = a ^ b; // b = (a ^ b) ^ b = a
a = a ^ b; // a = (a ^ b) ^ a = b
System.out.println("After Swap: a = " + a + " b = " + b);
}
public static void main(String[] args) {
for(char ch = 'A'; ch <= 'Z'; ch++){
System.out.print((char)(ch | ' '));
}
// Prints
// int x = 6;
// System.out.println(x + " + " + 1 + " is " + -~x);
// x = -4;
// System.out.println(x + " + " + 1 + " is " + -~x);
// x = 0;
// System.out.println(x + " + " + 1 + " is " + -~x);
// Swapping without temp variable
// swap(5, 10);
// Fast Exponentiation
// System.out.println(fastExpo(3, 5));
// Count Set Bits
// System.out.println(countSetBits(10));
// Check if Power of Two
// System.out.println(isPowerOfTwo(2));
// Clear Bits In Range
// System.out.println(clearBitsInRange(10, 2, 4));
// Clear I Bits
// System.out.println(clearIBits(15, 2));
// Update Ith Bit
// System.out.println(updateIthBit(10, 2, 0));
// Clear Ith Bit
// System.out.println(ClearIthBit(10, 1));
// Set Ith Bit
// System.out.println(setIthBit(10, 2));
// Get Ith Bit
// System.out.println(getIthBit(10, 2));
// Odd or Even
// oddOrEven(3);
// oddOrEven(11);
// oddOrEven(16);
}
}