原題: write the function int bitcount(short input) that takes a short as input 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
代碼:
/********************************************************************
created: 2006/06/14
created: 14:6:2006 16:53
filename: c:/documents and settings/administrator/my documents/近期閱讀/mytinything/bitcount.c
file path: c:/documents and settings/administrator/my documents/近期閱讀/mytinything
file base: bitcount
file ext: c
author: a.tng
version: 0.0.1
purpose: 面試題
write the function int bitcount(short input) that takes a short as input 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
*********************************************************************/
#include
/*
* name: bitcount
* params:
* input [in] 需要分析的 short 型數(shù)
* return:
* 輸入?yún)?shù) input 的為1的位數(shù)
* notes:
* 計(jì)算 input 中為1的位數(shù)
* author: a.tng 2006/06/14 17:00
*/
int bitcount(short input)
{
int n_ret = 0;
/* 只要 input 不為0,則循環(huán)判斷 */
while (0 != input)
{
/* 如果 input & 1 == 1 則表示最低位為1,反之則等于0 */
if (input & 1)
{
n_ret++;
}
input = input >> 1;
}
return n_ret;
}
/*
* name: main
* params:
* none
* return:
* 函數(shù)返回狀態(tài)給系統(tǒng)
* notes:
* 系統(tǒng) main 函數(shù)
* author: a.tng 2006/06/14 17:04
*/
int main()
{
/* 輸入需要計(jì)算的整數(shù) */
int n = 2543;
/* 輸出 bitcount 的結(jié)果 */
printf('%d/n', bitcount((short) n));
/* 發(fā)送 pause 命令給系統(tǒng) */
system('pause');
}
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
代碼:
/********************************************************************
created: 2006/06/14
created: 14:6:2006 16:53
filename: c:/documents and settings/administrator/my documents/近期閱讀/mytinything/bitcount.c
file path: c:/documents and settings/administrator/my documents/近期閱讀/mytinything
file base: bitcount
file ext: c
author: a.tng
version: 0.0.1
purpose: 面試題
write the function int bitcount(short input) that takes a short as input 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
*********************************************************************/
#include
/*
* name: bitcount
* params:
* input [in] 需要分析的 short 型數(shù)
* return:
* 輸入?yún)?shù) input 的為1的位數(shù)
* notes:
* 計(jì)算 input 中為1的位數(shù)
* author: a.tng 2006/06/14 17:00
*/
int bitcount(short input)
{
int n_ret = 0;
/* 只要 input 不為0,則循環(huán)判斷 */
while (0 != input)
{
/* 如果 input & 1 == 1 則表示最低位為1,反之則等于0 */
if (input & 1)
{
n_ret++;
}
input = input >> 1;
}
return n_ret;
}
/*
* name: main
* params:
* none
* return:
* 函數(shù)返回狀態(tài)給系統(tǒng)
* notes:
* 系統(tǒng) main 函數(shù)
* author: a.tng 2006/06/14 17:04
*/
int main()
{
/* 輸入需要計(jì)算的整數(shù) */
int n = 2543;
/* 輸出 bitcount 的結(jié)果 */
printf('%d/n', bitcount((short) n));
/* 發(fā)送 pause 命令給系統(tǒng) */
system('pause');
}

