The function that you describe is called the Hamming weight.
C . ( 32 ):
int popcount_3(uint32_t x) {
const uint32_t m1 = 0x55555555;
const uint32_t m2 = 0x33333333;
const uint32_t m4 = 0x0f0f0f0f;
const uint32_t h01 = 0x01010101;
x -= (x >> 1) & m1;
x = (x & m2) + ((x >> 2) & m2);
x = (x + (x >> 4)) & m4;
return (x * h01)>>24;
}
MIPS :
main:
addi $v0 $zero 5
syscall
lui $t5 0x0101
ori $t5 0x0101
lui $t6 0x5555
ori $t6 0x5555
lui $t7 0x3333
ori $t7 0x3333
lui $t8 0x0f0f
ori $t8 0x0f0f
srl $t0 $v0 1
and $t0 $t0 $t6
sub $v0 $v0 $t0
and $t0 $v0 $t7
srl $t1 $v0 2
and $t1 $t1 $t7
add $v0 $t0 $t1
srl $t0 $v0 4
add $t0 $v0 $t0
and $v0 $t0 $t8
mul $v0 $v0 $t5
srl $a0 $v0 24
li $v0 1
syscall
jr $ra