Mind the set of positive integers from 0 til L exclusive. For any positive integer n up to log2(K), can split the set in 2^n consecutive subsets of equal length. For example, for L = 256, n = 3, we'd get 2^3, or 8, subsets: {0..31}, {32..63}, {64..95}, {96..127}, {128..160}, {160..191}, {192..223}, {224..255}.
Let f(L,n,i) : Nat -> Nat -> Nat -> (Nat,Nat) return, given an arbitrary L and n, the subset that some i is contained in. For example, for f(256,3,100) = (96,127), because, if we divide the 0..255 range in 2^3 subsets, then number 100 is contained in the subset ranging from 96 to 127.
My question is, what is a fast implementation of f? I wonder if it is possible to do so in constant time, with just a few bitwise operations.
Quite obviously, just compute the length of each subset (
l), divideibyl, and take the floork. The desired range goes froml*ktol*(k+1)-1. In JavaScript:(TODO: convert to C for the sake of this answer.)