arrays - Fast Range Detection Algorithm -
i have array of 8 elements: bin[8]
. bin represents range container: receive number n
, such 0 <= n <= 255
.
- if
n < 32
==>bin[0] += 1
- else if
32 <= n < 64
==>bin[1] += 1
- ... etc.
i want fast solution not require if-else
directive, have multiple bins handle.
i using java, solution in programming language accepted.
thank you.
ensure number n indeed 0 <= n <= 255, simply:
bin[n/32]++;
edit: poster mentioned shifting right 5 bits. work too, feel dividing 32 shows intent cleaner , modern compiler optimize division away bitshift if it's more efficient on platform you're targeting anyway.
Comments
Post a Comment