-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecToBi.java
More file actions
32 lines (28 loc) · 848 Bytes
/
decToBi.java
File metadata and controls
32 lines (28 loc) · 848 Bytes
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
package Class09_BitOperations;
public class decToBi {
/**
* Converts an integer to a 32-bit binary string
* @param number
* The number to convert
* @param groupSize
* The number of bits in a group
* @return
* The 32-bit long bit string
*/
public String intToString(int number, int groupSize) {
StringBuilder result = new StringBuilder();
for(int i = 31; i >= 0 ; i--) {
int mask = 1 << i;
result.append((number & mask) != 0 ? "1" : "0");
if (i % groupSize == 0)
result.append(" ");
}
result.replace(result.length() - 1, result.length(), "");
return result.toString();
}
public static void main(String[] args) {
decToBi sol = new decToBi();
System.out.println(sol.intToString(5, 4));
System.out.println(sol.intToString(11, 4));
}
}