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

Optimization for Quotient and Remainder #16

Closed
ghost opened this issue Feb 28, 2020 · 0 comments
Closed

Optimization for Quotient and Remainder #16

ghost opened this issue Feb 28, 2020 · 0 comments
Assignees
Projects

Comments

@ghost
Copy link

ghost commented Feb 28, 2020

Re:

https://github.com/jinahya/bit-io/blob/develop/src/main/java/com/github/jinahya/bit/io/AbstractBitInput.java#L116

There are a few places in the code base that use integer division and modular arithmetic. It may be possible to replace them with faster bit-shifting equivalents. Imagine a variable mPosition that points to a particular bit within a byte array. Retrieving the value of the bit can be accomplished as follows, where BYTE_INDEX_SHIFT equals 3 (i.e., log base 2 of 8):

final int byteIndex = (int) (mPosition >> BYTE_INDEX_SHIFT);
final int bitIndex = (int) (mPosition - (byteIndex << BYTE_INDEX_SHIFT));

final int bitShifts = Byte.SIZE - bitIndex - 1;
final int bit = (mSequence[byteIndex] & (1 << bitShifts)) >> bitShifts;

I suspect such calculations will be more efficient than using %, especially on older hardware.

@onacit onacit self-assigned this Feb 29, 2020
@onacit onacit added this to To do in kanban via automation Feb 29, 2020
onacit added a commit that referenced this issue Mar 7, 2020
@onacit onacit moved this from To do to In progress in kanban Mar 7, 2020
onacit added a commit that referenced this issue Mar 7, 2020
onacit added a commit that referenced this issue Mar 11, 2020
@onacit onacit closed this as completed Mar 11, 2020
kanban automation moved this from In progress to Done Mar 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
kanban
  
Done
Development

No branches or pull requests

1 participant