Take the 2-minute tour ×

If I understand it correctly, register file is an array of integers. So I first need to convert the register $a0 to binary, right? Once I've done that, how would I access its individual bits? I think I may need to use a mask but I can't think of a way to access individual elements of register after I ORed or ANDed it.

Thank you

share|improve this question
You seem to have a very common confusion between numbers and representations of numbers. You need to spend some time thinking about this because it's a confusion that is fatal to a programmer. –  David Schwartz Nov 26 '12 at 4:53
@DavidSchwartz I wish I could upvote that comment about 31 more times. –  Jonathon Reinhart Nov 26 '12 at 4:57

1 Answer 1

You don't "convert an integer to binary". An integer is stored as bits in a register. Binary is a textual representation of an integer.

You want to mask out individual bits of the register with an AND and test if that result is nonzero.

// Assuming 32-bit registers
int reg = .... // Your register...
int count = 0;
    uint32_t mask = 1<<i;
    if (reg & mask)
share|improve this answer
If performance is important, use the SWAR algorithm. –  David Schwartz Nov 26 '12 at 4:54
Yes, integers are stored as bits. Thank you for pointing that out. As for the solution, are you suggesting to apply mask 32 times, like 0x0000 0001, then 0x0000 0002, 0x0000 0004 ... 0x8000 0000. I'm sure there is an easier way of doing that. –  user1078719 Nov 26 '12 at 4:55
Easier? Doubtful. More straightforward? I don't think so. More effecient? See @DavidSchwartz's link. –  Jonathon Reinhart Nov 26 '12 at 4:58
Thank you, but I'll look for other ways of doing that because my professor mentioned in the assignment that program should execute as few instructions as possible. –  user1078719 Nov 26 '12 at 6:11

Your Answer


