如何在 C 中反转按位与 (&)?
如何在 C 中反转按位与 (&)?
例如,我在 C 中有一个这样的操作:
((unsigned int)ptr & 0xff000000))
结果是 0xbf000000
。我现在需要的是如何反转上面的过程,即通过运算结果确定ptr
,当然还有0xff000000
。
有没有简单的方法可以用C实现这个?
How to reverse bitwise AND (&) in C?
For example I have an operation in C like this:
((unsigned int)ptr & 0xff000000))
The result is 0xbf000000
. What I need at this moment is how to reverse the above, i.e. determine ptr
by using the result from the operation and of course 0xff000000
.
Is there any simple way to implement this in C?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
按位
&
不能反转:Bitwise
&
can't be reversed:你不能这样做,因为你已经丢弃了信息(即位) - 你无法从任何地方取回信息。
请注意,
AND
(&
) 和OR
(|
) 都是破坏性的。唯一可逆的布尔运算是 XOR (^
) 和 NOT (~
)。You can't do that because you have thrown away information (i.e. bits) - you can't get information back from nowhere.
Note that both
AND
(&
) andOR
(|
) are destructive. The only Boolean operations that are reversible areXOR
(^
) and NOT (~
).不可能的。按位与0xff000000 是有损操作。您将永久丢失低 24 位。
Impossible. Bitwise & of 0xff000000 is a lossy operation. You lose the lower 24-bits permanently.
您只能反转异或,因为它是非破坏性的。
OR 和 AND 都是破坏性的。
You can only reverse XOR, as it's non-destructive.
Both OR and AND are destructive.