Блог пользователя rhezo

Автор rhezo, история, 8 лет назад, По-английски

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?

  • Проголосовать: нравится
  • +14
  • Проголосовать: не нравится

»
8 лет назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

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 лет назад, # |
  Проголосовать: нравится +10 Проголосовать: не нравится

| 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 лет назад, # |
  Проголосовать: нравится +37 Проголосовать: не нравится

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.