二进制位运算中,有个很实用的函数叫lowbit,低位数值。定义lowbit(n)表示n的二进制表示中最低位的1代表是十进制的几。例如十进制数6对应2进制110其中最低位的1代表2;十进制数8对应2进制1000其中最低位的1代表8.
请实现lowbit函数的运算:输入十进制正整数n,输出lowbit(n)的返回值。
代码如下,如有帮助,请采纳一下谢谢
#include <iostream>
#include <process.h>
using namespace std;
int lowbit(int n)
{
if(n % 2 == 1)
return 1;
else
{
n = n >> 1;
return 2*lowbit(n);
}
}
int main()
{
cout << " 1 :" << lowbit(1) << endl;
cout << " 2 :" << lowbit(2) << endl;
cout << " 6 :" << lowbit(6) << endl;
cout << " 8 :" << lowbit(8) << endl;
cout << " 10 :" << lowbit(10) << endl;
cout << " 12 :" << lowbit(12) << endl;
system("pause");
return 0;
}