LeetCode-476. Number Complement

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.

public class Solution {
    public int findComplement(int num) {
        return ~num & ((Integer.highestOneBit(num)<<1) - 1);
    }
}

 

posted @ 2017-01-20 11:15  Pickle  阅读(306)  评论(0编辑  收藏  举报