Skip to content

Instantly share code, notes, and snippets.

@gscattolin
Created September 10, 2013 18:18
Show Gist options
  • Select an option

  • Save gscattolin/6513358 to your computer and use it in GitHub Desktop.

Select an option

Save gscattolin/6513358 to your computer and use it in GitHub Desktop.
Complement Int32
private static byte GetPositionHigherBit(int value)
{
for (byte kk = 31; kk > 0; kk--)
if ((value & (int)(Math.Pow(2, kk))) == (int)(Math.Pow(2, kk))) return kk;
return 0;
}
private static int InvertBits(int value)
{
var posbit = GetPositionHigherBit(value);
int mask = (int)Math.Pow(2, posbit + 1) - 1;
var result = ~value & mask;
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment