Skip to content

Instantly share code, notes, and snippets.

@heat
Created April 4, 2018 12:02
Show Gist options
  • Save heat/484b4a965b61ebd0eb24eb4c94db07ab to your computer and use it in GitHub Desktop.
Save heat/484b4a965b61ebd0eb24eb4c94db07ab to your computer and use it in GitHub Desktop.
def getOneBits(n: Int): Array[Int] = {
var start: Int = 0
var total: Int = 0
val bin = n.toBinaryString
bin.foreach( c => if(c == '1') total += 1)
var positions = List(total)
for ((c, i) <- bin.zip(Stream from 1)) {
if(c == '1' && start == 0) start = i
if(c == '1') {
positions = i :: positions
}
}
positions = positions.reverse
return positions.toArray
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment