Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# logical operation question

why this is true:

(true | false & false)

and this is false:

(true | false && false)

in my mind should be the oposite..

like image 860
Rodolfo Avatar asked Feb 11 '11 11:02

Rodolfo


1 Answers

They bind as:

true | (false & false)  // true

and

(true | false) && false  // false

I would avoid writing code which relies on these rules though - it's obviously unclear to the reader :)

For reference, see section 7.3.1 of the C# 4 language specification, which shows & having higher precedence than | (hence the first result) and | having higher precedence than && (hence the second result).

like image 184
Jon Skeet Avatar answered Oct 06 '22 02:10

Jon Skeet