比特摆弄计算分数 [英] Bit fiddling calculating fraction

查看:84
本文介绍了比特摆弄计算分数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我正在写一个应该执行以下操作的功能:


/ **

*计算并返回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?

推荐答案



< do ********* @ gmail .comwrote in message

news:11 ********************** @ v33g2000cwv.googlegr oups.com ...

<do*********@gmail.comwrote in message
news:11**********************@v33g2000cwv.googlegr oups.com...

您好,


我正在写一个应该执行以下操作的函数:


/ **

*计算并返回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.


很抱歉不清楚。这可能更好吗?


ushort f(ushort valueA,ushort valueB){

浮动分数= valueB / 31.0;

return(ushort)(valueA * fraction);

}


只是我想避免使用浮点运算和

分割/乘法。

Sorry for being unclear. This is maybe better?

ushort f ( ushort valueA, ushort valueB) {
float fraction = valueB / 31.0;
return (ushort)(valueA * fraction);
}

Only that I want to avoid using floating point operations and
division/multiplication.


做*** ******@gmail.com 写道:
do*********@gmail.com writes:

我正在编写一个应该执行以下操作的函数:


/ **

*计算并返回valueA的分数,其中最大分数为31.

* param valueA五位值,0-31 。

* param valueB分数,五位值,0-31。

*

* /

ushort f(ushort valueA,ushort valueB){

返回值A *(valueB / 31); // paranthesis仅用于

它的外观。

}
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.
}



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;


我正在寻找一种方法来获得大致相同的结果

位摆弄值优化的位模式

表现。
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.



如果我们认为关于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.


这篇关于比特摆弄计算分数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆