Could anybody help me to understand what the | OR bitwise operator doing in this function?
Here's the whole program btw.Code:SetConsoleTextAttribute ( h, BACKGROUND_RED | 8 );
http://cboard.cprogramming.com/showthread.php?t=110879
Printable View
Could anybody help me to understand what the | OR bitwise operator doing in this function?
Here's the whole program btw.Code:SetConsoleTextAttribute ( h, BACKGROUND_RED | 8 );
http://cboard.cprogramming.com/showthread.php?t=110879
What is it that you do not understand about it?
--
Mats
It would be better to go read the manual to find out what 8 really means (bold, flash, whatever), and use the appropriately named readable constant.
Not some magic number pulled out of the ether.
You mean such as:
FOREGROUND_INTENSITY
--
Mats
Well i already checked out the manual but it says nothing about numbers or bitwise operator.
And what is it that a color has to do with a bitwise operator?
http://msdn.microsoft.com/en-us/libr...47(VS.85).aspx
From the link, once you click on "character attributes":
The character attribute variable is just a bunch of flags, and each of these named constants is just a bitmask, so you can set whichever flags you want to set by ORing them together.Quote:
The foreground attributes specify the text color. The background attributes specify the color used to fill the cell's background. The other attributes are used with DBCS.
An application can combine the foreground and background constants to achieve different colors. For example, the following combination results in bright cyan text on a blue background.
FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY | BACKGROUND_BLUE
If no background constant is specified, the background is black, and if no foreground constant is specified, the text is black. For example, the following combination produces black text on a white background.
BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED
Im really lame. Im gonna pay more attention next time. Thank you Tabstop!
But what about the number 8, what does it mean?
8 is one of those named constants. If I wanted to know which one, I'd look in the corresponding header file and see which of them is defined to be 8.
My guess is that it's the foreground intensity bit [based on guess-and-vague-memory principle] that I posted about earlier, but if you have Visual Studio or some other editor that allows you to "go to definition" of that name, then go to the definition of the background colour, and the all declaration of the foreground and background colours are (I would expect) nearby, so just find which one has the value 8.
--
Mats
As stated by matsp:
From: http://doc.ddart.net/msdn/header/include/wincon.h.htmlCode:#define FOREGROUND_INTENSITY 0x0008 // text color is intensified.
You probably just shouldn't be using 8 in this context. But if you have 8, you can try to find the windows header where all these BACKGROUND_BLUE's and such are defined and see which of these is defined as 8.
In "wincon.h" you'll find the following:
Code:#define FOREGROUND_BLUE 1
#define FOREGROUND_GREEN 2
#define FOREGROUND_RED 4
#define FOREGROUND_INTENSITY 8
#define BACKGROUND_BLUE 16
#define BACKGROUND_GREEN 32
#define BACKGROUND_RED 64
#define BACKGROUND_INTENSITY 128
Thank you, anon. You sure know how to recognize different types of trees from quite a long way away. :)
Wow that was simple! Now either these kind of things arent in the tutorials or i must have missed them somehow.
Anyways, thanks a lot everybody!
Also notice that all the constants are powers of two (in binary these are numbers which have one bit set - each value a different bit). This is what allows them to be combined using bitwise or (in case you want to do something like this yourself)