views:

333

answers:

4
+3  Q: 

Bitwise equality

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

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.

+6  A: 

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.

Mark Byers
Did you benchmark that? Theoretically ~(a^b) could be faster it probably executes in two cycles and pipelines well, an index lookup to memory can be expected to take several cycles.
JonB
The OP should note that the operations the compiler will perform to index the table are likely to be at least as expensive (probably more) as the operations to calculate the result directly. And that doesn't take into account the additional memory access.
Michael Burr
Cache-misses are 2 magnitudes slower than arithmetic ops.
Viktor Klang
When the lookup table and actual input byte array fit in the same cache line, this operation should be pretty quick. However, I expect a ~(a^b) to be as fast (and it's perhaps even optimized by the JIT compiler).
Steven
+2  A: 

These operations seem fast enough to be honest. I think you shouldn't try to optimize them further, but finish your software first, see if you are happy with the overall performance and use a profiler if you are not. I am fairly sure the problem will be elsewhere.

Grzenio
+1  A: 

What you want is an XNOR operation. Unfortunately this is not supported by C#/Mono. I think your solution is optimal.

Michael Krauklis
I wouldn't be surprised if the MS JIT compiler is able to optimize a ~(a^b) to an XNOR. CommuSoft should look at the generated assembly code to see if that's the case.
Steven
That's a good thought. I would be curious to see if that was optimized as well.
Michael Krauklis
I opened the Debug/Windows/Disassembly window in VS2008 to see what assembly gets generated and I see an XOR statement and a NOT statement. This would mean it is not optimized. However, we're talking about just 2 assembly instructions: this is blazingly fast.
Steven
That's too bad. Thanks for looking into it.
Michael Krauklis
+3  A: 

You are looking in the wrong place for this optimization; you won't end up finding any better bitwise operation here. Even if you did, it's hardly going to speed anything up. The real win will come from processing more than just a byte at a time. The processor is already having to do a bunch of bit shifting and masking operations just so that it can pretend for you that you are working with bytes. Process your arrays of bytes 1 word at a time, or use vector instructions if they are available.

Chris Marsh