简体   繁体   English

按位运算没有任何意义

[英]Bitwise operation aren't making any sense

my current understanding of bitwise operations is that it would push the binary reprisentation of a number a specific amount of times either removing numbers in the process (in case of >> ) or add 0's in the end of a number (in case of << ).我目前对按位运算的理解是,它会将数字的二进制表示推入特定次数,要么在过程中删除数字(在>>的情况下),要么在数字末尾添加 0(在<<的情况下) ). so why is it so when i have a int32 storing a hex value of int32_t color = 0xFFFF99FF; (= 1111 1111 1111 1111 1001 1001 1111 1111)那么为什么当我有一个 int32 存储十六进制值int32_t color = 0xFFFF99FF; (= 1111 1111 1111 1111 1001 1001 1111 1111) int32_t color = 0xFFFF99FF; (= 1111 1111 1111 1111 1001 1001 1111 1111) bitshifting right this int by 24 should give the value FF, because we moved the first two byts by the number of the byts remaining (32 - 8 = 24) but what actually happens is that i end up with the value -1 when i execute my code and 0 in calculator note: bitshifting right by 18 yeilds me the desired result. int32_t color = 0xFFFF99FF; (= 1111 1111 1111 1111 1001 1001 1111 1111)将这个 int 右移 24 位应该得到值 FF,因为我们将前两个字节移动了剩余的字节数 (32 - 8 = 24) 但实际发生的是当我执行我的代码时,我得到的值是 -1,而在计算器中的值是 0注意:右移 18 位得到了我想要的结果。

am using the SDL2 library and C++我正在使用 SDL2 库和 C++

am trying to store colors as their hex values then extract the red,green and blue chanel ignoring the alpha one.我试图将颜色存储为它们的十六进制值,然后提取忽略 alpha 的红色、绿色和蓝色香奈儿。 the code here is minimized without any unacessary details.这里的代码被最小化,没有任何不必要的细节。

int32_t color; //hex value of the color yellow


//taking input and changing the value of color based on it

if (event->type == SDL_KEYDOWN) {
    switch (event->key.keysym.sym)
    {
        case SDLK_a:
            //SDL_SetRenderDrawColor(renderer, 255, 255, 153, 255); // sand 
            color = 0xFFFF99FF; //hex code for sand color 
            break;
        case SDLK_z:
            //SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255); // water
            color = 0x0000FFFF; //hex code for blue color ...
            break;
        case SDLK_e:
            //SDL_SetRenderDrawColor(renderer, 139, 69, 19, 255); // dirt
            color = 0x8B4513FF;
            break;
        case SDLK_d:
            //SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); // delete button || air
            color = 0x000000FF;
            break;
        default:
            OutputDebugString("unhandled input.\n");
            break;
    }
}

//checking for mouse input and drawing a pixel with a specific color based on it
if (event-\>button.button == SDL_BUTTON_LEFT)
{
        SDL_SetRenderDrawColor(renderer, color \>\> 24, (color \>\> 16) - (color \>\> 24), (color \>\> 8) - ((color \>\> 16) - (color \>\> 24) + color \>\> 24));
        OutputDebugString(std::to_string(color \>\> 24).c_str());
        SDL_RenderDrawPoint(renderer, mouseX / 4, mouseY / 4);
        SDL_RenderPresent(renderer);
}

int32_t is signed and then >> is the signed shift, keeping the sign bit 31. You should use uint32_t . int32_t是有符号的,然后>>是有符号的移位,保持符号位 31。您应该使用uint32_t And it also is more logical to use uint8_t for color components.并且将uint8_t用于颜色分量也更合乎逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM