javascript - How to get percent from an binary int, with every bit representing x percent? -


i have binary bitmap 10bits. every bit represents 10%. there simple math function sum of percent bitmap?

sample

0000000000 = 0%  0000000001 = 10% 1000000000 = 10% 0000100000 = 10%  1000000001 = 20% 0000000011 = 20% 0000110000 = 20% 0010000010 = 20%  1010000010 = 30% 

be aware example of how bits activated. number have int such 0,1 1023.

you don't have use loop. don't have math. this:

var number = 1000100010;  alert(number.tostring().split("1").length - 1);    //a little more deep:  var number2 = 1100100000;  alert((number2.tostring().split("1").length - 1) * 10 + "%");


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -