计数被设置的比特数 [英] Counting the number of bits that are set
问题描述
欲计数被设置一个二进制数位的数量。
例如,用户输入是二进制01100001数97。该计划应该给我的3位被使用MIPS ISA设置。
I want to count the number of bits in a binary number that are set. For example, user enter the number 97 which is 01100001 in binary. The program should give me that 3 bits are set using MIPS ISA.
我能够用C来实现这一点,但我不知道如何使用汇编code来实现它。
I am able to achieve this in C, but I don't know how to achieve it using assembly code.
任何想法?
在此先感谢所有帮助。
Thanks in advance for all the help.
这是不是硬件,它的我的研究项目的一小部分。
This is not HW, its a tiny part of my research project.
推荐答案
什么你要找的是通常被称为人口数(popcount)。
What you're looking for is often referred to as the population count (popcount).
有一些在位操作黑客 C实现的(一些这些都是scarily聪明)。如果你熟悉C,每种方法应该有一个合理的翻译成MIPS汇编打破前任pressions后。
There are a number of C implementations on Bit Twiddling Hacks (some of which are scarily clever). If you're familiar with C, each approach should have a reasonable translation into MIPS assembly after breaking down the expressions.
如果您输入的域名是小(例如0-255),你总是可以做一个查找表和使用输入作为偏移,直接获取popcount。
If your input domain is small (e.g. 0-255), you could always do a lookup table and use the input as the offset to fetch the popcount directly.
这篇关于计数被设置的比特数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!