Skip to content

Fixed bug when initialising bitv from init=true #11615

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

Merged
merged 1 commit into from
Jan 19, 2014
Merged

Fixed bug when initialising bitv from init=true #11615

merged 1 commit into from
Jan 19, 2014

Conversation

adwhit
Copy link

@adwhit adwhit commented Jan 17, 2014

This is my first patch so feedback appreciated!

Bug when initialising bitv:Bitv::new(int,bool) when bool=true. It created a Bitv with underlying representation !0u rather than the actual desired bit layout ( e.g. 11111111 instead of 00001111). This works OK because a size attribute is included which keeps access to legal bounds. However when using BitvSet::from_bitv(Bitv), we then find that bitvset.contains(i) can return true when i should not in fact be in the set.

let bs = BitvSet::from_bitv(Bitv::new(100, true));
assert!(!bs.contains(&127)) //fails

The fix is to create the correct representation by treating various cases separately and using a bitshift (1<<nbits) - 1 to generate correct number of 1s where necessary.

@@ -269,14 +269,23 @@ impl Bitv {

impl Bitv {
pub fn new(nbits: uint, init: bool) -> Bitv {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add documentation for new (ie, what the parameters do)

@brson
Copy link
Contributor

brson commented Jan 18, 2014

Nice find!

bors added a commit that referenced this pull request Jan 19, 2014
This is my first patch so feedback appreciated!

Bug when initialising `bitv:Bitv::new(int,bool)` when `bool=true`. It created a `Bitv` with underlying representation `!0u` rather than the actual desired bit layout ( e.g. `11111111` instead of `00001111`). This works OK because a size attribute is included which keeps access to legal bounds.  However when using `BitvSet::from_bitv(Bitv)`, we then find that `bitvset.contains(i)` can return true when `i` should not in fact be in the set.

```
let bs = BitvSet::from_bitv(Bitv::new(100, true));
assert!(!bs.contains(&127)) //fails
```

The fix is to create the correct representation by treating various cases separately and using a bitshift `(1<<nbits) - 1` to generate correct number of `1`s where necessary.
@bors bors closed this Jan 19, 2014
@bors bors merged commit 32408a6 into rust-lang:master Jan 19, 2014
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

Successfully merging this pull request may close these issues.

4 participants