Skip to content

Instantly share code, notes, and snippets.

@Unril
Created August 27, 2013 07:30
Show Gist options
  • Save Unril/6350672 to your computer and use it in GitHub Desktop.
Save Unril/6350672 to your computer and use it in GitHub Desktop.
public static void CircularShiftLeft(ref uint value, int shift, int sizeInBits = BitsInInt32) {
Contract.Requires(sizeInBits > 0 && sizeInBits <= BitsInInt32);
Contract.Requires(shift <= sizeInBits && shift >= 0);
uint mask = UInt32.MaxValue >> (BitsInInt32 - sizeInBits);
uint masked = value & mask;
uint shifted = (masked << shift) | (masked >> (sizeInBits - shift));
value = (value & ~mask) | (shifted & mask);
}
public static void CircularShiftRight(ref uint value, int shift, int sizeInBits = BitsInInt32) {
Contract.Requires(0 < sizeInBits && sizeInBits <= BitsInInt32);
Contract.Requires(0 <= shift && shift <= sizeInBits);
uint mask = UInt32.MaxValue >> (BitsInInt32 - sizeInBits);
uint masked = value & mask;
uint shifted = (masked >> shift) | (masked << (sizeInBits - shift));
value = (value & ~mask) | (shifted & mask);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment