Will a C conditional always return 1 or 0?

后端 未结 4 1359
不思量自难忘°
不思量自难忘° 2021-01-11 23:50

Do C conditional statements always return [1 or 0], or do they return [0 or \'something other than zero\']. I ask because:

pseudo code -

f

4条回答
  •  臣服心动
    2021-01-12 00:20

    Rather than right shifting and left shifting back again to clear the LSB, I would bitwise-and it with 0xFE:

    register = register & 0xFE;
    

    [edit: assuming register is 8 bits. If not, adapt right hand operand as necessary]

    But yes, if shouldSend is a result of a conditional test then it is guaranteed by the standard to be either 0 or 1. If there's any doubt about whether shouldSend could be generated from anywhere else it would be wise to put in the sort of precaution you have, or something like

    register = register | (shouldSend ? 1 : 0);
    

提交回复
热议问题