Barry Andersen
Barry Andersen

Reputation: 609

julia language bitwise or in if statement

I am using Julia 0.3.0 on Windows 8.1

I enter the following:

julia> Y, M = 2000, 2
(2000,2)

julia> if M == 1 | M == 2
           Y -= 1
           M += 12
       end

julia> Y, M
(2000,2)

I expected Y = 1999, M = 14

Apparently this is not the way to use the bitwise or ( | ) How do I achieve the desired result?

Upvotes: 2

Views: 726

Answers (1)

IainDunning
IainDunning

Reputation: 11644

Use || (http://docs.julialang.org/en/latest/manual/control-flow/#man-short-circuit-evaluation) for control flow or.

The issue in this case is the operator precedence: | is bitwise-or which is higher than equality, see http://docs.julialang.org/en/latest/manual/mathematical-operations/. It works if you do (M==1)|(M==2), for example. Its unclear if the precedence should change, people are talking about the issue at least.

Upvotes: 3

Related Questions