Skip to content

Commit dbc9754

Browse files
authored
bitfield: fix pop_count(), add test (#26262)
1 parent b9e9192 commit dbc9754

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

vlib/bitfield/bitfield.v

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -428,20 +428,14 @@ pub fn (a BitField) == (b BitField) bool {
428428
pub fn (instance BitField) pop_count() int {
429429
size := instance.size
430430
bitnslots := zbitnslots(size)
431-
tail := size % slot_size
432431
mut count := 0
433-
for i in 0 .. bitnslots - 1 {
432+
for i in 0 .. bitnslots {
434433
for j in 0 .. slot_size {
435434
if u32(instance.field[i] >> u32(j)) & u32(1) == u32(1) {
436435
count++
437436
}
438437
}
439438
}
440-
for j in 0 .. tail {
441-
if u32(instance.field[bitnslots - 1] >> u32(j)) & u32(1) == u32(1) {
442-
count++
443-
}
444-
}
445439
return count
446440
}
447441

vlib/bitfield/bitfield_test.v

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ fn test_pop_count() {
134134
assert count0 == count1
135135
}
136136

137+
fn test_pop_count2() {
138+
b := bitfield.from_str('011000110110110000010001000011010011011111011110101001010011011010001100001001101111111011010011')
139+
assert b.pop_count() == 50
140+
}
141+
137142
fn test_hamming() {
138143
len := 80
139144
mut count := 0

0 commit comments

Comments
 (0)