Invert 1 bit in C#

前端 未结 2 1551
你的背包
你的背包 2021-02-12 15:37

I have 1 bit in a byte (always in the lowest order position) that I\'d like to invert. ie given 00000001 I\'d like to get 00000000 and with 00000000 I\'d like 00000

2条回答
  •  旧时难觅i
    2021-02-12 16:10

    Your solution isn't correct because if bit == 2 (10) then your assignment will yield bit == 0 (00).

    This is what you want:

    bit ^= 1;
    

提交回复
热议问题