blob: 297f22f4d724833e8fc89f27926aed49d7d8222e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
// 476. Number Complement
#include "leetcode.h"
/*
* the complement of an integer is the integer you get when you flip all the 0's
* to 1's and all the 1's to 0's in its binary representation. for example, the
* integer 5 is "101" in binary and its complement is "010", which is the
* integer 2. given an integer 'num', return its complement
*/
int findComplement(int num) { return (uint)~0 >> __builtin_clz(num) ^ num; }
int main() {
printf("%d\n", findComplement(5)); // expect: 2
printf("%d\n", findComplement(1)); // expect: 0
}
|