什么这个前pression是什么意思? (在C SetConsoleTextAttribute功能) [英] What do this expression mean? (SetConsoleTextAttribute function in C)
问题描述
我在我们的C编程类任务一创建一个控制台中间为主的小游戏,我决定加入文字颜色和文字的背景,使其更presentable和独特的。
虽然我对我的追求寻找解决的办法,我发现这个方便的功能,将只做我想为我的项目,但问题是,有这部分我不明白的方式:
WORD wColor =((BackC&安培;为0x0F)LT;< 4)+(ForgC&安培;为0x0F);
其中, BackC
和 ForgC
给出整数数据类型字
仅仅是一个unsigned short int类型的typedef。具体来讲,我不明白的是((BackC&安培;为0x0F)LT;< 4)+(ForgC&安培;为0x0F)
部分。谁能帮助我?我知道我可以只使用功能,但我真的想知道的功能是如何工作的...谢谢!
下面是完整的源代码code(colorExample.c)
的#include<&WINDOWS.H GT;
#包括LT&;&stdio.h中GT;
无效SetColorAndBackground(INT ForgC,诠释BackC);
诠释的main()
{
SetColorAndBackground(10,1); //颜色值范围0上行至256
的printf(什么是文字的背景色\\ n);
SetColorAndBackground(11,1);
的printf(这个怎么样?);
残培();
返回0;
}
无效SetColorAndBackground(INT ForgC,诠释BackC)
{
字wColor =((BackC&放大器;为0x0F)所述; 4;)+(ForgC&放大器;为0x0F);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),wColor);
返回;
}
ForgC
和 BackC
两个值的实际上只能扩展到占用4位每个(你可以告诉,因为他们正在按位AND关系与为0x0F
,它清除所有,但第一个操作数的最后4位)。
因此,如果我们把他们看成8位宽的价值观,他们会形式
ForgC 0000xxxx
BackC 0000yyyy
然后你按位移 ForgC
4位到左侧,使得
ForgC xxxx0000
BackC 0000yyyy
然后你将它们添加together¹,使得
ForgC xxxx0000
BackC 0000yyyy
结果XXXXYYYY
那么,这样做的效果是结合两个值融入其中。 SetConsoleTextAttribute
然后可能再次它们分开,或者,是它可能会使用组合的价值。
¹从技术上讲,这应该是一个位或的,而不是整数加法。虽然在此特定的情况下(其中两个操作数都保证没有在相同的位置上的1位)都操作将产生相同的结果,按位或使得意图更加清楚。
I'm in a the middle of creating a console based small game for our C programming class assignment and I decided to make it more presentable and unique by adding text colors and text backgrounds.
While I was on my quest searching for a solution, I've found this handy function that will do just the way I wanted for my project but the problem is that there is this part I do not understand:
WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
Where, BackC
and ForgC
are given integers and the data type WORD
is just a typedef for an unsigned short int. Specifically, what I don't understand is the ((BackC & 0x0F) << 4) + (ForgC & 0x0F)
part. Can anyone help me with this? I know that I can just use the function but I really want to know how the function works...Thanks!
Here is the full source code (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;
}
ForgC
and BackC
are two values that can really only expand to take up of 4 bits each (you can tell this because they are being bitwise ANDed with 0x0F
, which clears all but the last 4 bits of the first operand).
So if we look at them as 8-bit wide values, they would be of the form
ForgC 0000xxxx
BackC 0000yyyy
Then you bitwise shift ForgC
4 bits to the left, making
ForgC xxxx0000
BackC 0000yyyy
And then you add them together¹, making
ForgC xxxx0000
BackC 0000yyyy
Result xxxxyyyy
So what this does in effect is "combine" both values into one. SetConsoleTextAttribute
might then separate them again, or it might use the combined value as-is.
¹ Technically this should be a bitwise OR instead of integer addition. Although in this specific case (where the two operands are guaranteed to not have an 1-bit in the same position) both operations will produce the same result, bitwise OR makes the intent clearer.
这篇关于什么这个前pression是什么意思? (在C SetConsoleTextAttribute功能)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!