mardi 9 février 2021

How to check odd or even using bit wise operations without the branch /If condition?

I have a code condition such as following

for(int i=0;i<Number;i++)
{     
     int* pIn = pInputArr[i];
     int* pOut = pOutputArr[i];

     if (Input_number % 2 == 0)
       {
           pIn = pOutputArr[i];
           pOut = pInputArr[i];
       }
     else
       {
           pOut = pInputArr[i];
           pIn = pOutputArr[i];
       }
}

I wanted to replace it with a more efficient way in embedded programming since I was told that branch operations are costly in embedded programming. Is there a cleaner way to achieve this using bit operations and without the if conditions?. I checked similar queries in several posts but didn't find them useful. Any suggestions will be highly appreciated.

Aucun commentaire:

Enregistrer un commentaire