Easiest way to flip a boolean value?

前端 未结 13 1450
后悔当初
后悔当初 2020-12-04 08:31

I just want to flip a boolean based on what it already is. If it\'s true - make it false. If it\'s false - make it true.

Here is my code excerpt:

swi         


        
13条回答
  •  南方客
    南方客 (楼主)
    2020-12-04 09:26

    For integers with values of 0 and 1 you can try:

    value = abs(value - 1);
    

    MWE in C:

    #include 
    #include 
    int main()
    {
            printf("Hello, World!\n");
            int value = 0;
            int i;
            for (i=0; i<10; i++)
            {
                    value = abs(value -1);
                    printf("%d\n", value);
            }
            return 0;
    }
    

提交回复
热议问题