Created
September 10, 2013 18:18
-
-
Save gscattolin/6513358 to your computer and use it in GitHub Desktop.
Complement Int32
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
| 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