bit manipulation - C# - fast way to compare 2 integers, bit by bit, and output for more than one integer, possible? -
i have 2 input integer numbers , output list< int> myoutputlist. inputs lets say
a=0x110101
b=0x101100
then have calculated c integer number depending on , b numbers.i coded algorithm, can calculate c integer. c integer shows bits should changed. 1 value represents changing bits, 0 values represents unchanging bits. 1 bit should changed in each time. c integer depends on , b inputs, 1 bit, 3 bits, 8 bits needs changed. in given , b values, have c integer follows
c=0x 010010 (1 represents changing values; second , fifth bits should changed in case)
as c integer has value "1" 2 times; there should 2 results in case
result 1-changing second bit, other bits same a(0x110101) :
change second bit of => d1=1101 1 1
result 2-changing fifth bit, other bits same a(0x110101) :
change fifth bit of => d2=1 1 0101
what thinking using loop, shifting , c step step, , using &1 mask c? , check if equal "1"
for(i=0;i<32;i++) {int d=(c>>i)&1; //i tried check if i.th value equal 1 or not if(d==1) { int e=(a&(~(2^i))) | ((2^i)&(~b)) //in first brackets, removed i.th bit of a, replaced "not b" value. myoutputlist.add(e); } }
i need lots of calculations disturbing issue need check (d==1) 32 times. use many million times, calculations take 2 mins. looking faster way. there idea, trick?
i hope understood question right.
you looking xor operator.
c = ^ b 110101 b 101100 -------- c 011001
xor 1 if 2 inputs "different". see:
| | b | xor | |---+---+-----| | 0 | 0 | 0 | | 0 | 1 | 1 | | 1 | 0 | 1 | | 1 | 1 | 0 |
then able loop through bits of c
this:
for (int = 0; < 32; i++) { bool bit = (c & (1 << i)) != 0; }
Comments
Post a Comment