While reading about bitmap processing in C++ I came across this block of code
used for loading a color palette with data taken from a bitmap file:
//set Number of Colors
numColors = 1 << bmih.biBitCount;
//load the palette for 8 bits per pixel
if(bmih.biBitCount == 8) {
colours=new RGBQUAD[numColours];
fread(colours,sizeof(RGBQUAD),numColours,in);
}
where “bmih.biBitCount” is a predefined variable that already has a value.
Why does the author declare numColors to equal 1 then assign the value bmih.biBitCount to that variable in the same line? what exactly does this do and what are the benefits of assigning a variable a value twice inline like this?
He doesn’t; he assigns the result of the expression
1 << bmih.biBitCounttonumColors. The assignment occurs last.<<is the bitwise left shift operator. Think of it this way: