按位相等 [英] Bitwise equality

查看:56
本文介绍了按位相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在两个字节之间执行按位相等.这意味着,例如,如果我有两个字节:00011011和00011110,则结果为11111010 我看到的唯一快速的方法是使用以下语句

I need to perform a bitwise equality between two bytes. That means that for instance if I have two bytes: 00011011 and 00011110 the result is 11111010 The only fast way I see is to use the following statement

byte a, b;//set input bytes
byte c = ~(a^b);//output bytes

但是我想知道是否有更快的解决方案.经过这些相等的操作后,我想掩盖我需要的位.因此,我需要使用AND运算.因此代码变为:

But I wonder if there is a faster solution for this. After these equality operation I want to mask the bits I need. So I need to use an AND-operation. So the code becomes:

byte a, b;//set input bytes
byte m;//mask, intresting bits are set to 1, others to 0
byte c = (~(a^b))&m;//output bytes

没有任何更快,更简单的方法不需要使用所有这些按位运算,因为这部分代码将经常被调用.

aren't there any faster and more simple methods that don't need to use all those bitwise operations because this part of the code will be called very often.

推荐答案

我怀疑可以用更少的操作完成它.这看起来是最佳的.也许您可以将〜(a ^ b)存储在查找表(256 * 256个条目)中?我怀疑您会获得很多好处,甚至可能使情况变得更糟,但是您可以尝试一下.

I doubt it can be done in fewer operations. That looks optimal. Perhaps you could store ~(a^b) in a lookup table (256*256 entries)? I doubt you would get much benefit and possibly even make things worse, but you could try it.

这篇关于按位相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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