Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save KarstenB/d3c54319e2e6697c5547 to your computer and use it in GitHub Desktop.
Save KarstenB/d3c54319e2e6697c5547 to your computer and use it in GitHub Desktop.
This allows a BigInteger to be logically shifted right (filling with 0)
public static BigInteger srl(BigInteger l, int width, int shiftBy) {
if (l.signum() >= 0)
return l.shiftRight(shiftBy);
BigInteger opener = BigInteger.ONE.shiftLeft(width + 1);
BigInteger opened = l.subtract(opener);
BigInteger mask = opener.subtract(BigInteger.ONE).shiftRight(shiftBy + 1);
BigInteger res = opened.shiftRight(shiftBy).and(mask);
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment