TheAlgorithms-C/leetcode/src/1009.c
Alexander Pantyukhin 856168384d
feat: add compliment of 10 integer (#1136)
* add complinent of 10  integer

* Update 1009.c

add new line at the end

* Rename README.md to DIRECTORY.md

change filename

* chore: apply suggestions from code review

Co-authored-by: David Leal <halfpacho@gmail.com>
2022-11-15 18:27:22 -06:00

16 lines
364 B
C

// Bit manipulation.
// - Find the bit length of n using log2
// - Create bit mask of bit length of n
// - Retun ~n and bit of ones mask
// Runtime: O(log2(n))
// Space: O(1)
int bitwiseComplement(int n){
if (n == 0){
return 1;
}
int binary_number_length = ceil(log2(n));
return (~n) & ((1 << binary_number_length) - 1);
}