所以我正在阅读这个网站的一些代码:http ://www.geeksforgeeks.org/write-ac-program-to-find-the-parity-of-an-unsigned-integer/
它显示了如何确定一个数字是偶校验还是奇校验。但是,我不明白为什么运行时效率是 log(n)。这是供参考的代码:
# include <stdio.h>
# define bool int
/* Function to get parity of number n. It returns 1
if n has odd parity, and returns 0 if n has even
parity */
bool getParity(unsigned int n)
{
bool parity = 0;
while (n)
{
parity = !parity;
n = n & (n - 1);
}
return parity;
}