Monday, 15 February 2010

bit manipulation - C# - fast way to compare 2 integers, bit by bit, and output for more than one integer, possible? -


I have two input integer numbers and an output list & lt; Int> myoutputlist is my information - A = 0x 110101
B = 0x 101100

Then I have calculated the number of integer C. And B number I have already coded my algorithm, I can calculate the SI integer. The C integer shows when bits should be replaced. 1 represents the value biting, 0 value represents the unchanged bits. Only one bit should be changed at all times since the integer A and B depend on the inputs, sometimes 1 bit, sometimes 3 bits, sometimes 8 bits need to be changed. In this given A and B values, I have the whole integer I

C = 0x 0 1 00 1 0 (1 to the changing values Represents; second and fifth bits should be changed in this case)

The value of "1" as C integer is twice; In this case, there should be 2 results

Result 1 - Only changing the second bit, are similar to other bits A (0x110101):
A => D1 = 1101 Change the second part of 1 Result 2- Only changing the fifth bit is similar to the other bits A (0x110101): Change the fifth bit of A => D2 = 1 1 > 0101

What am I thinking, A and C steps for loop, and for C & amp; Using a 1 mask to use? And (i = 0; i <32; i ++) {int D = (C & gt; i) & amp; 1; Is equal to, check that it is equal to "1"

 . // I tried to check whether the ith value is equal to 1 or not (d == 1) {int E = (A & amp; (~ (2 ^ i)) ((2 ^ i) & amp; (~ B)) // In the first brackets, I removed a bit of A, then changed it with the "No B" value. Myoutputlist.add (e); }}  

I need to calculate a lot, but the issue of disturbing is that I have to check for 32 times (D == 1) I will use it several times , Some calculations take about 2 minutes. I'm exploring a fast way, is there any idea, move?

I hope that I understand your question correctly.

You XOR operator.

  C = A BA110101 B 101100 --------C111001  

XOR will always be 1 Two inputs are "different" see:

  | A. B XOR | | --- + --- + ----- | | 0 | 0 | 0 | | 0 | 1 | 1 | | 1 | 0 | 1 | | 1 | 1 | 0 |  

Then you can enable loop through the bits of C :

 for  (int i = 0 ; I & lt; 32; i ++) {bool bit = (C & amp; (1 & lt; & lt; i))! = 0; }  

No comments:

Post a Comment