Bit Manipulation

91 阅读1分钟

之前写的文章:Leetcode 191 Number of 1 bits

image.png

class Solution {
public:
    int findComplement(int num) {
        //bit manipulation 的题。
        int todo = num, bit = 1;
        while(todo != 0){
            //flip the current bit
            num = num ^ bit;

            //prepare for the next run
            bit = bit << 1;
            todo = todo >> 1;
        }
        return num;
    }
};

自己补一下bit 的基本操作