Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bitwise and/or with ternary operator

Look at this tiny snippet.

y<v|!v?:y=v;

(y is minimum value, and v is current compared value. This way would make you think easier.)

This snippet’s meaning is simple.
If current value v is smaller then minimum value y, set new minimum value(y=v). But v=0 case is excluded.

Then I thought if the 'adverse code' could be generated, the result should be same. I mean,

y>v&v?y=v:;

This code should do same thing. But it cannot be compiled. The error is as follows.

error: expected expression
  for(int v: a) v=abs(a[i]-v), x>v?:x=v, y>v&v?y=v:;
                                                   ^

It’s weird. I think two codes are same each other. If latter ternary operator is erroneous, former should have same problem. But it didn’t.

Can someone explain why?

Next question. I inserted a 0 to compile. y>v&v?y=v:0;
Then I got a false answer. So I changed & to &&. y>v&&v?y=v:0;
Finally I got a right answer. But without these process, using | operator can do all. Why?

<additional info>

My compiler version is as follows.

$ gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.4.0
Thread model: posix

And compile option:

g++ -std=c++11 my.cpp

If you want to have a sample code to test, this would help.

#include <iostream>
#include <vector>
using namespace std;
int working(int i, vector<int> a) {
  int y=INT_MAX;
  for(int v: a) v=abs(a[i]-v), y<v|!v?:y=v;
  return y;
}

int not_working(int i, vector<int> a) {
  int y=INT_MAX;
  for(int v: a) v=abs(a[i]-v), y>v&v?y=v:0;
  return y;
}

int main() {
  vector<int> b({-5,-2,2,7});
  cout << working(2, b) << endl;
  cout << not_working(2,b) << endl;
  return 0;
}

(p.s. correction of my poor english is always welcomed)

like image 237
plhn Avatar asked Aug 17 '15 14:08

plhn


People also ask

Can ternary operator have 3 conditions?

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.

When to use logical & and bitwise &?

Logical operators: Compare bits of the given object and always return a Boolean result. Bitwise operators: Perform operations on individual bits, and the result is also always a bit. Assignment operators: allow us to initialize an object with a value or perform specific operations on it. Miscellaneous operators.

Is || A Bitwise operator?

The bitwise operators should not be used in place of logical operators. The result of logical operators (&&, || and !) is either 0 or 1, but bitwise operators return an integer value. Also, the logical operators consider any non-zero operand as 1.

What is bitwise inclusive OR?

The bitwise inclusive OR operator ( | ) compares each bit of its first operand to the corresponding bit of its second operand. If either bit is 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0. Both operands to the operator must have integral types.


1 Answers

The codes do not do the same thing, because the conditions are not the negation of each other. Try it with y == 3, v == 2:

y < v | !v => 3 < 2 | !2 => false | !true => false | false => 0 | 0 => 0

y > v & v => 3 > 2 & 2 => true & 2 => 1 & 2 => 0

like image 197
Angew is no longer proud of SO Avatar answered Sep 29 '22 07:09

Angew is no longer proud of SO