Thread: SetConsoleTextAttribute

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    SetConsoleTextAttribute

    Could anybody help me to understand what the | OR bitwise operator doing in this function?

    Code:
    SetConsoleTextAttribute ( h, BACKGROUND_RED | 8 );
    Here's the whole program btw.
    http://cboard.cprogramming.com/showthread.php?t=110879
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What is it that you do not understand about it?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You mean such as:
    FOREGROUND_INTENSITY

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    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
    Using Windows 10 with Code Blocks and MingW.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Ducky View Post
    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 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
    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.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Im really lame. Im gonna pay more attention next time. Thank you Tabstop!

    But what about the number 8, what does it mean?
    Using Windows 10 with Code Blocks and MingW.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Ducky View Post
    Im really lame. Im gonna pay more attention next time. Thank you Tabstop!

    But what about the number 8, what does it mean?
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    As stated by matsp:
    Code:
    #define FOREGROUND_INTENSITY 0x0008 // text color is intensified.
    From: http://doc.ddart.net/msdn/header/include/wincon.h.html
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    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!
    Last edited by Ducky; 02-26-2009 at 10:50 PM.
    Using Windows 10 with Code Blocks and MingW.

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed