⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bitvectordemo.py

📁 java bitvector implementation.
💻 PY
字号:
#!/usr/bin/env pythonimport BitVector# Construct a bit vector of size 0bv1 = BitVector.BitVector( size = 0 )print bv1                                   # no output# Construct a bit vector of size 1bv2 = BitVector.BitVector( size = 2 )print bv2                                   # 0# Join two bit vectors:print bv1 + bv2                             # 0# Construct a bit vector with a tuple of bits:bv = BitVector.BitVector( bitlist = (1, 0, 0, 1) )print bv                                    # 1001# Construct a bit vector with a list of bits:    bv = BitVector.BitVector( bitlist = [1, 1, 0, 1] )print bv                                    # 1101# Construct a bit vector from an integerbv = BitVector.BitVector( intVal = 5678 )print bv                                    # 0001011000101110bv = BitVector.BitVector( intVal = 0 )print bv                                    # 0bv = BitVector.BitVector( intVal = 2 )print bv                                    # 10bv = BitVector.BitVector( intVal = 3 )print bv                                    # 11bv = BitVector.BitVector( intVal = 123456 )print bv                                    # 11110001001000000print bv.intValue()                         # 123456print int( bv )# Construct a bit vector directly from a file-like object:import StringIOx = "111100001111"fp_read = StringIO.StringIO( x )bv = BitVector.BitVector( fp = fp_read )print bv                                    # 111100001111 # Construct a bit vector directly from a bit string:bv = BitVector.BitVector( bitstring = '00110011' )print bv                                    # 00110011bv = BitVector.BitVector( bitstring = '' )print bv                                    # nothing# Get the integer value of a bit vector:print bv.intValue()                         # 51# Test array-like indexing for a bit vector:bv = BitVector.BitVector( bitstring = '110001' )print bv[0], bv[1], bv[2], bv[3], bv[4], bv[5]       # 1 1 0 0 0 1print bv[-1], bv[-2], bv[-3], bv[-4], bv[-5], bv[-6] # 1 0 0 0 1 1# Check equality and inequality operators:bv1 = BitVector.BitVector( bitstring = '00110011' )bv2 = BitVector.BitVector( bitlist = [0,0,1,1,0,0,1,1] )print bv1 == bv2                            # Trueprint bv1 != bv2                            # Falseprint bv1 < bv2                             # Falseprint bv1 <= bv2                            # Truebv3 = BitVector.BitVector( intVal = 5678 )print bv3.intValue()                        # 5678print bv3                                   # 0010110000101110print bv1 == bv3                            # Falseprint bv3 > bv1                             # Trueprint bv3 >= bv1                            # True# Create a string representation of a bit vector:fp_write = StringIO.StringIO()bv.write_bits_to_fileobject( fp_write )print fp_write.getvalue()                   # 111100001111 # Show array like access for getting and setting:print bv2[2]                                # 0bv2[2] = 1print bv2                                   # 1111bv2[2] = 0# Experiments with bit-wise logical operations:bv3 = bv1 | bv2                              print bv3                                   # 1101bv3 = bv1 & bv2print bv3                                   # 1001    bv3 = bv1 + bv2print bv3                                   # 10011101bv4 = BitVector.BitVector( size = 3 )print bv4                                   # 000bv5 = bv3 + bv4print bv5                                   # 10011101000bv6 = ~bv5print bv6                                   # 01100010111bv7 = bv5 & bv6print bv7                                   # 00000000000bv7 = bv5 | bv6print bv7                                   # 11111111111# Try setbit and getsize:bv7[7] = 0print bv7                                   # 11111110111print len( bv7 )                            # 11bv8 = (bv5 & bv6) ^ bv7print bv8                                   # 11111110111# Construct a bit vector from a LIST of bits:bv = BitVector.BitVector( bitlist= [0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1] )print bv                                    # 0010010100101001# Construct a bit vector from a file:bv = BitVector.BitVector( filename = 'testinput1.txt' )print bv                                    # nothing to showbv1 = bv.read_bits_from_file(64)    print bv1     # 0100000100100000011010000111010101101110011001110111001001111001bv2 = bv.read_bits_from_file(64)    print bv2     # 0010000001100010011100100110111101110111011011100010000001100110bv3 = bv1 ^ (bv2)print bv3     # 0110000101000010000110100001101000011001000010010101001000011111# Divide into two bit vectors:[bv4, bv5] = bv3.divide_into_two()print bv4                            # 01100001010000100001101000011010print bv5                            # 00011001000010010101001000011111# Permute a bit vector:bv1 = BitVector.BitVector( bitlist = [1, 0, 0, 1, 1, 0, 1] )print bv1                                    # 1001101bv2 = bv1.permute( [6, 2, 0, 1] )print bv2                                    # 1010bv3 = BitVector.BitVector( bitlist = [1, 1, 0, 0, 0, 1, 1] )print bv3                                    # 1100011bv4 = bv1 & bv3 print bv4                                    # 1000001print# Read a file from the beginning to end:bv = BitVector.BitVector( filename = 'testinput4.txt' )while (bv.more_to_read):    bv_read = bv.read_bits_from_file( 64 )    print bv_readprint# Experiment with closing a file object and start# extracting bit vectors from the file from# the beginning again:bv.close_file_object()bv = BitVector.BitVector( filename = 'testinput4.txt' )bv1 = bv.read_bits_from_file(64)        print bv1           FILEOUT = open( 'testinput5.txt', 'w' )bv1.write_to_file( FILEOUT )FILEOUT.close# Experiment in 64-bit permutation and unpermutation:# The permutation array was generated separately by the# Fisher-Yates shuffle algorithm:bv2 = bv1.permute( [22, 47, 33, 36, 18, 6, 32, 29, 54, 62, 4,                    9, 42, 39, 45, 59, 8, 50, 35, 20, 25, 49,                    15, 61, 55, 60, 0, 14, 38, 40, 23, 17, 41,                    10, 57, 12, 30, 3, 52, 11, 26, 43, 21, 13,                    58, 37, 48, 28, 1, 63, 2, 31, 53, 56, 44, 24,                    51, 19, 7, 5, 34, 27, 16, 46] )print bv2bv3 = bv2.unpermute( [22, 47, 33, 36, 18, 6, 32, 29, 54, 62, 4,                      9, 42, 39, 45, 59, 8, 50, 35, 20, 25, 49,                      15, 61, 55, 60, 0, 14, 38, 40, 23, 17, 41,                      10, 57, 12, 30, 3, 52, 11, 26, 43, 21, 13,                      58, 37, 48, 28, 1, 63, 2, 31, 53, 56, 44, 24,                      51, 19, 7, 5, 34, 27, 16, 46] )    print bv3printprint# Try circular shifts to the left and to the rightprint bv3bv3 << 7print bv3bv3 >> 7print bv3# Test len()    print len( bv3 )                      # 64# Test slicingbv4 = bv3[5:22]print bv4                             # 00100100000011010# Test the iterator:for item in bv4:    print item,                       # 0 0 1 0 0 1 0 0 0 0 0 0 1 1 0 1 0print    # Demonstrate padding a bit vector from leftbv = BitVector.BitVector( bitstring = '101010' )bv.pad_from_left( 4 )print bv                              # 0000101010# Demonstrate padding a bit vector from rightbv.pad_from_right( 4 )print bv                              # 00001010100000

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -