What are "&" and "|" used for color generation code?

Just stumbled upon a series of videos for developing games on YouTube using designbyzephy and decided that learning and understanding the code from the video would be my next Java tutorial.

The biggest hurdle is trying to decrypt the code yourself, because the video creator has an incredibly newbie unfriendly approach to explaining everything.

So here is my question and video

video: http://www.youtube.com/watch?v=7eotyB7oNHE

at 5:31, he enters the code

int index = 0;
    for (int r = 0; r < 6; r++){
        for (int g = 0; g < 6; g++){
            for (int b = 0; b < 6; b++){

                int rr = (r * 255/5);
                int gg = (g * 255/5);
                int bb = (b * 255/5);

                colors[index++] = rr << 16 | gg << 8 | bb;
            }
        }
    }
colors[index++] = rr << 16 | gg << 8 | bb;

      

In my opinion, it populates the array with all the combinations generated 6 shades for each color, but what I don't get is symbol |

. He mentions in the video, he talks about it in previous videos, but it does not do this, I checked, and I'm sure he made a mistake in explaining &

to |

because it touches &

a bit, but never mentioned |

in any of his previous videos ... Another thing is that I don't understand why we are changing colors. He explains it in the video, but it still doesn't make sense to me. Basically, everything he says, we endure, because bb

, gg

andrr

all have 2 and 8 bits of data at their disposal, but that's not enough to explain. I need to know why we do it, why we need to move to the left, because the colors bb

, gg

, rr

have it 2 ^ 8 data bits, and that has 2 ^ In the first place, 8 data bits?

+3


source to share


1 answer


|

in java is bitwise or operator: Bitwise operations Despite all the videos, I think it is trying to make all color (RGB) values ​​into one 32Bit integer. Since rr, gg, bb can only have values ​​from 0-255, which only requires 8 bits, it can put them into one variable using shift and / or operations. For example:

rr:       00000000 00000000 00000000 10101010
rr<<16:   00000000 10101010 00000000 00000000

gg:       00000000 00000000 00000000 11110000
gg<<8:    00000000 00000000 11110000 00000000

bb:       00000000 00000000 00000000 00001111

value = rr << 16 | gg << 8 | bb 

rr<<16:   00000000 10101010 00000000 00000000
gg<<8:    00000000 00000000 11110000 00000000
bb:       00000000 00000000 00000000 00001111

value:    00000000 10101010 11110000 00001111
                      ^        ^        ^
                      rr       gg       bb

      



So now we have all three color values ​​in one integer variable.

+9


source







All Articles