forked from dreamapplehappy/hacking-with-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtry-operator.js
75 lines (57 loc) · 1.25 KB
/
try-operator.js
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
//var a = 64.3;
//var isPowerOf2 = a && !(a & (a - 1));
//var b = 7.2;
//var max = a ^ ( (a ^ b) & -(a < b) );
//var min = b ^ ( (a ^ b) & -(a < b) );
//console.log(max, min);
//
//var c = 3.1415926;
//var d = c | 0;
//var e = ~~c;
//console.log(d === Math.floor(c), e === d);
//var bgColor = {
// r: 234,
// g: 36,
// b: 122
//};
//var hexColor = RGB2HEX(bgColor.r, bgColor.g, bgColor.b);
//console.log(hexColor); // #ea247a
//
//function RGB2HEX(r, g, b) {
// return '#' + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).substr(1);
//}
//
//a ^= b;
//b ^= a;
//a ^= b;
//console.log(a, b); // 6, 3
//function compare(a, b) {
// if(a > b) {
// return a;
// }
// else {
// return b;
// }
//}
//function compare(a, b) {
// return a > b ? a : b;
//}
//console.log(isPowerOf2);
//var n = 3;
//var result = 1 << n;
//console.log(result); // 8
//console.log(result === Math.pow(2, 3)); // true
//console.log(0x1);
var a = 1.1;
var b = 2.2;
function compare(a, b) {
return a > b ? a: b;
}
console.time('use func');
console.log(compare(a, b));
console.timeEnd('use func');
console.time('use bitwise operators');
a = a | 0;
b = b | 0;
console.log(a ^ ( (a ^ b) & -(a < b) ));
console.timeEnd('use bitwise operators');