浮点数的按位运算(对于图形)?

发布于 2024-08-20 09:57:56 字数 714 浏览 5 评论 0原文

可能的重复:
如何对浮点数执行按位运算 < /p>

, 每个人!

背景:
我知道可以对图形应用按位运算(例如异或)。我还知道,在图形程序中,图形数据通常以浮点数据类型存储(例如能够将数据“乘以”1.05)。所以一定可以对浮点数据执行按位运算,对吧?

我需要能够对浮点数据执行按位运算。我不想想要将数据转换为长整型,按位操作它,然后转换回浮点数。

我认为,存在一种数学方法可以实现这一点,它更优雅(?)和/或更快(?)。

我已经看到了一些答案,但他们无济于事,包括这个

编辑:
另一个问题涉及空指针转换,这将依赖于更深层次的数据表示。所以它不是“完全重复”。

Possible Duplicate:
how to perform bitwise operation on floating point numbers

Hello, everyone!

Background:
I know that it is possible to apply bitwise operation on graphics (for example XOR). I also know, that in graphic programs, graphic data is often stored in floating point data types (to be able for example to "multiply" the data with 1.05). So it must be possible to perform bitwise operations on floating point data, right?

I need to be able to perform bitwise operations on floating point data. I do not want to cast the data to long, bitwise manipulate it, and cast back to float.

I assume, there exist a mathematical way to achieve this, which is more elegant (?) and/or faster (?).

I've seen some answers but they could not help, including this one.

EDIT:
That other question involves void-pointer casting, which would rely on deeper-level data representation. So it's not such an "exact duplicate".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吐个泡泡 2024-08-27 09:57:56

当“图形数据”到达屏幕时,它都不是浮点的。位运算实际上是对位串进行的。由于编码方案与二进制一致,位运算仅对数字有意义。除了提取指数或尾数之外,尝试对浮点数进行任何类型的逻辑按位运算都是一条通往地狱的道路。

基本上,您可能不想这样做。你认为你为什么这样做?

By the time the "graphics data" hits the screen, none of it is floating point. Bitwise operations are really done on bit strings. Bitwise operations only make sense on numbers because of consistent encoding scheme to binary. Trying to get any kind of logical bitwise operations on floats other than extracting the exponent or mantissa is a road to hell.

Basically, you probably don't want to do this. Why do you think you do?

べ繥欢鉨o。 2024-08-27 09:57:56

浮点数只是内存中二进制的另一种表示形式,因此您可以:

  • 测量数据类型的大小(例如 32 位),例如 sizeof(pixel)
  • 获取指向它的指针 - 选择相同大小的整数类型为此,例如 UINT *ptr = &pixel
  • 使用指针的值,例如 newpixel=(*ptr)^(*ptr)

这应该至少适用于非负值,并且应该没有相当大的计算开销,至少在像 C++ 这样的非托管上下文。也许您在执行操作时必须屏蔽一些位,并且 - 根据类型 - 您可能必须单独处理指数和基数。

A floating point number is just another representation of a binary in memory, so you could:

  • measure the size of the data type (e.g. 32 bits), e.g. sizeof(pixel)
  • get a pointer to it - choose an integer type of the same size for that, e.g. UINT *ptr = &pixel
  • use the pointer's value, e.g. newpixel=(*ptr)^(*ptr)

This should at least work with non-negative values and should have no considerable calculative overhead, at least in an unmanaged context like C++. Maybe you have to mask out some bits when doing your operation, and - depending of the type - you may have to treat exponent and base separately.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文