目录
-
统计一个无符号数中的1的个数
-
实现方法一:
/*方法一:
*
* eg:
*
* 9--> 1001
*
* &
*-----------------------
* 9-1--> 1000
*
*-----------------------
* 1000
*一次类推,直到:num = 0,为止.
*
* */
/**************************************************************************************
* Description: To info the num of 1 in an unsigned number.
* Input Args: An unsigned number.
* Output Args: No.
* Return Value: The num of 1 in the unsigned number.
*************************************************************************************/
int Count_One1 (unsigned int num)
{
if (num < 0)
return -1;
static int count = 0;
for (count = 0; num != 0; count++)
{
num &= num - 1;
}
return count;
} /* ----- End of Count_One() ----- */
-
实现方法二:
/*方法二:
* 因为>>n,相当于/2^n
* 即也可以使用num / 2来代替num >> 1;
* eg:
*
* D ---> B
* 9 ---> 1001
* &
* 0001
*---------------------
* 0001
*---------------------
*依次右移一位后&1:
*---------------------
* 9>>1 --> 0100
* &
* 0001
* ------------------
* 0000
*
**********************
*
* 9>>1 --> 0010
* &
* 0001
* ---------------
* 0000
**********************
*
* 9>>1 --> 0000
* &
* 0001
* -----------------
* 0001
*----------------------
*依次与1做与运算,直到:num = 0,为止.
*
* */
/**************************************************************************************
* Description: To info the num of 1 in an unsigned number.
* Input Args: An unsigned number.
* Output Args: No.
* Return Value: The num of 1 in the unsigned number.
*************************************************************************************/
int count_One2(unsigned int num)
{
static int count = 0;
while ( num )
{
// 当最后一位为1时,则加1
if( (num & 1) ){
++count;
}
num >>= 1;
}
return count;
} /* ----- End of count_one() ----- */
-
统计一个无符号数中的0的个数
-
实现方法:
/*方法:
* 因为>>n,相当于/2^n
* 即也可以使用num / 2来代替num >> 1;
*
* eg:
*
* D ---> B
* 9 ---> 1001
* &
* 0001
*---------------------
* 0001
* !(0001) = 0000
*
*---------------------
*依次右移一位后&1:
*---------------------
* 9>>1 --> 0100
* &
* 0001
* ------------------
* 0000
* !(0000) = 1111
*
**********************
*
* 9>>1 --> 0010
* &
* 0000
* ---------------
* 0000
* !(0000) = 1111
**********************
*
* 9>>1 --> 0000
* &
* 0001
* -----------------
* 0000
* !(0000) = 1111
*----------------------
*依次与1做与运算之后在取反,直到:num = 0,为止.
*
* */
/**************************************************************************************
* Description: To info the num of 0 in an unsigned number.
* Input Args: An unsigned number.
* Output Args: No.
* Return Value: The num of 0 in the unsigned number.
*************************************************************************************/
int count_Zero(unsigned int num)
{
static int count = 0;
while ( num )
{
// 当最后一位为1时,则加1
if( !(num & 1) ){
++count;
}
num >>= 1;
}
return count;
} /* ----- End of count_Zero() ----- */
-
测试
-
测试代码
/********************************************************************************* * Copyright: (C) 2020 ysn * All rights reserved. * * Filename: Info_zero_one_num.c * Description: This file is to info a unsigned num 0/1 's count. * * Version: 1.0.0(24/07/20) * Author: tianjincheng <473892093@qq.com> * ChangeLog: 1, Release initial version on "24/07/20 14:38:42" * ********************************************************************************/ #include <stdio.h> /*方法一: * * eg: * * 9--> 1001 * * & *----------------------- * 9-1--> 1000 * *----------------------- * 1000 *一次类推,直到:num = 0,为止. * * */ /************************************************************************************** * Description: To info the num of 1 in an unsigned number. * Input Args: An unsigned number. * Output Args: No. * Return Value: The num of 1 in the unsigned number. *************************************************************************************/ int Count_One1 (unsigned int num) { if (num < 0) return -1; static int count = 0; for (count = 0; num != 0; count++) { num &= num - 1; } return count; } /* ----- End of Count_One() ----- */ /*方法二: * 因为>>n,相当于/2^n * 即也可以使用num / 2来代替num >> 1; * eg: * * D ---> B * 9 ---> 1001 * & * 0001 *--------------------- * 0001 *--------------------- *依次左移一位后&1: *--------------------- * 9>>1 --> 0100 * & * 0001 * ------------------ * 0000 * ********************** * * 9>>1 --> 0010 * & * 0001 * --------------- * 0000 ********************** * * 9>>1 --> 0000 * & * 0001 * ----------------- * 0001 *---------------------- *依次与1做与运算,直到:num = 0,为止. * * */ /************************************************************************************** * Description: To info the num of 1 in an unsigned number. * Input Args: An unsigned number. * Output Args: No. * Return Value: The num of 1 in the unsigned number. *************************************************************************************/ int count_One2(unsigned int num) { static int count = 0; while ( num ) { // 当最后一位为1时,则加1 if( (num & 1) ){ ++count; } num >>= 1; } return count; } /* ----- End of count_one() ----- */ /*方法: * 因为>>n,相当于/2^n * 即也可以使用num / 2来代替num >> 1; * * eg: * * D ---> B * 9 ---> 1001 * & * 0001 *--------------------- * 0001 * !(0001) = 0000 * *--------------------- *依次左移一位后&1: *--------------------- * 9>>1 --> 0100 * & * 0001 * ------------------ * 0000 * !(0000) = 1111 * ********************** * * 9>>1 --> 0010 * & * 0000 * --------------- * 0000 * !(0000) = 1111 ********************** * * 9>>1 --> 0000 * & * 0001 * ----------------- * 0000 * !(0000) = 1111 *---------------------- *依次与1做与运算之后在取反,直到:num = 0,为止. * * */ /************************************************************************************** * Description: To info the num of 0 in an unsigned number. * Input Args: An unsigned number. * Output Args: No. * Return Value: The num of 0 in the unsigned number. *************************************************************************************/ int count_Zero(unsigned int num) { static int count = 0; while ( num ) { // 当最后一位为1时,则加1 if( !(num & 1) ){ ++count; } num >>= 1; } return count; } /* ----- End of count_Zero() ----- */ int main (int argc, char **argv) { int count1 = 0; int count2 = 0; int count3 = 0; count1 = Count_One1(2020); count3 = count_One2(2020); count2 = count_Zero(2020); printf("way1:count of 1: %d\n", count1); printf("way2:count of 1: %d\n", count3); printf("count of 0: %d\n", count2); return 0; } /* ----- End of main() ----- */
-
运行结果
代码仓库:源码仓库链接