Why '&&' and not '&'?

前端 未结 16 1534
别跟我提以往
别跟我提以往 2020-11-27 12:11

Why is && preferable to & and || preferable to |?

I asked someone who\'s been programming for years a

16条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-27 13:05

    && and & mean two very different things and give you two different answers.

    1 && 2 yields 1 ("true")
    1 & 2 yields 0 ("false")

    && is a logic operator -- it means "true if both of the operands are true"
    & is a bitwise comparison. It means "tell me which of the bits are set in both of the operands"

提交回复
热议问题