Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,959 questions

51,901 answers

573 users

How to create a bitset in Python

1 Answer

0 votes
class BitSet:
    def __init__(self, size):
        self.size = size
        self.bits = [False] * size

    def set(self, index):
        if 0 <= index < self.size:
            self.bits[index] = True

    def clear(self, index):
        if 0 <= index < self.size:
            self.bits[index] = False

    def get(self, index):
        if 0 <= index < self.size:
            return self.bits[index]
        return False

    def __str__(self):
        return ''.join(['1' if bit else '0' for bit in reversed(self.bits)])

bitset = BitSet(16)

bitset.set(3)
bitset.set(5)
print(bitset)

bitset.clear(3)
print(bitset)



'''
run:

0000000000101000
0000000000100000

'''

 



answered May 7, 2025 by avibootz

Related questions

1 answer 37 views
1 answer 106 views
106 views asked May 7, 2025 by avibootz
1 answer 99 views
99 views asked May 7, 2025 by avibootz
1 answer 103 views
103 views asked May 7, 2025 by avibootz
2 answers 107 views
2 answers 83 views
2 answers 94 views
...