arrays - Fast Range Detection Algorithm -


i have array of 8 elements: bin[8]. bin represents range container: receive number n, such 0 <= n <= 255.

  1. if n < 32 ==> bin[0] += 1
  2. else if 32 <= n < 64 ==> bin[1] += 1
  3. ... 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

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -