-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitManipulation.java
More file actions
243 lines (216 loc) · 8.36 KB
/
BitManipulation.java
File metadata and controls
243 lines (216 loc) · 8.36 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package TimeAndSpace.DSA;
import java.util.Scanner;
public class BitManipulation {
// Check if a number is even or odd using bitwise operators
public static void checkEvenOdd(int num) {
if ((num & 1) == 0) {
System.out.println(num + " is even.");
} else {
System.out.println(num + " is odd.");
}
}
// Count the number of set bits (1s) in a number using Brian Kernighan's algorithm
public static void countSetBits(int num) {
int count = 0;
while (num > 0) {
num = num & (num - 1); // Remove the rightmost set bit
count++;
}
System.out.println("Number of set bits: " + count);
}
// Flip all the bits of a number
public static void flipBits(int num) {
int flipped = ~num;
System.out.println("Flipped bits: " + flipped);
}
// Check if a number is a power of 2
public static void checkPowerOfTwo(int num) {
if ((num & (num - 1)) == 0) {
System.out.println(num + " is a power of 2.");
} else {
System.out.println(num + " is not a power of 2.");
}
}
// Toggle the nth bit of a number
public static void toggleBit(int num, int n) {
int toggled = num ^ (1 << n);
System.out.println("Number after toggling bit " + n + ": " + toggled);
}
// Set the nth bit of a number
public static void setBit(int num, int n) {
int set = num | (1 << n);
System.out.println("Number after setting bit " + n + ": " + set);
}
// Clear the nth bit of a number
public static void clearBit(int num, int n) {
int cleared = num & ~(1 << n);
System.out.println("Number after clearing bit " + n + ": " + cleared);
}
// Swap two numbers using XOR
public static void swapNumbers(int a, int b) {
a = a ^ b;
b = a ^ b;
a = a ^ b;
System.out.println("Swapped numbers: a = " + a + ", b = " + b);
}
// Get the bit at the nth position
public static void getBitAtPosition(int num, int n) {
int bit = (num >> n) & 1;
System.out.println("Bit at position " + n + ": " + bit);
}
// Find the most significant set bit
public static void mostSignificantBit(int num) {
int msb = 31 - Integer.numberOfLeadingZeros(num);
System.out.println("Most significant bit is at position: " + msb);
}
// Find the rightmost set bit
public static void findRightmostSetBit(int num) {
int rightmostSetBit = num & -num;
System.out.println("Rightmost set bit: " + rightmostSetBit);
}
// Clear the rightmost set bit
public static void clearRightmostSetBit(int num) {
int cleared = num & (num - 1);
System.out.println("After clearing the rightmost set bit: " + cleared);
}
// Left shift a number by n bits
public static void leftShift(int num, int n) {
int shifted = num << n;
System.out.println("After left shifting by " + n + " bits: " + shifted);
}
// Right shift a number by n bits
public static void rightShift(int num, int n) {
int shifted = num >> n;
System.out.println("After right shifting by " + n + " bits: " + shifted);
}
// Rotate the bits of a number to the left by n bits
public static void rotateLeft(int num, int n) {
int result = (num << n) | (num >> (32 - n));
System.out.println("After rotating left by " + n + " bits: " + result);
}
// Rotate the bits of a number to the right by n bits
public static void rotateRight(int num, int n) {
int result = (num >> n) | (num << (32 - n));
System.out.println("After rotating right by " + n + " bits: " + result);
}
// Check if two numbers have opposite signs
public static void checkOppositeSigns(int num1, int num2) {
if ((num1 ^ num2) < 0) {
System.out.println("The numbers have opposite signs.");
} else {
System.out.println("The numbers have the same sign.");
}
}
// Find the number of trailing zeros in the binary representation of a number
public static void countTrailingZeros(int num) {
if (num == 0) {
System.out.println("Number has infinite trailing zeros.");
} else {
int count = 0;
while ((num & 1) == 0) {
count++;
num >>= 1;
}
System.out.println("Trailing zeros: " + count);
}
}
// Main method to test the bit manipulation operations
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number:");
int num = sc.nextInt();
System.out.println("Enter the position of the bit (for set/clear/toggle operations):");
int n = sc.nextInt();
System.out.println("Select an option:");
System.out.println("1. Check Even/Odd");
System.out.println("2. Count Set Bits");
System.out.println("3. Flip Bits");
System.out.println("4. Check Power of 2");
System.out.println("5. Toggle Bit");
System.out.println("6. Set Bit");
System.out.println("7. Clear Bit");
System.out.println("8. Swap Numbers");
System.out.println("9. Get Bit at Position");
System.out.println("10. Find Most Significant Bit");
System.out.println("11. Find Rightmost Set Bit");
System.out.println("12. Clear Rightmost Set Bit");
System.out.println("13. Left Shift");
System.out.println("14. Right Shift");
System.out.println("15. Rotate Left");
System.out.println("16. Rotate Right");
System.out.println("17. Check if Two Numbers Have Opposite Signs");
System.out.println("18. Count Trailing Zeros");
int option = sc.nextInt();
switch (option) {
case 1:
checkEvenOdd(num);
break;
case 2:
countSetBits(num);
break;
case 3:
flipBits(num);
break;
case 4:
checkPowerOfTwo(num);
break;
case 5:
toggleBit(num, n);
break;
case 6:
setBit(num, n);
break;
case 7:
clearBit(num, n);
break;
case 8:
System.out.println("Enter another number to swap:");
int b = sc.nextInt();
swapNumbers(num, b);
break;
case 9:
getBitAtPosition(num, n);
break;
case 10:
mostSignificantBit(num);
break;
case 11:
findRightmostSetBit(num);
break;
case 12:
clearRightmostSetBit(num);
break;
case 13:
System.out.println("Enter number of positions to left shift:");
int leftShiftBy = sc.nextInt();
leftShift(num, leftShiftBy);
break;
case 14:
System.out.println("Enter number of positions to right shift:");
int rightShiftBy = sc.nextInt();
rightShift(num, rightShiftBy);
break;
case 15:
System.out.println("Enter number of positions to rotate left:");
int rotateLeftBy = sc.nextInt();
rotateLeft(num, rotateLeftBy);
break;
case 16:
System.out.println("Enter number of positions to rotate right:");
int rotateRightBy = sc.nextInt();
rotateRight(num, rotateRightBy);
break;
case 17:
System.out.println("Enter another number to check signs:");
int num2 = sc.nextInt();
checkOppositeSigns(num, num2);
break;
case 18:
countTrailingZeros(num);
break;
default:
System.out.println("Invalid option!");
}
sc.close();
}
}