简体   繁体   English

如何计算十进制数中有多少位是1?

[英]How to calculate how many bits in a decimal number is 1?

This program I created in RISC-V RARS 1.3 application is designed to take a decimal number and count how many bits are in that number.我在 RISC-V RARS 1.3 应用程序中创建的这个程序旨在采用十进制数并计算该数字中有多少位。 The one I am testing is the decimal number 5, and this program should work for any positive number I put on t1.我正在测试的是十进制数 5,这个程序应该适用于我放在 t1 上的任何正数。 Here is the code I created.这是我创建的代码。 The program is meant to add one counter whenever the result of the AND function is not 0, but the problem I have is that the program does not stop.该程序旨在在 AND 函数的结果不为 0 时添加一个计数器,但我遇到的问题是该程序不会停止。 Is there a solution to this problem?这个问题有解决方案吗?

_start:

li t1,2 # start with decimal 5, binary 101
li t2,1 # adding counter for AND function
li t3,0 # bit counter count
li t4,0 # to compare 0

and t5,t1,t2 # t1 & t2 = t5
bne t5,t4,label # go to label if t5 != 0
beqz t5,label2 # go to label if t5 == 0

label:
addi t3,t3,1 # add one to bit count
slli t2,t2,1 # shift left
and t5,t1,t2 # t1 & new t2 = t5
bne t5,t4,label # go to label if t5 != 0
beqz t5,label2 # go to label if t5 == 0

label2:
slli t2,t2,1 # shift left
and t5,t1,t2 # t1 & new t2 = t5

.data

Since you start with t2 = 1 and multiply it by 2 in each iteration, you should stop the calculation once the value of t2 becomes greater than t1 .由于您从t2 = 1开始并在每次迭代中将其乘以2 ,因此一旦t2的值大于t1 ,您应该停止计算。

Also, in your code, I see you that you probably intended to handle two cases:另外,在您的代码中,我看到您可能打算处理两种情况:

  1. label: - this block handles the case when the current bit tested is 1, it increments the number of bits and then jumps to either label or label2 . label: - 此块处理当前测试位为 1 的情况,它增加位数,然后跳转到labellabel2 Here, you just need to add the exit condition as mentioned above在这里,你只需要添加上面提到的退出条件
  2. label2: - this block handles the case when the current bit tested is 0, it does not change the number of bits, but it also does not seem to continue with either label or label2 . label2: - 此块处理当前测试位为 0 的情况,它不会更改位数,但它似乎也不会继续使用labellabel2 I think that is should keep looking whether there are any higher 1 bits, until the exit condition t2>t1 is reached.我认为应该继续查看是否有更高的 1 位,直到达到退出条件t2>t1

If RISC-V doesn't have an instruction that counts the number of set bits efficiently (most other CPUs do now);如果 RISC-V 没有有效计算设置位数的指令(大多数其他 CPU 现在都有); then the next best approach is like:那么下一个最好的方法是:

    // Value is 32 pieces with 1 bit per piece

    temp1 = (value & 0x555555555) + (value & 0xAAAAAAAA) >> 1;

    // Temp1 is 16 pieces with 2 bits per piece

    temp2 = (temp1 & 0x33333333) + (temp1 & 0xCCCCCCCC) >> 2;

    // Temp2 is 8 pieces with 4 bits per piece

    temp3 = (temp2 & 0x0F0F0F0F) + (temp2 & 0xF0F0F0F0) >> 4;

    // Temp3 is 4 pieces with 8 bits per piece

    temp4 = (temp3 & 0x00FF00FF) + (temp3 & 0xFF00FF00) >> 8;

    // Temp4 is 2 pieces with 16 bits per piece

    result = (temp4 & 0x0000FFFF) + (temp2 & 0xFFFF0000) >> 16;

    // Result is the count of all bits that were set in value (or, sum of all 32 of the original 1-bit values)

I have no idea how to write this in RISC-V assembly (I'd compile it then cut&paste the resulting assembly, but don't have a compiler for RISC-V either).我不知道如何在 RISC-V 程序集中编写它(我会编译它然后剪切并粘贴结果程序集,但也没有用于 RISC-V 的编译器)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM