Bitwise and / or with triple operator

Look at this little snippet.

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

( y is the minimum value, and v is the current comparable value. This method will make you think easier.)

The meaning of this fragment is simple.
If the current value of v less than the minimum value of y , set a new minimum value ( y=v ). But the case v=0 excluded.

Then I thought that if a "bad code" could be generated, the result should be the same. I mean,

 y>v&v?y=v:; 

This code should do the same. But it can not be compiled. The following error.

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

It’s strange. I think the two codes are the same. If the last ternary operator is wrong, the former should have the same problem. But this is not so.

Can someone explain why?

The next question. I inserted 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 the correct answer. But without this process, the operator | can do everything. Why?

<additional information>

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 the compilation option:

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

If you want to have sample code for testing, this will 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; } 

(ps correcting my bad english is always welcome)

+5
source share
3 answers

In this snippet:

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

v converted to bool and canceled with ! . Since both sides are bitwise or | are bools, | behaves like a logical or.

Otherwise:

 y>v&v?y=v:0; 

there is no conversion to bool , and instead the result y>v converted to int . Bitwise and & give different results, depending on the least significant bit v . It does not behave like a logical and.
This should work like the original:

 y>v&!!v?y=v:0; 

because there is a conversion to bool.

+2
source

Codes do not do the same, because conditions are not negation of each other. Try using y == 3, v == 2 :

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

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

+4
source

The syntax of a conditional statement is:

logical expression? true-expression: false-expression

In the first,

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

you have no true expression. I get the following compiler warning with g ++ when compiling with -Wall .

 socc.cc: In function 'int main()': socc.cc:14:12: warning: the omitted middle operand in ?: will always be 'true', suggest explicit middle operand [-Wparentheses] y<v||!v?:y=v; 

In the second,

 y>v&v ? y=v : ; 

You are missing a false expression. For some reason, g ++ treats this as an error instead of a warning.

You could fix this by providing a dummy value for both of them.

By the way, you use bitwise operators | and & . I am sure this is a small mistake.

You can use:

 (y<v || !v) ? 0 : y=v; 

or

 (y>v && v) ? y=v : 0; 

Update

Expression

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

is not legal C / C ++. It is supported by g ++ as an extension. More can be seen in the conditional statement C ('?') With an empty second parameter .

+2
source

All Articles