位运算知识点
-
函数构建 int lowbit(int x )
{
return x & -x ;//用处是输出最后一个1及其后面的0
} -
x=1010
原码=0…01010
反码=1…11010
补码=1…10110= ~x + 1 = -x(负数)
~x=0101(取反)
为什么负数要用补码来表示呢?
因为计算机组成中没有减法,需要用加法来做减法
include[HTML_REMOVED]
using namespace std ;
int lowbit(int x )
{
return x & -x ;
}
int main ()
{
int n ;
cin >> n ;
while (n–)
{
int x ;
cin >> x ;
int res = 0 ;
while (x) x -= lowbit(x) , res ++ ;
cout << res << ' ' ;
}
return 0 ;
}