I've to compare 4 variables a,b,c,d
if any of them is -1
return false
.
and How mush terse this could be ?
may be some mathematical operation could be done !! I dont like wasting so many characters or 开发者_如何学JAVAlines for this simple thing.
Normally:
return a!=-1 && b!=-1 && c!=-1 && d!=-1;
Since ~(-1) == 0
in 2's complement machine, and 0
is a false value, we could reduce the above to
return ~a && ~b && ~c && ~d;
or, not relying on 2's complement:
return a+1 && b+1 && c+1 && d+1;
but it has undefined behavior on overflow.
(But please use the normal way. You may forget what this clever hack is doing years later.)
If your numbers can be either non-negative or -1, then you can use the following:
return~(a|b|c|d);
(removed the white-space so it looks more terse)
精彩评论