Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

As far as i know these are bitwize operators?

it is not a problem but it got me puzzled

| & are bitwize operators 'common'

but they r used in a

logical context || | && & in c#

? how to not mislead the compiler

2 Answers

Relevance
  • Chris
    Lv 7
    5 years ago

    && is the logical AND. You use it to combine two bits / booleans, for instance like this:

    if (x > 0 && x < 11)

    If x is -2, the above will turn into

    if (false && true), then

    if (false), which will cause the subsequent commands to not happen.

    & in contrast is meant to combine the individual bits, usually of integers.

    For instance 10 & 7 will evaluate to 2, since 10 in binary is 1010 and 7 in binary is 0111. The only common bit is the third from the right, 0010, which is 2 in decimal.

    You can actually use

    if (x > 0 & x < 11) instead,

    but if you use 10 && 7 instead of 10 & 7, the compiler will either complain or turn any number other than zero into "true", which means 10 && 7 is simply "true" instead of 2.

  • 5 years ago

    AND

    OR

    XOR

Still have questions? Get your answers by asking now.