File tree Expand file tree Collapse file tree 2 files changed +6
-4
lines changed Expand file tree Collapse file tree 2 files changed +6
-4
lines changed Original file line number Diff line number Diff line change @@ -43,8 +43,9 @@ def each_byte
43
43
end
44
44
45
45
# Returns the total number of bits that are set
46
- # (The technique used here is about 6 times faster than using each or inject direct on the bitfield)
46
+ # Use Brian Kernighan's way, see
47
+ # https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan
47
48
def total_set
48
- @field . each_byte . inject ( 0 ) { |a , byte | a += byte & 1 and byte >>= 1 until byte == 0 ; a }
49
+ @field . each_byte . inject ( 0 ) { |a , byte | ( a += 1 ; byte &= byte - 1 ) while byte > 0 ; a }
49
50
end
50
51
end
Original file line number Diff line number Diff line change @@ -47,9 +47,10 @@ def each_byte
47
47
end
48
48
49
49
# Returns the total number of bits that are set
50
- # (The technique used here is about 6 times faster than using each or inject direct on the bitfield)
50
+ # Use Brian Kernighan's way, see
51
+ # https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan
51
52
def total_set
52
- @field . each_byte . inject ( 0 ) { |a , byte | a += byte & 1 and byte >>= 1 until byte == 0 ; a }
53
+ @field . each_byte . inject ( 0 ) { |a , byte | ( a += 1 ; byte &= byte - 1 ) while byte > 0 ; a }
53
54
end
54
55
55
56
def byte_position ( position )
You can’t perform that action at this time.
0 commit comments