Let’s say I have two integers with the following binary representations:
01101010
00110101
And now I want to copy the last 3 bits from the first integer over the second one so that it becomes
00110010
What’s the easiest way to do that?
(Actually, my goal is to shift the all the X+1 bits to the right one, essentially deleting the Xth bit, and keeping the X-1 bits the same — in this case, X is 4)
The “why?”:
You have a bunch of flags,
1 = 'permission x'
2 = 'permission y'
4 = 'permission z'
8 = 'permission w'
You decide that that “permission y” is no longer needed in your program, and thus shift z and w up a position (making them 2 and 4 respectively). However, now you need to update all the values in your database…. (what formula do you use?)
Depending on your version of python, the way you express binary literals changes, see this question for the details.
I’m using 2.5.2, so I used this:
Details:
I didn’t understand your goal in the last sentence, so I don’t know how to address that 🙂