Returns the number of digits 1 in the integer

xiaoxiao2021-04-03  225

Original title:

Write the function int bitcount (short infut) Takes a short as infut and returns an int. The function returns the number of bits set in the input variable. For instance: Bitcount (7) -> 3 Bitcount (2543) - > 9 Bitcount (11111) -> 9

Code:

/ ************************************************** ****************** Created: 2006/06/14 create: 14: 6: 2006 16:53 FileName: c: / Documents and settings / administrator / my documents / Recent reading / ITITINYTHING/bitcount.c file path: c: / documents and settings / administrator / My documents / recently reading / mytinything file base: Bitcount File Ext: c Author: a.tng version: 0.0.1 Purpose: Interview Questions WRITE The function int bitcount (Short Input) That Takes A Short As INPUT AND RETURns An Int. The Function Retur Of Bits Set in The Input Variable. for Instance: Bitcount (7) -> 3 Bitcount (2543) -> 9 Bitcount (11111) -> 9 ************************************************** ************************************************ / # include

/ * * Name: bitcount * params: * INPUT [in] The number of SHORT types required to be analyzed * Return: * Enter the number of bits of the parameter INPUT * notes: * Calculate the number of bits 1 in INPUT * Author: A. TNG 2006/06/14 17:00 * / int bitcount (short input) {int n_ret = 0;

/ * As long as INPUT is not 0, the loop judgment * / while (0! = INPUT) {/ * If INPUT & 1 == 1 indicates the lowest bit of 1, it is equal to 0 * / if (INPUT & 1) { N_ret ;} input = input >> 1;}

Return n_ret;

/ * * Name: main * params: * none * return: * Function return status to system * Notes: * System main function * Author: a.tng 2006/06/14 17:04 * / int main () {/ * Enter an integer * / int N = 2543; / * Output BitCount's result * / printf ("% d / n", bitcount ((short) n));

/ * Send the PAUSE command to the system * / system ("pause");

转载请注明原文地址:https://www.9cbs.com/read-131494.html

New Post(0)