在阅读 C++ 中的位图处理时,我遇到了这段代码 用于使用从位图文件中获取的数据加载调色板:
//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);
}
其中“bmih.biBitCount”是一个已经有值的预定义变量。 为什么作者声明 numColors 等于 1,然后在同一行中将值 bmih.biBitCount 赋给该变量?这到底是做什么的?像这样内联两次为变量赋值有什么好处?
最佳答案
Why does the author declare numColors to equal 1 then assign the value bmih.biBitCount to that variable in the same line?
他没有;他分配表达式 1 << bmih.biBitCount
的结果至 numColors
.分配发生在最后。 <<
是bitwise left shift operator .这样想:
//set Number of Colors
numColors = (1 << bmih.biBitCount);
关于c++ - 声明一个变量内联赋值C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12358072/