rhezo's blog

By rhezo, history, 8 years ago, In English

I was solving this problem. I have a function go, which returns a boolean true or false. When I replace "|" with "||", I get AC, otherwise I get TLE on test 6. I get AC with "||" and "or", but TLE with "|". Can anyone tell me the difference between the three?

  • Vote: I like it
  • +14
  • Vote: I do not like it

»
8 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

For booleans there is priority difference: http://en.cppreference.com/w/cpp/language/operator_precedence

Can you show your code? Possibly you use '|' and '&&' combination.

»
8 years ago, # |
  Vote: I like it +10 Vote: I do not like it

| is bitwise operator.

|| and "or" are logical operator.

As tyamgin said it could happen because of precedence.

"The & and | operators have lower precedence than comparison operators. That means that x & 3 == 1 is interpreted as x & (3 == 1), which is probably not what you want." Source

»
8 years ago, # |
  Vote: I like it +37 Vote: I do not like it

First of all, || and or are identical, but there is a big difference between | and ||.

|| is a boolean operator. a || b returns true or false. If a is non-zero, then the value of b is not checked (since the answer is guaranteed to be true).

| is bitwise-or. a | b returns a number (which can then be cast to a boolean if you need to). This evaluates both a and b, and then computes the bitwise or of them.

The most likely reason that || is not getting TLE is because it is not evaluating the second operand. This is especially true if the second operand is a complex function that has to be evaluated.