fix `countZeros` (#80)

should return zeros based on bits, not bytes!
This commit is contained in:
Jacek Sieka 2021-04-12 22:55:09 +02:00 committed by GitHub
parent ede0651741
commit 7d2790fdf4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 2 deletions

View File

@ -302,7 +302,7 @@ func countOnes*(x: SomeUnsignedInt): int {.inline.} =
countOnesNim(x) countOnesNim(x)
func countZeros*(x: SomeUnsignedInt): int {.inline.} = func countZeros*(x: SomeUnsignedInt): int {.inline.} =
sizeof(x) - countOnes(x) bitsof(x) - countOnes(x)
func parity*(x: SomeUnsignedInt): int {.inline.} = func parity*(x: SomeUnsignedInt): int {.inline.} =
## Calculate the bit parity in integer. If number of 1-bit ## Calculate the bit parity in integer. If number of 1-bit

View File

@ -11,6 +11,10 @@ template test() =
doAssert countOnes(0b11111111'u64) == 8 doAssert countOnes(0b11111111'u64) == 8
doAssert countOnes(0b11000001'u) == 3 doAssert countOnes(0b11000001'u) == 3
doAssert countZeros(0b00000000'u8) == 8
doAssert countZeros(0b01000100'u8) == 6
doAssert countZeros(0b11111111'u64) == 56
doAssert firstOne(0b00000000'u8) == 0 doAssert firstOne(0b00000000'u8) == 0
doAssert firstOne(0b00000001'u64) == 1 doAssert firstOne(0b00000001'u64) == 1
doAssert firstOne(0b00010010'u8) == 2 doAssert firstOne(0b00010010'u8) == 2
@ -141,4 +145,3 @@ suite "bitops2":
test "bitops2_test": test "bitops2_test":
test() # Cannot use unittest at compile time.. test() # Cannot use unittest at compile time..
runtimeTest() runtimeTest()