没有赋值运算符的按位非操作 - 可能吗?

Bitwise NOT operation without the assignment operator - posible?

我知道我可以用这种方式做无赋值的加法运算 intValue++ / ++intValue 而不是 intValue = intValue+1.

我想知道是否可以做同样的事情,但使用按位非运算。

我猜测语法可能性很小,唯一没有以编译错误结束的是 ~~intValue。但是当我打印变量时我没有看到任何变化。

我为什么要这样做:

我知道如果我在执行请求的操作时使用赋值操作(这种情况:"bitwise not"),那么根据我的逻辑,这就是低级别发生的事情:

1) Allocate temporary memory area for the data type

2) Copy the value of intValue to the temporary memory area

3) Perform the requested operation on the temporary memory area (so the result is in the temporary area)

4) Copy the result from the temporary memory area to the memory area of intValue

5) I'm not really sure about this: deallocate the temporary memory area.

现在,我知道如果变量存储小值,这不是真正的问题。

但是如果变量是一组 RGB 颜色(图像数据)呢?这是我的情况。如果我在这里写的是正确的,那么这意味着它会复制以执行请求的操作。这是大数据的情况。

所以这就是为什么我要这样"bitwise not"。

目前我正在使用 OpenCV 中的函数来执行请求的操作,我是这样做的:

Mat matValue = imread("bmpTest.bmp", CV_LOAD_IMAGE_COLOR);
bitwise_not(matValue, matValue);
imshow("test", matValue);
waitKey(0);

因为这个函数的第二个参数是dst(destination),那么恐怕这个函数的操作方式就是我描述的4、5步以上。

注意:如果此问题中存在措辞问题 - 请建议我如何解决此问题。英语不是我的母语。说的不够清楚请见谅

感谢各位帮手

我敢肯定,如果您不将其分配给任何东西,则为 ++foo 和 foo++ 生成的程序集是相同的。 但是正如你所说,如果你做 foo=~foo 需要额外的操作。 您可以检查这个简单的测试以了解我的意思:https://godbolt.org/g/he4xRP 否则,如果你在局部变量中执行 =~ 操作,它会直接在它所在的寄存器中进行操作,因此只是一条指令。

据我所知,c/c++

中没有~~或~=这样的东西

OpenCV 足够智能,可以在不需要时避免内存分配和复制。

由于 srcdst 矩阵在 bitwise_not(src, dst) 中是相同的,所以您不分配新内存也不复制结果。您就地执行此操作。

实际情况是这样的:

  1. 创建目标矩阵
    • 如果dst等于src,不分配任何东西。使 dst 指向与 src
    • 相同的数据
    • else 分配 dstsrc
    • 相同的大小和类型
  2. 执行操作:dst(i) = op(src(i))(这里的op是按位非)

注意:

bitwise_not(src, dst);
dst = ~src;

并不完全等价,但最后dst = ~src会调用bitwise_not(src, dst)