mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
856168384d
* 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>
16 lines
364 B
C
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);
|
|
}
|