How to make a boolean variable switch between true and false every time a method is invoked?

后端 未结 9 1036
南旧
南旧 2020-12-13 09:01

I am trying to write a method that when invoked, changes a boolean variable to true, and when invoked again, changes the same variable to false, etc.

For example: ca

9条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-13 09:39

    value ^= true;
    

    That is value xor-equals true, which will flip it every time, and without any branching or temporary variables.

提交回复
热议问题