-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctions_bin_to_decimal.html
129 lines (110 loc) · 3.42 KB
/
Functions_bin_to_decimal.html
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<body>
<script>
var binStr = "11011010";
//算法一
function binToDecimal(bin){
// 将二进制字符串分割成单数字字符数组
var binStrArray = bin.split(''); // 1,1,0,1,1,0,1,0
var decimal = 0; //保存十进制的最终结果
var temp = 1; //临时保存每一位转换结果
for (var i= binStrArray.length - 1, j = 0 ; i > 0 ; i-- , j++ ){ // i等于(二进制)2的幂,j是数组索引值
// 从二进制数的最高有效位向最低有效位逐个迭代
if(binStrArray[j] == 1){ // 如果第j个元素等于1,则需要计算i个2相乘(即2的i次幂)
for (var k = i; k > 0 ; k-- ){ //
temp *= 2;
}
decimal += temp; //累加每一位的转换结果
temp = 1; // 当前位转换完毕,重置临时变量
}
}
alert(decimal);
}
//算法二
function binToDecimal2(bin){
var decimal = 0, temp = 1, powerArray = [],pos=bin.indexOf('1');digits = bin.length;
//通过while循环得到二进制数值中所有1对应的幂的数组
while (pos > -1 ){
powerArray.push(digits - pos - 1); //二进制数的位数减1再减索引值[7,6,4,3,1]
pos = bin.indexOf('1', pos +1);
}
for (var i = 0; i < powerArray.length ; i++ ){
var j = powerArray[i];
for (var k = 0; k<j ; k++ ){
temp *= 2;
}
decimal += temp; temp = 1;
}
alert(decimal);
}
//算法三:算法二的优化
function binToDecimal3(bin){
var decimal = 0, temp = 1, pos=bin.indexOf('1');digits = bin.length;
//通过while循环得到二进制数值中所有1对应的幂的数组
while (pos > -1 ){
var power = digits - pos - 1; //二进制数的位数减1再减索引值等于幂
for (var i = 0; i < power ; i++ ){
temp *= 2;
}
decimal += temp; temp = 1;
pos = bin.indexOf('1', pos +1);
}
alert(decimal);
}
//算法四
function binToDecimal4(bin){
var decimal = 0, temp = 1, digits = bin.length; currentBit = bin.slice(0,1);
while (digits > 0){
if (currentBit == 1){
var power = digits - 1; //二进制数的位数减1即等于幂
for (var i = 0; i < power ; i++ ){
temp *= 2;
}
decimal += temp; temp = 1;
}
bin = bin.slice(1); currentBit = bin.slice(0,1);digits = bin.length;
}
alert(decimal);
}
//算法五:算法四的变体——用数组代替字符串
function binToDecimal5(bin){
var decimal = 0, temp = 1, binStrArray = bin.split(''), currentBit = binStrArray.shift(), digits = binStrArray.length;;
while (digits > 0){
if (currentBit == 1){
var power = digits; //数组长度即等于幂
for (var i = 0; i < power ; i++ ){
temp *= 2;
}
decimal += temp; temp = 1;
}
currentBit = binStrArray.shift(); digits = binStrArray.length;
}
alert(decimal);
}
//算法六:算法五的变体——使用pop()方法
function binToDecimal6(bin){
var decimal = 0, temp = 1, binStrArray = bin.split(''), currentBit = binStrArray.shift(), digits = binStrArray.length;;
while (digits > 0){
if (currentBit == 1){
var power = digits; //数组长度即等于幂
for (var i = 0; i < power ; i++ ){
temp *= 2;
}
decimal += temp; temp = 1;
}
currentBit = binStrArray.shift(); digits = binStrArray.length;
}
alert(decimal);
}
binToDecimal6(binStr); // 218
</script>
</body>
</html>