-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinaryconversion.java
96 lines (88 loc) · 2.43 KB
/
binaryconversion.java
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
public class binaryconversion {
public static String converttobin(int n) {
String res = "";
int target = n;
if (target == 0 || target == 1) {
return String.valueOf(target);
}
while (target >= 1) {
if (target % 2 == 0) {
res = "0" + res;
} else {
res = "1" + res;
}
target = target / 2;
}
return res;
}
public static double converttodecimal(String bin) {
double sum = 0;
String target = bin;
int count = 0;
if (bin == "0") {
return 0;
}
for (int i = target.length() - 1; i >= 0; i--) {
sum = sum + Math.pow(2 * (Character.getNumericValue(target.charAt(i))), count);
count++;
}
return (int) (sum);
}
public static int converttodecimal2(String bin) {
int sum = 0;
int p = 1;
for (int i = bin.length() - 1; i >= 0; i--) {
if (bin.charAt(i) == '1') {
sum = sum + p;
}
p = p * 2;
}
return sum;
}
public static String onescomplement(int n) {
int target = n;
String res = "";
while (target >= 1) {
if (target % 2 == 0) {
res = "0" + res;
} else {
res = "1" + res;
}
target = target / 2;
}
return res;
}
public static int andop(Character a, Character b) {
if (a == '1' && b == '1') {
return 1;
} else {
return 0;
}
}
public static int orop(Character a, Character b) {
if (a == '1' || b == '1') {
return 1;
} else {
return 0;
}
}
public static void swap(int a, int b) {
a = a + b;
b = a - b;
a = a - b;
System.out.println(a + " - a");
System.out.println(b + " - b");
}
public static boolean setornot(int n, int t) {
return (n & (1 << t)) != 0;
}
public static void main(String[] args) {
// System.out.println(converttobin(13));
// System.out.println(converttodecimal2("1101001111"));
// System.out.println(onescomplement(13));
// System.out.println(andop('1', '0'));
// System.out.println(orop('1', '0'));
System.out.println(setornot(4, 0));
// swap(5, 8);
}
}