Say I have a binary number 01100 = 12, what’s an efficient way to iterate starting with this number such that the bits already set to one remain set?
In this example the sequence would go
01100 = 12
01101 = 13
01110 = 14
01111 = 15
11100 = 28
11101 = 29
11110 = 30
11111 = 31
Save the original value. Then every time you increment the dynamic value, or it with the original. In Java: