Click here to Skip to main content
16,006,768 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: What is the difference?? Pin
toxcct29-Mar-06 3:53
toxcct29-Mar-06 3:53 
AnswerRe: What is the difference?? Pin
includeh1029-Mar-06 4:43
includeh1029-Mar-06 4:43 
QuestionQuestion Pin
big_denny_20029-Mar-06 3:33
big_denny_20029-Mar-06 3:33 
AnswerRe: Question Pin
khan++29-Mar-06 3:50
khan++29-Mar-06 3:50 
GeneralRe: Question Pin
big_denny_20029-Mar-06 3:58
big_denny_20029-Mar-06 3:58 
GeneralRe: Question Pin
khan++29-Mar-06 4:08
khan++29-Mar-06 4:08 
GeneralRe: Question Pin
Gavin Taylor29-Mar-06 4:12
professionalGavin Taylor29-Mar-06 4:12 
AnswerRe: Question Pin
toxcct29-Mar-06 4:34
toxcct29-Mar-06 4:34 
i don't exactly get you ; however, if i understand well, you want to know this :
let go deeper in the instruction BS_STYLE1 | BS_STYLE3 | BS_STYLE6.

firstly, we need to know what are defined those constants like.
let's consider they're declared this way :
enum {
    BS_STYLE1 =  1,
    BS_STYLE2 =  2,
    BS_STYLE3 =  4,
    BS_STYLE4 =  8,
    BS_STYLE5 = 16,
    BS_STYLE6 = 32
};

to understand why these constants' values are chosen, we must go in binary representation to understand :
 1 -> 0b 00000000 00000000 00000000 00000001
 2 -> 0b 00000000 00000000 00000000 00000010
 4 -> 0b 00000000 00000000 00000000 00000100
 8 -> 0b 00000000 00000000 00000000 00001000
16 -> 0b 00000000 00000000 00000000 00010000
32 -> 0b 00000000 00000000 00000000 00100000

notice that for those special values that are multiples of 2, only one bit is set at a time, all the others are equal to 0.
the interrest in this is that each bit is a flag for a functionnality (style here), and we can mix them in once variable to avoid using as many booleans as we need flags.
for example, see the following case :
 0b 00000000 00000000 00000000 00100101
 
Flags of <code>Style6</code>, <code>Style3</code> and <code>Style1</code> are set


now, as we don't handle binary directly in C++, we use bitwise operators instead.
what you see as that vertical bar | is the bitwise OR operator.
the OR operator works like this :
 x y  |
--------
 0 0  0
 0 1  1
 1 0  1
 1 1  1

in the case of the instruction BS_STYLE1 | BS_STYLE3 | BS_STYLE6, it so ORs like this :
 0b 00000000 00000000 00000000 00000001   <- BS_STYLE1
 0b 00000000 00000000 00000000 00000100   <- BS_STYLE3
 0b 00000000 00000000 00000000 00100000   <- BS_STYLE6
-----------------------------------------------
 0b 00000000 00000000 00000000 00100101   <- BS_STYLE1 | BS_STYLE3 | BS_STYLE6


Now, the "mix value" is constructed, we can pass it to a function.
but how does the function know which bits are set, and which are not ?
easy... follow me !

Lets say we want to know if the bit of BS_STYLE1 is set from the mix we get in parameter (let's call it MIX_VAR.
we must create a maskwhich we'll AND with the :
STYLE1_MASK = 0b 00000000 00000000 00000000 00000001

as you see, the mask is the same as the STYLE constant, so in practice, you'll never see defined such mask :
MIX_VAR  -> 0b 00000000 00000000 00000000 00100101
MASK1    -> 0b 00000000 00000000 00000000 00000001
           ----------------------------------------
<code>Bitwise AND</code> 0b 00000000 00000000 00000000 00000001

if the result is equal to the STYLE1, then it is because the bit was set. otherwise, you get 0.

ok, now, in practice, here is how you'll often see it :
void fooFunc(DWORD dwMix) {
    if (BS_STYLE1 & dwMix) == BS_STYLE1) {
        //Apply style 1
    }
    else if (BS_STYLE2 & dwMix) == BS_STYLE2) {
        //Apply style 2
    }
    else if (BS_STYLE3 & dwMix) == BS_STYLE3) {
        //Apply style 3
    }
    //etc...
}


wow, that's very long finally... hum, well, i hope it will help, truly ! Cool | :cool: Rose | [Rose]
GeneralRe: Question Pin
Eytukan29-Mar-06 5:29
Eytukan29-Mar-06 5:29 
GeneralRe: Question Pin
Wes Aday29-Mar-06 6:26
professionalWes Aday29-Mar-06 6:26 
GeneralRe: Question Pin
big_denny_20029-Mar-06 6:57
big_denny_20029-Mar-06 6:57 
GeneralRe: Question Pin
toxcct29-Mar-06 20:16
toxcct29-Mar-06 20:16 
QuestionAbout sending mail Pin
mkoliv29-Mar-06 3:32
mkoliv29-Mar-06 3:32 
QuestionRe: About sending mail Pin
David Crow29-Mar-06 4:43
David Crow29-Mar-06 4:43 
AnswerMessage Closed Pin
29-Mar-06 21:44
mkoliv29-Mar-06 21:44 
GeneralRe: About sending mail Pin
David Crow30-Mar-06 1:57
David Crow30-Mar-06 1:57 
QuestionTreeView Control Problem Pin
gbruyneel29-Mar-06 3:00
gbruyneel29-Mar-06 3:00 
AnswerRe: TreeView Control Problem Pin
Gavin Taylor29-Mar-06 3:27
professionalGavin Taylor29-Mar-06 3:27 
GeneralRe: TreeView Control Problem Pin
gbruyneel29-Mar-06 21:23
gbruyneel29-Mar-06 21:23 
QuestionHow to access HKEY_CURRENT_USER from local system account? Pin
khb29-Mar-06 1:54
khb29-Mar-06 1:54 
AnswerRe: How to access HKEY_CURRENT_USER from local system account? Pin
James R. Twine29-Mar-06 2:49
James R. Twine29-Mar-06 2:49 
GeneralRe: How to access HKEY_CURRENT_USER from local system account? Pin
khb29-Mar-06 3:08
khb29-Mar-06 3:08 
AnswerRe: How to access HKEY_CURRENT_USER from local system account? Pin
David Crow29-Mar-06 3:16
David Crow29-Mar-06 3:16 
GeneralRe: How to access HKEY_CURRENT_USER from local system account? Pin
khb29-Mar-06 3:29
khb29-Mar-06 3:29 
QuestionCan I return an array of pointers of type unsigned char[] Pin
Laughing Buddha29-Mar-06 1:52
Laughing Buddha29-Mar-06 1:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.