Skip to content

Commit d79782c

Browse files
authored
Update 1680-concatenation-of-consecutive-binary-numbers.js
1 parent 9638b9a commit d79782c

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

1680-concatenation-of-consecutive-binary-numbers.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,22 @@ const concatenatedBinary = function(n) {
1414
function dec2bin(dec){
1515
return (dec >>> 0).toString(2);
1616
}
17+
18+
// another
19+
20+
/**
21+
* @param {number} n
22+
* @return {number}
23+
*/
24+
const concatenatedBinary = function (n) {
25+
const mod = BigInt(1e9 + 7)
26+
let res = 0n
27+
for (let i = 1n, shift = 0n; i <= n; i++) {
28+
let singleBit = (i & (i - 1n)) == 0
29+
if (singleBit) shift++
30+
res <<= shift
31+
res += i
32+
res %= mod
33+
}
34+
return res
35+
}

0 commit comments

Comments
 (0)