16-Bit Shifting

Started by
3 comments, last by PsYcHoPrOg 24 years, 2 months ago
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.
D:
Advertisement

<
char r=0, g=0, b=0;
for(int n = 1; n<65536; N<<=1)
{
IF(ddsd.ddpfPixelFormat.dwRBitMask & N)
++R;
IF(ddsd.ddpfPixelFormat.dwRBitMask & N)
++G;
IF(ddsd.ddpfPixelFormat.dwRBitMask & N)
++B;
}

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

rgb16->dwGBitMask = ddsd.ddpfPixelFormat.dwGBitMask;
rgb16->Position.rgbGreen = b;

rgb16->dwBBitMask = ddsd.ddpfPixelFormat.dwBBitMask;
rgb16->Position.rgbBlue = 0;
>
D:
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.
===========================There are 10 types of people in the world. Those that understand binary and those that don't.( My views in no way reflect the views of my employer. )
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


Carl "trixter"[email=carl@trixoft.com]carl@trixoft.com[/email]http://www.trixoft.com
Thank you both. I understand completely now. Thank you for answering my post.
D:

This topic is closed to new replies.

Advertisement