Skip to content

Instantly share code, notes, and snippets.

@kagawagao
Last active July 26, 2016 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kagawagao/242f1705c8cbd8e42b35683c68d2ba75 to your computer and use it in GitHub Desktop.
Save kagawagao/242f1705c8cbd8e42b35683c68d2ba75 to your computer and use it in GitHub Desktop.
将由1 2 4 8 等或运算的结果分解
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