如何移位u16*的数据?

Cya*_*ime 1 c++ opengl bit-manipulation

对不起,如果这没有任何意义,但我正在尽我所能了解它.

所以我基本上有一个指向openGL中纹理数据的指针,它是16 bpp(pcx.image.data16),我需要打开每个像素的alpha位.所以我想出了这个,但你可以说它真的很糟糕.

for(int i = 0; i < (TEXTURE_SIZE_128 * TEXTURE_SIZE_128); i++){
        pcx.image.data16 |=  1 << ((16 * i) + 15);
    }
Run Code Online (Sandbox Code Playgroud)

我收到这些错误:

c:/Users/me/Desktop/neronds/source/neroedge.cpp:40:43: error: invalid operands of types 'u16* {aka short unsigned int*}' and 'int' to binary 'operator|'
c:/Users/me/Desktop/neronds/source/neroedge.cpp:40:43: error:   in evaluation of 'operator|=(u16* {aka short unsigned int*}, int)'
Run Code Online (Sandbox Code Playgroud)

如何修复这些错误,我是否正在改变每像素的alpha位?

01d*_*d55 5

试试这个:

for(unsigned i = 0; i < (TEXTURE_SIZE_128 * TEXTURE_SIZE_128); i++)
    {
        constexpr u16 ALPHA_BIT = 1 << 15;
        pcx.image.data16[i] |= ALPHA_BIT;
    }
Run Code Online (Sandbox Code Playgroud)

如果你的编译器还不支持constexpr(c ++ 11特性),请使用const.