本文整理汇总了Python中BitVector.count_bits方法的典型用法代码示例。如果您正苦于以下问题:Python BitVector.count_bits方法的具体用法?Python BitVector.count_bits怎么用?Python BitVector.count_bits使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BitVector
的用法示例。
在下文中一共展示了BitVector.count_bits方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: printSubtree
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import count_bits [as 别名]
else:
result = "(" + result + ")"
return result
def printSubtree(adjList, taxaBv):
result = printSubtreeRecursive(adjList, taxaBv, 1)
sys.stdout.write( result + ";\n")
adjList = parseAdjList(nodeDmp)
relevantTaxa = parseTaxList(relevantTaxaFile)
maxi = 0
for elem in adjList.keys():
tmp = max(adjList[elem])
if tmp > maxi:
maxi = tmp
taxaBv = BitVector(size=maxi)
for taxon in relevantTaxa:
taxaBv[taxon] = 1
i = 0
found = 0
unrooted = False
numBits = taxaBv.count_bits()
# progress = ProgressBar(554623)
printSubtree(adjList, taxaBv)
示例2: print
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import count_bits [as 别名]
bv1[20:] = bv1[5:10]
print("bv1= " + str(bv1)) # 0101001010000000000001010
bv1[:] = bv1[:]
print("bv1= " + str(bv1)) # 0101001010000000000001010
bv3 = bv1[:]
print("bv3= " + str(bv3)) # 0101001010000000000001010
print("\nTesting reset function:")
bv1.reset(1)
print("bv1= " + str(bv1)) # 1111111111111111111111111
print(bv1[3:9].reset(0)) # 000000
print(bv1[:].reset(0)) # 0000000000000000000000000
print("\nTesting count_bit():")
bv = BitVector(intVal=45, size=16)
y = bv.count_bits()
print(y) # 4
bv = BitVector(bitstring='100111')
print(bv.count_bits()) # 4
bv = BitVector(bitstring='00111000')
print(bv.count_bits()) # 3
bv = BitVector(bitstring='001')
print(bv.count_bits()) # 1
bv = BitVector(bitstring='00000000000000')
print(bv.count_bits()) # 0
print("\nTest set_value idea:")
bv = BitVector(intVal=7, size=16)
print(bv) # 0000000000000111
bv.set_value(intVal=45)
print(bv) # 101101
示例3: des
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import count_bits [as 别名]
def des(encrypt_or_decrypt, input_file, output_file, key ):
new_bv = BitVector(size=64)
new_bv1 = BitVector(size=64)
bv = BitVector( filename = input_file )
FILEOUT = open( output_file, 'w' )
FILE = open("out1.txt",'w')
count=0 ## counts number of times the while loop runs
sum = 0 ## sums number of bits changed across all the blocks
sum1 = 0 ## sums number of bits changed across all the blocks->confusion
while(bv.more_to_read):
bitvec = bv.read_bits_from_file( 64 ) ## assumes that your file has an integral
bitvec.pad_from_right(64 - len(bitvec)) ## multiple of 8 bytes. If not, you must pad it.
[LE, RE] = bitvec.divide_into_two()
### diffusion ###
bit = randint(0,31) #generates index of the random bit to be changed
diff_bv = RE.deep_copy() #new bitvector generated to measure diffusion
if diff_bv[bit] == 0:
diff_bv[bit] = 1
else:
diff_bv[bit] = 0
######################
### confusion ###
bit1 = randint(0,47)
diff_key = key.deep_copy()
if diff_key[bit1] == 0:
diff_key[bit1] = 1
else:
diff_key[bit1] = 0
######################
rkey = extract_round_key(key)
rkey1 = extract_round_key(diff_key)
## generating different bitvector accounting confusion and diffusion
new_bv = diffusion_or_confusion(diff_bv, LE,encrypt_or_decrypt,rkey,s_box)
new_bv1 = diffusion_or_confusion(RE,LE,encrypt_or_decrypt,rkey1,s_box)
for i in range(16):
## write code to carry out 16 rounds of processing
TMP = RE
## Expansion_permutation
RE = RE.permute(expansion_permutation)
# Get the order of key right
if encrypt_or_decrypt == "encrypt":
RE = RE ^ rkey[i]
elif encrypt_or_decrypt == "decrypt":
RE = RE ^ rkey[15 - i]
## Do the S_boxes subsititution
k=0
output = BitVector(size = 0)
for ct in range(8):
row = RE[k]*pow(2,1) + RE[k+5]*pow(2,0)
col = RE[k+1]*pow(2,3) + RE[k+2]*pow(2,2)+ RE[k+3]*pow(2,1) + RE[k+4]*pow(2,0)
sbox_val = s_box[ct][row][col]
sbox_bv = BitVector(intVal = int(sbox_val), size = 4)
output += sbox_bv
k += 6
## Permutation with P-Box
output = output.permute(p_box_permutation)
## XOR with original LE
RE = LE ^ output
LE = TMP
## Add RE and LE up
bitvec = RE + LE
new_bv ^= bitvec
new_bv1 ^= bitvec
sum += new_bv.count_bits()
count += 1
sum1 += new_bv1.count_bits()
## Output the encryption or decryption
mytext = bitvec.get_text_from_bitvector()
FILEOUT.write(mytext)
print 'Average number of bits changed for diffusion is', sum/count
print 'Average number of bits changed for confusion is', sum1/count
FILEOUT.close()