내가 16 개 진수 값이에서 비트를 계산하는 방법, I (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
비트의 총 수를 계산해야하지만, 그것은 단지 첫 번째 바이트의 비트를 반환합니다. 반품 전 반품 인쇄. 예 : 0x3f0xff 내가 필요로하는 것은 3F00FF입니다 (한 16 진수로 함께 묶인 각 16 진수의 이진 값) 또는 다른 방법으로 결합 된 비트 수를 읽을 수 있습니다.
이 작동 듯하지만 난 대신 16 bus.write_port 10 (0, 0xFF로) 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()
임 (10)의 개수를 얻는거야?
이
지금 노력하고 있습니다def bitCount():
result = bin(bus_1.read_port(0)).count('1') + bin(bus_1.read_port(1)).count('1')
return (result)
음'0x3f0xff '은 의미가 없습니다. –
@ WillemVanOnsem의 작업 방법을 얻는 방법에 대한 추가 코드와 질문에 이어 원래 질문을 다시 작성하는 것이 도움이됩니다. – scrpy