问题描述:获取某一整数对应的二进制比特串中最右端1的位置(右数,从0开始计数)。
string intToString(unsigned int a) {//将整数转换为二进制字符串
bitset<32> bit(a);
string ret = bit.to_string();
return ret;
}
int lsb(string str) {//获取字符串中右数第一个1的位置
for (int i = str.length(); i > 0; i--) {
if (int(str[i - 1]) == 49) {//比较ASCII码
return str.length() - i;
}
}
}
int main() {
unsigned int a = 734;//1011011110
string str = intToString(a);
int b = lsb(str);//右数第一个1的位置(从0开始计数)
cout << b << endl;//b=1
system("pause");
return 0;
}