Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. Karsten Becker created this gist Jul 13, 2014.
    9 changes: 9 additions & 0 deletions Shift Right Logical (SRL) for BigInteger
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    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;
    }