optimization - Bit wise operator -


take number of following type 2, 2^2(=4), 2^3(=8) , 16 ,32 , 64 ...

if make oring (|) of 2 numbers

2| 4 = xyz  0010 | 0100 = 0110  

then checking whether number xyz contains of given numbers make anding of xyz number , if number again can contains number

0110 & 0010 = 0010  

can use property optimizing our code ? or in other practical solution ?

this not quite same thing, "practical solution" using bit-wise operators using them avoid branching.

example:

if(input < b)     output = up; else if(input > a)     output = down; 

can replaced bit-wise operations like:

output = ~(!(input < b) * -1) & | ~(!(input > a) * -1) & down; 

in case, run lines many millions of times day benefit of small decrease in time take run outweighs time needed code little more complex instead of simple if/else if avoid branching.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -