Strange if statement

后端 未结 4 1551
甜味超标
甜味超标 2021-02-18 13:16

I found this strange if-statement in somebody else’s code:

if variable & 1 == 0:

I don\'t understand it. It should have two

4条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-18 14:14

    This code just checks if the lowest bit of variable is a 0. Based on operator precedence this is:

    if (variable & 1) == 0:
    

    First AND the lowest bit with one (extract just the lowest bit), then check if it is 0.

提交回复
热议问题