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

getBit behaviour differs from setBit #4

Open
lime opened this issue Aug 19, 2019 · 1 comment · May be fixed by #5

Comments

@lime
Copy link

commented Aug 19, 2019

Hi, thanks for the library!

I was playing around with it and noticed some odd behaviour. Calling setBit(n, true) will set bit n counting from the least significant bit upwards (as expected), but getBit(n) will look in a different place.

var BitMask = require("bit-mask");
var mask = new BitMask("0");

mask.setBit(3, true);
console.log(mask.bits()); // "1000"
console.log('3: ' + mask.getBit(3)); // "3: false"
console.log('7: ' + mask.getBit(7)); // "7: true"

It looks like the logic of getBithas been tuned quite specifically for the file permission case, while also counting bits in the reverse order, and the position of the "0" bit depends on which base was used when initializing the mask, which in the case of file permissions (base 8 with 3 digits) happens to work:

bit-mask/bit-mask.js

Lines 18 to 20 in ee387f1

BitMask.prototype.getBit = function(position){
return !!((1 << (this.base-position) ) & this.value);
}

This should probably be changed to mirror how setBit works, with bit 0 being the rightmost, least significant bit.

@lime lime referenced a pull request that will close this issue Aug 19, 2019
@khrome

This comment has been minimized.

Copy link
Owner

commented Sep 2, 2019

Thanks for the contribution... good catch and the code looks good. I'll test locally and if everything passes, I'll merge!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.