/******************************************************************************************************
* @file name: :CountInt
* @brief :
* @author :[email protected]
* @date :2024/06/03
* @version 1.0 :V1.0
* @property :暂无
* @note :None
* CopyRight (c) 2023-2024 [email protected] All Right Reseverd
******************************************************************************************************/
/*******************************************************************************************************
* @function name : count_bits_1
* @brief : 计算int变量中有多少bit的值是1。
* @param : value
* @retval : int
* @date :2024/06/03
* @version :V1.0
* @note :None
*******************************************************************************************************/
int count_bits_1(int value)
{
int count = 0, i = 0;
for (i = 0; i < 32; i++)
{
if (value & 0x01)
{
count++;
}
value >>= 1; //逐个移位操作
}
return count;
}
int main()
{
printf("%d\n", count_bits_1(7));
}
标签:count,变量,int,value,2024,bit,bits
From: https://www.cnblogs.com/hhail08/p/18229758