Decomposing a value into results of powers of two
问题 Is it possible to get the integers that, being results of powers of two, forms a value? Example: 129 resolves [1, 128] 77 resolves [1, 4, 8, 64] I already thought about using Math.log and doing also a foreach with a bitwise comparator. Is any other more beautiful solution? 回答1: The easiest way is to use a single bit value, starting with 1 and shift that bit 'left' until its value is greater than the value to check, comparing each bit step bitwise with the value. The bits that are set can be