Python implements bitset classes similar to C ++
Bitset in C ++ and BitSet in Java are both very convenient and powerful in-place operations, which can greatly save memory and improve operation efficiency. Unfortunately, Python does not provide similar classes or modules. However, it is very easy to use the powerful capabilities of Python to implement a similar bitset class. Next we will create a BitSet class for Python. This article introduces how to handle exceptions in practical applications, such as illegal input locations. Okay, let's just move on to the code.
Python BitSet class implementation
#! /Usr/bin/env python #-*-coding: UTF-8-*-''' Copyright (C) 2015 By Thomas Hu. all rights reserved. @ author: Thomas Hu @ version: 1.0 @ created: 2015-4-24 ''' import arrayclass BitSet (object): # from low to high "00000001 00000010 00000011", the array is [1, 2, 3] def _ init _ (self, capacity): # The type of "B" is equivalent to the unsigned char of the C language, which occupies 1 byte (8 bits ), therefore, the size is set to 8 self. unit_size = 8 self. unit_count = int (capacity + sel F. unit_size-1)/self. unit_size) self. capacity = capacity self. arr = array. array ("B", [0] * self. unit_count) def any (self): # whether a bit set to 1 exists for a in self. arr: if! = 0: return True return False def all (self): # whether all bits are 1, that is, whether a bits t = (1 <self. unit_size)-1 for a in self. arr: if (a & t )! = T: return False return True def none (self): # whether all bits are 0, that is, whether the bits set to 1 exist for a in self. arr: if! = 0: return False return True def count (self): # Number of BITs set to 1 c = 0 for a in self. arr: while a> 0: if a & 1: c + = 1 a = a> 1 return c def size (self): # return self for the number of all BITs. unit_count * self. unit_size def get (self, pos): # obtain the value index = int (pos/self. unit_size) offset = (self. unit_size-(pos-index * self. unit_size)-1) % self. unit_size return (self. arr [index]> offset) & 1 def test (self, pos): # Determine whether the value of pos is 1 if self. get (pos): return True return False def set (self, pos =-1): # set the value of pos to 1. If pos is-1, then all bits are set to 1 if pos> = 0: index = int (pos/self. unit_size) offset = (self. unit_size-(pos-index * self. unit_size)-1) % self. unit_size self. arr [index] = (self. arr [index]) | (1 <offset) else: t = (1 <self. unit_size)-1 for I in range (self. unit_count): self. arr [I] = self. arr [I] | T def reset (self, pos =-1): # set the value of pos to 0. If pos is-1, then all bits are set to 0 if pos> = 0: index = int (pos/self. unit_size) offset = (self. unit_size-(pos-index * self. unit_size)-1) % self. unit_size x = (1 <offset) self. arr [index] = (self. arr [index]) & (~ X) else: for I in range (self. unit_count): self. arr [I] = 0 def flip (self, pos =-1): # reverse the value of the pos bit. If pos is-1, then, all bits are reversed. if pos> = 0: if self. get (pos): self. reset (pos) else: self. set (pos) else: for I in range (self. unit_count): self. arr [I] = ~ Self. arr [I] + (1 <self. unit_size) def binstr (self): B = "" for a in self. arr: t = bin (a) B + = "0" * (self. unit_size-len (t) + 2) + t + "," return "[" + B. replace ("0b ",""). strip (",") + "]" def show (self): return self. arrdef test (): B = BitSet (20) print "size =", B. size () print "binstr =", B. binstr (), B. show () # Set first block test B. set (0) print "B. set (0), binstr = ", B. binstr (), B. show () B. reset () B. set (1) print "B. set (1), binstr = ", B. binstr (), B. show () # Set second block test B. reset () B. set (7) print "B. set (7), binstr = ", B. binstr (), B. show () B. reset () B. set (8) print "B. set (8), binstr = ", B. binstr (), B. show () B. reset () B. set (9) print "B. set (9), binstr = ", B. binstr (), B. show () # any test print ("\ nany () test... ") B. reset () print B. any (), B. set (0) print B. any (), B. set () print B. any () # all test print ("\ nall () test... ") B. reset () print B. all (), B. set (0) print B. all (), B. set () print B. all () # none test print ("\ nnone () test... ") B. reset () print B. none (), B. set (0) print B. none (), B. set () print B. none () print ("\ nflip () test... ") B. reset () print B. binstr (), B. flip () print B. binstr () B. reset (1) print B. binstr (), B. flip () print B. binstr () if _ name _ = "_ main _": test ()
Output result of the test function
>>> size= 24binstr= [00000000,00000000,00000000] array('B', [0, 0, 0])b.set(0), binstr= [10000000,00000000,00000000] array('B', [128, 0, 0])b.set(1), binstr= [01000000,00000000,00000000] array('B', [64, 0, 0])b.set(7), binstr= [00000001,00000000,00000000] array('B', [1, 0, 0])b.set(8), binstr= [00000000,10000000,00000000] array('B', [0, 128, 0])b.set(9), binstr= [00000000,01000000,00000000] array('B', [0, 64, 0])any() test...False True Trueall() test...False False Truenone() test...True False Falseflip() test...[00000000,00000000,00000000] [11111111,11111111,11111111][10111111,11111111,11111111] [01000000,00000000,00000000]>>>