我正在为我们的C编程类作业创建基于控制台的小型游戏,并且决定通过添加文本颜色和文本背景使其更加可视化和独特。这个表达意味着什么? (C中的SetConsoleTextAttribute函数)
虽然我对我的追求寻找解决的办法,我发现这个方便的功能,将做到这我想为我的项目,但问题是,有这部分我不明白的方式:
WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
其中,BackC
和ForgC
是整数,数据类型WORD
只是unsigned short int的typedef。具体来说,我不明白的是((BackC & 0x0F) << 4) + (ForgC & 0x0F)
部分。谁能帮我这个?我知道我可以使用该功能,但我真的很想知道该功能是如何工作的......谢谢!
以下是完整的源代码(colorExample.c)
#include <windows.h>
#include <stdio.h>
void SetColorAndBackground(int ForgC, int BackC);
int main()
{
SetColorAndBackground(10,1); //color value range 0 up-to 256
printf("what is text background color \n");
SetColorAndBackground(11,1);
printf("how about this?");
getch();
return 0;
}
void SetColorAndBackground(int ForgC, int BackC)
{
WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
return;
}
http://en.wikipedia.org/wiki/Bitwise_operation – 2011-09-24 14:30:48