class example(Enum):
x1 = 0
x2 = 1
x3 = 2
x4 = 4
x5 = 8
x6 = 16
...
If I have the number 12, how do I know that it matches x4 + x5
?
The x-numbers are always power of 2
class example(Enum):
x1 = 0
x2 = 1
x3 = 2
x4 = 4
x5 = 8
x6 = 16
...
If I have the number 12, how do I know that it matches x4 + x5
?
The x-numbers are always power of 2
It seems as if you are trying to convert denary
to binary
. It is a good exercise to try and do this yourself, but storing the powers of 2
in different variables
is not a good idea as it will not scale when trying to convert larger numbers...
So, yes, you could write your own algorithm for doing this, but if you aren't fussed, just use Python's built-in bin
function.
From the docs:
Convert an integer number to a binary string. The result is a valid Python expression. If x is not a Python int object, it has to define an index() method that returns an integer.
So as an example, if you wanted to know the binary
value of 12
, you could do:
bin(12)
which would give:
'0b1100'
Hope this helps!