top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Is there a case where '&' yields a lower performance than '%'? Does compiler optimize it automatically?

+2 votes
147 views

The following two code sequences produce the same result:

uint mod = val % 4;   
uint mod1 = val & 0x3;

I can use both to calculate the same result. I know that in hardware the & operator is much more simpler realised than the % operator. Therefore I expect it to have a better performance than the % operator.

posted May 12, 2016 by Pratiksha Shetty

Looking for an answer?  Promote on:
Facebook Share Button Twitter Share Button LinkedIn Share Button

Similar Questions
+3 votes
int32 find_result(int64 sec)
{

    if(config == defualt_base)
    {
        uint32 H      = j * k + 1; /*(here j and k global)*/

        if((((uint32)(sec - CS - RS)) % (H * N_p * M_r))== 0) 
        {
            result = 0;
        }
    }
    else
    {
        if((((uint32)(sec - CS - RS)) % (N_p * M_r))== 0)  /*(N_p and M_r global)*/
        {
            result = 0;
        }
    }
}

This function have to be execute several times in a second, How can optimize it ? Please help me to avoid mathematical operations applying on this function?

...