Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

count_ones() gives wrong result for BitSlice #7

Closed
geq1t opened this issue Apr 12, 2019 · 3 comments
Closed

count_ones() gives wrong result for BitSlice #7

geq1t opened this issue Apr 12, 2019 · 3 comments

Comments

@geq1t
Copy link

geq1t commented Apr 12, 2019

Using bitvec 0.10, counting 1 bits in a bit slice within a single storage unit returns the total number of bits within the slice, not the number of 1 bits. Interestingly, it does work across when operating across byte boundaries. The following program:

fn main()
{
    let bits: [u8; 2] = [0b00111100, 0b11001100];
    let bv: bitvec::BitVec = (&bits as &[u8]).into();

    // Here, count_ones() fails
    let bs = &bv[4..8];
    println!("{} {}", bs, bs.count_ones());

    // Here, it works
    let bs = &bv[6..10];
    println!("{} {}", bs, bs.count_ones());
}

returns the following output

[1100] 4
[00, 11] 2
@myrrlyn
Copy link
Collaborator

myrrlyn commented Apr 12, 2019

Hi! Thanks so much for your report. The bug here is pretty embarrassing in how it got overlooked. The diff here is just inserting the correct .filter call in the sole-element case; I'd managed to forget to do that in both count_ones and count_zeros, and never noticed.

I've published 0.10.1 to https://crates.io/crates/bitvec; you should be able to get the fix with a cargo update command.

@myrrlyn
Copy link
Collaborator

myrrlyn commented Apr 12, 2019

@geq1t, I've been looking through the git history of this bug and I want to take a minute to say how extremely thankful I am not only for opening this issue but for the information you provided. The test case you gave reproduced, and your sentence,

Interestingly, it does work across when operating across byte boundaries.

Told me exactly what the error was while I was reading the email, before I'd even opened my editor. This was a great report; thanks for using bitvec and I hope it's working well for you!

@geq1t
Copy link
Author

geq1t commented Apr 12, 2019

Thanks for fixing this so quickly! I'm glad the bug report was useful. I'll give it a try again next week, but other duties call right now.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants