How to count the number of 1 bits in an integer.
So say you have the binary number 11100000
. Basically there are 3 boolean flags at the beginning. The corresponding decimal notation for this is 224
. Wondering how to take that integer and loop through it somehow to add the number of 1s that it starts with. Something like this:
var int = 224
var n = 8
var i = 0
var total = 0
while (i++ < n) {
if (int.getBitAt(i) == 1) {
total++
} else {
break
}
}
I've never really dealt with bits so not sure how to accomplish this in an optimal way (i.e. without converting it to a string '11100000'
or other unoptimal ways.