How do I check if an integer is even or odd using bitwise operators

前端 未结 9 1476
迷失自我
迷失自我 2020-12-02 05:36

How do I check if an integer is even or odd using bitwise operators

9条回答
  •  广开言路
    2020-12-02 06:27

    Just a footnote to Jim's answer.

    In C#, unlike C, bitwise AND returns the resulting number, so you'd want to write:

    if ((number & 1) == 1) {
       // It's odd
    }
    

提交回复
热议问题