Skip to content

Commit 6382069

Browse files
authored
Update print-binary.js
1 parent 654d37d commit 6382069

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

print-binary.js

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
const toBinaryRecursive = (num) => {
22
return num < 2 ? num + "" : toBinaryRecursive(num >> 1) + num % 2;
33
}
4+
5+
const toBinary = (num) => {
6+
7+
let bits = [], mask = 1;
8+
9+
while (mask <= num) {
10+
bits.unshift(num & mask ? 1 : 0);
11+
mask <<= 1;
12+
}
13+
return bits.join("");
14+
}

0 commit comments

Comments
 (0)