70,023
社区成员




i = xxxxx;
j = 0;
while( i )
{
i &= ( i - 1 );
j++;
}
struct s
{
char b1:1;
char b2:1;
char b3:1;
char b4:1;
char b5:1;
char b6:1;
char b7:1;
char b8:1;
int foo(){return b1+b2+b3+b4+b5+b6+b7+b8;}
};
[code=Assembly]
?foo@s@@QAEHXZ PROC ; s::foo, COMDAT
; _this$ = eax
; 17 : int foo(){return b1+b2+b3+b4+b5+b6+b7+b8;}
mov cl, BYTE PTR [eax]
mov dl, cl
shl dl, 7
movsx eax, dl
mov dl, cl
shl dl, 6
movsx edx, dl
sar edx, 7
sar eax, 7
add eax, edx
mov dl, cl
shl dl, 5
movsx edx, dl
sar edx, 7
add eax, edx
mov dl, cl
shl dl, 4
movsx edx, dl
sar edx, 7
add eax, edx
mov dl, cl
add dl, dl
add dl, dl
add dl, dl
movsx edx, dl
sar edx, 7
add eax, edx
mov dl, cl
add dl, dl
add dl, dl
movsx edx, dl
sar edx, 7
add eax, edx
mov dl, cl
add dl, dl
movsx edx, dl
movsx ecx, cl
sar edx, 7
add eax, edx
sar ecx, 7
add eax, ecx
ret 0
?foo@s@@QAEHXZ ENDP ; s::foo
#include <stdio.h>
int bitcount(unsigned x)
{
int b;
for(b = 0; x != 0; x &= (x-1))
b++;
return b;
}
main()
{
unsigned int y;
int n;
scanf("%d",&y);
n = bitcount(y);
printf("there is %d one bits in y.\n",n);
}
int count=0;
while(n){
count++;
n&=n-1;
}
cout<<n;