How does the unary minus operator work on booleans in C++?

前端 未结 4 1110
别跟我提以往
别跟我提以往 2021-01-19 00:55

I am currently converting some OpenCV code from C++ to Java. I can\'t use JavaCV, as we need the conversion in native Java, not a JNA. At one point in the code, I get the fo

4条回答
  •  死守一世寂寞
    2021-01-19 01:18

    kHit >= kForeground returns either true or false, which in C++ sort of means 1 or 0. The minus in front transforms this to -1 or 0. The cast to uchar ((uchar)) returns 0 for 0 and wraps to 255 for the negative -1.

    Following Konrad's comment, I'm also skeptical this is well defined. It is well defined, but it's still an awful piece of code in terms of readability. :)

提交回复
热议问题