[Tutor] testing the bits of a binary number
Phil
phillor9 at gmail.com
Tue Apr 26 06:37:26 EDT 2022
I've been experimenting with a simpler way of testing the bits of a
number without success.
Say I have a number represented as 0b00001001 and I want to test which
bits are set. I solved this originally as I would in assembly language
by anding a bit mask and the number. The number and the bit mask are
shifter left followed by another anding operation until the most
significant bit is reached.
This does work but it seems unnecessarily complex when using a high
level language. An Internet search shows that similar questions have
been asked and answerers given but I haven't been able to adapt those
answerers to my problem. Can this problem be solved in a Pythonic way?
--
Regards,
Phil
More information about the Tutor
mailing list