Using bitwise operators

后端 未结 12 1656
既然无缘
既然无缘 2021-01-06 08:21

I\'ve been studying C# and ran accross some familiar ground from my old work in C++. I never understood the reason for bitwise operators in a real application. I\'ve never u

12条回答
  •  再見小時候
    2021-01-06 08:53

    There is a reason that, depending on the kind of person, can be important: cool-iness! ;)

    Take this, an algorithm to calculate the absolute value of a number, without using any conditional branch instruction:

    int abs(const int input) {
       int temp = A >> 31;
       return ( input ^ A ) - A;
    }
    

    A reason to avoid conditional branching is that it could stop your processor pre-fetching, waiting for the condition to be verified to know to which value the program counter should be set.

    So, a part from the joke, there are very good technical reasons to do it.

提交回复
热议问题