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

Always read multiple bits from the current byte #4

Merged
merged 2 commits into from
Nov 12, 2016

Conversation

icewind1991
Copy link
Collaborator

Read the maximum number of bits from the current byte as we can instead of reading bits one by one.

Note that the check for the special case (reading a whole byte) is more expensive than the overhead for the generic case in my tests

Copy link
Owner

@inolen inolen left a comment

Choose a reason for hiding this comment

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

LGTM for the most part, added some minor nagging comments.

Should we apply the same concept to setBits?

var read = Math.min(toRead, 8 - bitOffset);

// create a mask with the correct bit width
var mask = 0xFF >> (8 - read);
Copy link
Owner

Choose a reason for hiding this comment

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

I think it's a bit more common to see (1 << read) - 1 for this purpose.

value |= (this._getBit(offset) << i);
read = 1;
}
var toRead = bits - i;
Copy link
Owner

Choose a reason for hiding this comment

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

var remaining would avoid the confusion with the subsequent read variable name.

@icewind1991
Copy link
Collaborator Author

fixed both nitpicks

Not sure about setBits, while it's possible to use something similar it would be more complex since it would involve first masking out the to-overwrite bits so I'm not sure how big the speed improvements would be

@inolen inolen merged commit 26f3f48 into inolen:master Nov 12, 2016
@inolen
Copy link
Owner

inolen commented Nov 12, 2016

Thanks!

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.

None yet

2 participants