#### Archived

This topic is now archived and is closed to further replies.

# 16-Bit Shifting

This topic is 6553 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I did not understand several parts of the code that I looked at from one of the articles. One of those parts were the "N<<=1" in the for loop. The other part is in the next part, outside of the for loop. Why do the variables for the rgb16->position.rgbBlue, rgb->position.rgbRed, and rgb->position.rgbGreen equal what he made them equal. And why does rgb->position.rgbBlue = 0? Anyway, the code is in the next post. By the way, don''t comment on some of the syntax errors in the code. I took it directly from an article.

##### Share on other sites

<
char r=0, g=0, b=0;
for(int n = 1; n<65536; N<<=1)
{
++R;
++G;
++B;
}

// THIS IS THE PART THAT ASSUMES THE FORMAT IS RGB
rgb16->Position.rgbRed = g + b;

rgb16->Position.rgbGreen = b;

rgb16->Position.rgbBlue = 0;
>

##### Share on other sites
firstly N<<1 multiples N by 2. << is the shift left operator. ie it shifts the variable a certain number of places to the left. eg if you have the binary number 0010 and use 0010<<1 you get 0100. As each digit is a power of 2 this multiples the number by two. In the example 2 becomes 4. Hope that makes sense.

For the second part, this is because of how the colour information for the pixel is stored. For 16 bit pixels it is usually RRRRRGGGGGGBBBBB. The mask that DDraw returns is 1111100000000000 for red, 11111100000 for green and 11111 for blue. The for loop sets B to 5, G to 6 and R to 5 ie the number of bits for each colour value. BTW the code in the for loop is wrong, the second and third bitmasks should be for green and blue respectively.

The start position for red is therefore the number of bits for green and the number of bits for blue. ie g+b. The start position for green is the number of bits for blue ie b. As blue is the first colour its start position is 0.

This may be better explained in the 16bit pixel plotting articles here on GameDev.net under the developer resources section.

No sense being pessimistic. It wouldn''t work anyway.

##### Share on other sites
n<<=1 does this

for time thru n = 1 = 00000000000000000000000000000001b
after 1st n<<=1: n = 2 = 00000000000000000000000000000010b
after 2nd n<<=1: n = 4 = 00000000000000000000000000000100b
and so on...

65536 = 00000000000000010000000000000000b

color bits: RRRRRGGGGGGBBBBB for RGB16 565
red position starts after the blue and green bits.
green position starts after the blue bits.
blue position starts at the beginning.

maybe this''ll help somehow

##### Share on other sites
Thank you both. I understand completely now. Thank you for answering my post.