Skip to content
Advertisement

How does python perform bitwise operations to store three values ​in an int?

For example, if I have an int with a length of 32 bits, how do I store value A in the first 16 bits, then store value B in bits 17-24, and value C in bits 25-32? And how can I get these three values ​​out of an int?

Advertisement

Answer

Assuming that your values fit in the assigned bit counts, it’s just a question of shifting them to make an int, and then unshifting and masking to get them back:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement