问题描述:

I'm new to mips. I know the basic functions (How to use registers, printing and scanning ints using syscall) but I've hit a wall when trying to figure out how to access the individual bits of a 32-bit integer to count the amount of 1s that are in it. I was thinking that I perhaps had to create an array of ints to represent all of the bits? But I feel like there must be an easier way.

网友答案:

basically:

int countOnes(int number)
{
    int count = 0;

    for (int i = 1; i < 32) {
        count += number & 1;
        number >>= 1;
    }
    return count;
}
相关阅读:
Top