问题描述
您好,
我正在写一个应该执行以下操作的功能:
/ **
*计算并返回valueA的分数,其中最大分数为31.
* param valueA五位值,0-31。
* param valueB分数,五位值,0-31。
*
* /
ushort f(ushort valueA,ushort valueB){
返回值A *(valueB / 31); //这个paranthesis仅用于
它的外观。
}
我正在寻找一种方式通过做一些
来摆弄价值的位模式优化
表现来获得大致相同的结果。
任何人都有任何想法?表查找?
Hello,
I''m writing a function that should do the following:
/**
* Calculate and return fraction of valueA where max fractions is 31.
* param valueA A five bit value, 0-31.
* param valueB The fraction, a five bit value, 0-31.
*
*/
ushort f ( ushort valueA, ushort valueB) {
return valueA * (valueB / 31); //the paranthesis are only here for
the looks of it.
}
I''m looking for a way to get approximatly the same result by doing some
bit fiddling with the bit patterns of the values optimizing
performance.
Anyone got any ideas? Table lookup?
推荐答案
目前还不清楚你在做什么。根据评论,这个
返回A或零。此外,parantheses确实有所作为。
It isn''t clear what you are doing. Based on the comments this
returns A or zero. Also, the parantheses do make a difference.
Clc指南建议发布一个完整的计划。我们必须猜测
ushort是什么。这并不像你想象的那么容易,因为你上面写的
如果我们假设是没有意义
typedef unsigned short ushort;
C.l.c Guidelines suggest posting a complete program. We have to guess
what ushort is. This is not as easy as you probably think since what
you have written above makes no sense if we assume
typedef unsigned short ushort;
如果我们认为关于valueB中可能值的块评论,则
返回值B == 31;
将与您编写的return语句相同。如果你错误地把
括号(认为它没有区别)那么
a鸿沟可能就是这样。
当然,你更有可能想要除以32.在
这种情况下,移位会更快,但编译器可能会为你做这个
给你无论如何。
写下简单最简单的代码并优化速度,只有当你发现它是一个问题时才能获得速度。
-
Ben。
If we believe the block comment about the possible values in valueB, then
return valueB == 31;
will do the same as the return statement you wrote. If you put the
parentheses in by mistake (thinking that it made no difference) then
a divide is probably the way.
Of course, it is more likely that you want to be dividing by 32. In
that case a shift will be faster but the compiler will probably do that
for you anyway.
Write the simples clearest code and optimise it for speed only if you
find it to be a problem.
--
Ben.
这篇关于比特摆弄计算分数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!