Bit twiddle help: Expanding bits to follow a given bitmask
问题 I'm interested in a fast method for "expanding bits," which can be defined as the following: Let B be a binary number with n bits, i.e. B \in {0,1}^ n Let P be the position of all 1/true bits in B , i.e. 1 << p[i] & B == 1 , and | P |= k For another given number, A \in {0,1}^ k , let Ap be the bit-expanded form of A given B , such that Ap[j] == A[j] << p[j] . The result of the "bit expansion" is Ap . A couple examples: Given B : 00 1 0 111 0, A : 0110, then Ap should be 00 0 0 110 0 Given B :