A complement of Base 10 Integer
找到数字的二进制的反数. 先找十进制的数在二进制下有多少bit位. 然后再找到bit下的全1数字, 然后相减.
1 2 3 4 5 6 7 8 |
class Solution { public int bitwiseComplement(int N) { if(N == 0) return 1; int length = (int)(Math.log(N) / Math.log(2) + 1); return (int)Math.pow(2, length) - 1 - N; } } |