-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExample19.java
More file actions
84 lines (63 loc) · 2.16 KB
/
Example19.java
File metadata and controls
84 lines (63 loc) · 2.16 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
package applications.algorithms;
/** Category: Algorithms
* ID: Example19
* Description: Find binary representation of an integer
* Taken From:
* Details:
* TODO
*/
public class Example19 {
public static String reverse(String s){
int middle = s.length() >> 1;
StringBuilder builder = new StringBuilder(s);
for(int i=0; i<middle; ++i){
char tmp = s.charAt(i);
builder.setCharAt(i, s.charAt(s.length() -i -1));
builder.setCharAt(s.length() -i -1, tmp);
}
return builder.toString();
}
public static String complement(String s){
StringBuilder builder = new StringBuilder(s);
for(int i=0; i<s.length(); ++i){
if(s.charAt(i) == '0'){
builder.setCharAt(i, '1');
}
else{
builder.setCharAt(i, '0');
}
}
return builder.toString();
}
public static String addOne(String s){
StringBuilder builder = new StringBuilder(s);
builder.setCharAt(s.length()-1, '1');
return builder.toString();
}
public static String binaryRepresentation(int n){
boolean negative = false;
if( n < 0){
n = Math.abs(n);
negative = true;
}
StringBuilder builder = new StringBuilder();
while( n != 0){
int i = n % 2;
builder.append(i);
n /= 2;
}
if(negative){
// get the complement
String s = Example19.complement(Example19.reverse(builder.toString()));
// add 1 and return the string
return Example19.addOne(s);
}
return Example19.reverse(builder.toString());
}
public static void main(String[] args){
System.out.println("Binary representation of 37: "+Example19.binaryRepresentation(37));
System.out.println("Binary representation of 13: "+Example19.binaryRepresentation(13));
System.out.println("Binary representation of 17: "+Example19.binaryRepresentation(17));
System.out.println("Binary representation of -17: "+Example19.binaryRepresentation(-17));
}
}