私は16進値を持っているからビットをカウントする方法を、私は(1の)は、複数のHexidecimals
#I have tried this
val = hex(bus.read_port(0))+hex(bus.read_port(1)) #There are 16 ports.
#read port returns a Decimal (0-255)
#val is 0x3f0xff
def bitCount(val):
count = bin(val).count('1')
return (count)
#bitCount() Returns 6 instead of 14
の総ビット数をカウントする必要がある。しかし、それは唯一の最初のバイトのビットを返します。 返品前にvalを印刷しています。 0x3f0xff 私が必要とするのは3F00FFです(各16進数のバイナリ値を1つの16進数にまとめて文字列にします) または、組み合わせたビット数を読み取る別の方法です。 10の数を取得
これは動作するように見えたが、私が代わりに16 bus.write_port(0、0xFFで)の10を得 bus.write_port(1、0xFFで)
def popcount255(k):
k = (k & 15) + (k >> 4)
k = (k & 3) + (k >> 2)
return (k & 1) + (k >> 1)
def bitCount():
result = popcount255(bus.read_port(0)) + popcount255(bus.read_port(1))
return (result)
if True:
print "BitCount ", bitCount()
イム?
この
は今働いているdef bitCount():
result = bin(bus_1.read_port(0)).count('1') + bin(bus_1.read_port(1)).count('1')
return (result)
「0x3f0xff」は意味がありません。 –
@ WillemVanOnsemのメソッドを動作させる方法については、元の質問をバックアップし、その後に追加のコードと質問を付けると便利です。 – scrpy