简体   繁体   English

使用C ++按位运算符查找匹配的颜色

[英]Finding matched color using C++ bitwise operator

I want to figure out whether jersey1 has a Blue pixel in it or not. 我想弄清楚jersey1中是否有一个蓝色像素。 Also, I have find this using bitwise operator. 此外,我发现使用按位运算符。 For doing so, should I use the bitwise and (&) or bitwise or (|). 为此,应使用按位和(&)或按位或(|)。 Can you please explain me how they differ from each other? 您能解释一下它们之间的区别吗? Thanks in advance. 提前致谢。

 const unsigned int GREEN= 0x0000FF00;  //green
 const unsigned int RED  = 0x00FF0000;    //red
 const unsigned int BLUE = 0x000000FF;   //blue
 const unsigned int RGB  = RED | GREEN | BLUE;

 unsigned int jersey1 = 123;

You use the bitwise and operator & to mask the values you are interested in. 您可以使用按位运算符&掩盖您感兴趣的值。

For example, to see if an blue bits are set you would say jersey1 & BLUE . 例如,要查看是否设置了蓝色位,您可以说jersey1 & BLUE If the value is 0 then there are no blue bits. 如果值为0,则没有蓝色位。 If it's non-zero then there is a blue bit set. 如果非零,则设置一个蓝色位。

You'll want to use a bitwise '&' to test if a blue pixel exists in jersey1 like the following: 您需要使用按位的'&'来测试jersey1中是否存在蓝色像素,如下所示:

if(jersey1 & BLUE)
{
   cout << "It's blue!" << endl;
}
else
{
   cout << "It's not blue!" << endl;
}

The '&' or "and" operator with the bits requires that both bits in the same position of each value (BLUE and jersey1) are equal to 1 for it to be true. 带这些位的'&'或“ and”运算符要求每个值在相同位置(BLUE和jersey1)的两个位都等于1才能使其为真。 Whereas, the '|' 而“ |” (or) operator only requires that one set of bits in one of the values is equal to 1. In the above example, had you used "bitwise or" (|), the if statement would always be true which is not what you want. (或)运算符仅要求其中一个值中的一组位等于1。在上面的示例中,如果您使用“按位或”(|),则if语句将始终为true,这不是您想要的。

You can think of the bitwise AND and OR operators as gates that either stop values (mask them out) or let them through. 您可以将按位AND和OR运算符视为停止值(将其屏蔽)或让其通过的门。 If you take a value and a mask, and compare their binary representations, then the AND operator will have a 1 anywhere that both have a 1. The OR operator will have a 1 wherever at least one of them has a 1. It looks like this, reading vertically: 如果您使用一个值和一个掩码,并比较它们的二进制表示形式,则AND运算符在任何一个都具有1的地方都将为1。或运算符将在其中至少一个为1的任何地方都为1。垂直阅读:

0 1 0 1
0 0 1 1
-------  AND
0 0 0 1

0 1 0 1
0 0 1 1
------- OR
0 1 1 1

Colors are frequently represented by hexadecimal values which contain 256 possible shades of each of the red, green, and blue components of the color. 颜色通常由十六进制值表示,十六进制值包含该颜色的红色,绿色和蓝色分量各自的256种可能的阴影。 The bytes are (usually) ordered RRGGBB. 字节(通常)是有序的RRGGBB。 Therefore you should convert your int 123 to hexadecimal to get 0x0000007B and then choose a mask which will determine the value that's in the "blue" position. 因此,您应该将int 123转换为十六进制以获得0x0000007B ,然后选择一个掩码,该掩码将确定在“ blue”位置的值。 {Actually, we can already see that there is a value in the blue position, so we could stop now...} Anyway, to simply mask a value down to its blue channel, we would AND with a mask which only passes through the values in that last position. {实际上,我们已经可以看到蓝色位置有一个值,所以我们现在就可以停止...}无论如何,要简单地将值屏蔽到其蓝色通道,我们将使用仅通过值在最后一个位置。 Since FF in hex gives 11111111 and ones pass values through, we can mask with 0000FF , which is the mask you were given for the blue channel (the BLUE const). 由于以十六进制表示的FF给出11111111并且一个值通过,因此我们可以使用0000FF进行掩码,这是为蓝色通道( BLUE const)指定的掩码。 You'd simply need to test the value of (jersey1 & BLUE) to see if it's nonzero to find out whether there's blue in that channel. 您只需要测试(jersey1 & BLUE)的值以查看它是否为非零值,以找出该通道中是否存在蓝色。

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

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