Created
July 13, 2014 18:41
-
-
Save KarstenB/d3c54319e2e6697c5547 to your computer and use it in GitHub Desktop.
This allows a BigInteger to be logically shifted right (filling with 0)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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