-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinNum.java
68 lines (54 loc) · 907 Bytes
/
BinNum.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
package hexaVersNum;
public class BinNum {
private int dec;
private int[] bin;
public BinNum(int n){
dec=n;
bin=toBin();
}
public BinNum(int[] bin){
this.bin=bin;
dec=toDec();
}
int toDec(){
int[] binaire = bin;
int coeff,nbre=0;
for(int i=0;i<16;i++){
coeff = binaire[15-i];
int n=i;
int base=1;
while(n>0){
base*=2;
n--;
}
nbre +=coeff*base;
}
return nbre;
}
public int[] toBin(){
int nombre = dec;
int[] binaire = new int[16];
for(int i=15;i>=0;i--){
binaire[i]=nombre%2;
nombre/=2;
}
return binaire;
}
public int[] getBin(){
return bin;
}
public int distHam(BinNum b2){
int distance=0;
for(int i=0;i<bin.length;i++){
if(bin[i]!=b2.getBin()[i])
distance++;
}
return distance;
}
public String toString(){
String s="";
for(int i=0;i<16;i++)
s+=bin[i];
return s;
}
}