Last active
July 26, 2016 09:46
-
-
Save kagawagao/242f1705c8cbd8e42b35683c68d2ba75 to your computer and use it in GitHub Desktop.
将由1 2 4 8 等或运算的结果分解
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getBinary (num) { | |
var n = Math.ceil(Math.sqrt(num)) | |
var codes = [] | |
for (let i = 0; i<= n; i++) { | |
codes.push(Math.pow(2,i)) | |
} | |
var res = [] | |
for (let k = codes.length -1; k >= 0; k--) { | |
if (num >= codes[k]) { | |
num = num - codes[k] | |
res.push(codes[k]) | |
} | |
} | |
return res | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment