Bitwise operations in a class inheriting int

I inherited from int because I wanted to implement a simple interface for bitwise operations. Due to the immutability of int, I have to use integer member functions such as int.__and__, ....

class Bitset(int)
    ...
    def __setitem__(self, index, value):
        if value:
            self.__ior__(1 << int(index))
        else:
            self.__iand__(~(1 << int(index)))

In one of my member functions, I want to use the |=and functions &=, but the integer has no member functions __ior__and __iand__. So my question is: how can I solve this problem?

Edit:

I do not want to simplify binary operations, I would like to manipulate the bits of an integer. For example.

a = Bitset(0)
a[0]
>>>0
a[0] = 1
a[0]
>>>1

But I did not want to redefine all the whole operations that should work anyway. If I complete the internal integer, I have to do this. for example

a = Bitset(0)
a += 1

must work.

+3
2

int , , __setindex__(). , -, , , , . __int__() __long__() ( , ).

+1

Source: https://habr.com/ru/post/1622085/


All Articles