CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: | operator

  1. #1
    Join Date
    Oct 2000
    Posts
    147

    | operator

    Hello

    I'm creating my edit box in my OnInitdialog() like this. this works fine.

    CRect rect(100,100, 200, 130); // L,T,R,B
    CEdit* pEdit = new CEdit;
    DWORD dwstyles;
    dwstyles = WS_BORDER|WS_CHILD|WS_VISIBLE|ES_AUTOHSCROLL;
    pEdit->Create(dwstyles, rect, this, IDC_EDIT1);

    Now I want to make my edit box ES_NUMBER, but I don't want to just add it to my dwstyles, I want to learn how to do the or(|) thing. Can anyone explain me how to go about it.

    thanks





  2. #2
    Join Date
    Nov 1999
    Location
    Dresden / Germoney
    Posts
    1,402

    Re: | operator

    The '|' operator does an bitwise 'or'. In long terms c=a|b means:
    - look at a and b in binary
    - for each bit position: if the bit is set in a, or b, or both, set the bit in c, otherwise, clear it:
    a=0011
    b=1010
    ------
    c=1011

    Now for the window styles: they typically have only one bit set. so ES_FOO might be 0010 binary. The effect of '|' instead of '+' is: if the bit is already set, nothing bad happens. e.g. we have a=0011, (the ES_FOO bit is already set). if we now add them:
    0011
    +0010
    ----
    0101
    So other bits are affected, and the bit we wanted to set is actually re-set. Sing or:

    0011
    |0010
    ----
    0011
    everything is fine.

    Peter



  3. #3
    Join Date
    Oct 2000
    Posts
    147

    Re: | operator

    sorry, but I have no clue what you just said. Now for starter how do I find out the binary value of ES_FOO, and how do I know what is the ES_FOO(with the bit set).

    thanks


  4. #4
    Join Date
    Nov 1999
    Location
    Dresden / Germoney
    Posts
    1,402

    Re: | operator

    Hi,

    Well.. if you didn't hear of binary numbers before, perhaps this information is enough: If you add the ES_NUMBER style using '+', the result is wrong if the style was already set. Using '|', it works well even if ES_NUMBER is already set.

    There are other cases where binary operations are far more important, here it's just 'good style'.

    To understand hexadecimal & binary representation of numbers, you might want to grab a neat "math for dummies" book. It#s tough to make you UNDERSTAND in a few lines.

    Peter


  5. #5
    Join Date
    Aug 2000
    Location
    Winnipeg, Canada
    Posts
    344

    Re: | operator

    You don't actually need to know the binary values. All you need to know is how to use the bitwise-OR (|) and bitwise-AND (&) operations. Peter was just trying to explain why this works.

    To add a style:

    dwStyle |= ES_NUMBER;
    // OR
    dwStyle = dwStyle | ES_NUMBER;



    To subtract a style:

    dwStyle &= ~ES_NUMBER;
    // OR
    dwStyle = dwStyle & ~ES_NUMBER;



    BTW, "set" means 1, and "clear" means 0. "[Window styles] typically have only one bit set" means that they are binary numbers that have a single 1, with the rest as zeroes (e.g. 0010). He wasn't talking about a "set of bits".

    O-Deka-K


  6. #6
    Join Date
    Oct 2000
    Posts
    147

    Re: | operator

    thanks, this is what I was looking for.




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)