How does a unary minus operator work in Boolean languages ​​in C ++?

I am currently converting some OpenCV code from C ++ to Java. I can’t use JavaCV, since we need to convert to native Java, not JNA. At some point in the code, I get the following assignment:

dst[x] = (uchar)(-(kHit >= kForeground));

Where dst- uchar*, kHitand kForeground- ints.

I could not find anything about how this works, and Java does not recognize it as an operation. The operation on these two variables is performed at another point in the code and one of two values ​​is stored: 255 or 0.

This code comes from opencv/video/src/bgfg_gaussmix.cpp.

+5
source share
4 answers

++ - 0 1. - , 0 -1. -1 uchar, 255.

Java :

dst[x] = (kHit >= kForeground) ? 255 : 0;

- , . , Java .

+7

kHit >= kForeground true, false, ++ 1 0. -1 0. uchar ((uchar)) 0 0 255 -1.

Konrad, , . , .:)

+6

:

kHit >= kForeground

- bool

-(kHit >= kForeground)

bool int ( true==1 false==0) , true==-1 false==0.

uchar, -1==255 0==0.

, , , , , ++ C, .

Java , :

dst[x] = (kHit>=kForeground) ? 255 : 0;
+1

(kHit >= kForeground) , true false. -, bool int, 1 true 0 false. -1 0, uchar .

, , operator- , int, . :

template <typename T, typename U>
struct same_type {
    static const bool value = false;
};
template <typename T>
struct same_type<T,T> {
    static const bool value = true;
};
template <typename T>
void f( T value ) {
    std::cout << "Is int? " << std::boolalpha << same_type<T, int>::value << "\n";
    std::cout << "Is bool? " << same_type<T, bool>::value << "\n";
}
int main() {
    f(-true);
}

f int bool same_type ( ). f -true , T -true. , , Is int? true\nIs bool? false.

+1

All Articles