[RESOLVED] about logical bits
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: [RESOLVED] about logical bits

  1. #1
    Join Date
    Apr 2009
    Posts
    626

    [RESOLVED] about logical bits

    can anyone explain to me the logical bit's?
    imagine these:

    wparam = MK_LBUTTON | MK_RBUTTON

    how can i test wparam for see if have 1 of that consts?
    if i need take off some of that consts i must use ~ for do it?
    (please anyone explain to me these, because i continue confused)

  2. #2
    Join Date
    Apr 1999
    Posts
    27,424

    Re: about logical bits

    Quote Originally Posted by Cambalinho View Post
    can anyone explain to me the logical bit's?
    http://en.wikipedia.org/wiki/Bitwise_operations_in_C

    Learning bitwise operations is a basic fundamental for many, if not most computer languages. It isn't obscure -- you need to know it.
    imagine these:

    wparam = MK_LBUTTON | MK_RBUTTON
    how can i test wparam for see if have 1 of that consts?
    In binary, what is MK_LBUTTON equal to? In binary, what is MK_BUTTON equal to? How would you test if wparam has the "MK_BUTTON" bit on using the bit operations? Would you use "&"? Would you use "|"? Likewise, how would you test if the MK_RBUTTON bit is on in wparam?
    if i need take off some of that consts i must use ~ for do it?
    Instead of answering you outright, I think I leave it to you as a homework exercise. The reason why is once again, if you don't know how to handle bitwise operations, then you need to learn very fast since it is a basic fundamental of computer programming.

    One hint -- it takes more than ~ to turn a bit "off" in a binary number -- you need to use ~ and a bit operation applied to the original value. For example, if the number is:
    Code:
    0111000011110010
    and you want to turn off the third bit from the left without disturbing the other bits, what bit operation(s) and value would you use to do this? In other words, make the bits:
    Code:
    0101000011110010
    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; January 6th, 2014 at 10:05 PM.

  3. #3
    Join Date
    Jul 2013
    Posts
    246

    Re: about logical bits

    Quote Originally Posted by Cambalinho View Post
    please anyone explain to me these, because i continue confused
    Here's a article about low-level bit manipulation,

    http://www.catonmat.net/blog/low-lev...ely-must-know/

  4. #4
    Join Date
    Apr 2009
    Posts
    626

    Re: about logical bits

    Quote Originally Posted by razzle View Post
    Here's a article about low-level bit manipulation,

    http://www.catonmat.net/blog/low-lev...ely-must-know/
    thanks for that link.
    i don't know nothing about negative bits, but i can learn
    Paul McKenzie: sorry i can't answer that(at least for now)
    i think is: 0111000011110010~(1<<14) but maybe it's incorrect
    Last edited by Cambalinho; January 7th, 2014 at 06:46 AM.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,424

    Re: about logical bits

    Quote Originally Posted by Cambalinho View Post
    thanks for that link.
    i don't know nothing about negative bits, but i can learn
    Paul McKenzie: sorry i can't answer that(at least for now)
    i think is: 0111000011110010~(1<<14) but maybe it's incorrect
    Assume that your wParam is this:
    Code:
    0111000011110010
    To preserve all of the bits after a bitwise operation, the operation you want to use is "&" along with a bit value of 1111111111111111. See here:
    Code:
    0111000011110010
    &
    1111111111111111
    ------------------
    0111000011110010
    So if you apply "&" with the value of all 1's, you get the original wParam value again. So how do we change a single bit using the ~ operator, and you want to leave all the other bits in wParam alone? Somehow you want to generate this mask value:
    Code:
    1101111111111111
    And apply & with the wParam value. Here is where the ~ operator comes into play.

    Say you have this value:
    Code:
    0010000000000000
    Let's assume this value above is one of the "BUTTON" values (call it BUTTONVALUE), meaning this value turns on some BUTTON. So your original bit string (wParam) has this BUTTON value set to "on" (look at the third bit from the left in wParam). You now want to turn it off, but at the same time you don't want to change any other bits. You want to turn BUTTONVALUE "inside-out", and apply it to wParam using "&".

    To turn the number inside-out, you negate the BUTTONVALUE value (you make every 0 turn into 1, and every 1 turn into a 0) by applying the bitwise ~:
    Code:
    ~0010000000000000 == 1101111111111111
    Now you have your value that you can "&" with your wParam value:
    Code:
    0111000011110010
    &
    1101111111111111
    ------------------
    0101000011110010  <-- our new set of bits, with the third bit from the left now turned off.
    So the whole sequence would look like this:
    Code:
    wParam = 0111000011110010                  (Step 1)
    
    wParam = wParam & (~BUTTONVALUE) -->
    
    wParam = wParam & (~0010000000000000)  -->
    
    wParam = wParam &  1101111111111111 -->
    
    which finally yields this:
    wParam = 0101000011110010
    It doesn't take a lot of work to understand this. All you need to know is what and, or, and not do, as well as shifting left and right. Then come up with the appropriate sequence of operations to achieve your goal.

    One advice -- Never say that you don't understand this stuff in a general C++ programming forum, unless you're a rank beginner and have no idea about C++ (or any) programming, and especially so if you're doing non-trivial programming (as you tend to want to do if we look at your other posts). Instead, learn this on your own, as again, this is a fundamental aspect of programming, and especially in C++ (and most other languages).

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; January 7th, 2014 at 03:35 PM.

  6. #6
    Join Date
    Apr 2009
    Posts
    626

    Re: about logical bits

    Quote Originally Posted by Paul McKenzie View Post
    Assume that your wParam is this:
    Code:
    0111000011110010
    To preserve all of the bits after a bitwise operation, the operation you want to use is "&" along with a bit value of 1111111111111111. See here:
    Code:
    0111000011110010
    &
    1111111111111111
    ------------------
    0111000011110010
    So if you apply "&" with the value of all 1's, you get the original wParam value again. So how do we change a single bit using the ~ operator, and you want to leave all the other bits in wParam alone? Somehow you want to generate this mask value:
    Code:
    1101111111111111
    And apply & with the wParam value. Here is where the ~ operator comes into play.

    Say you have this value:
    Code:
    0010000000000000
    Let's assume this value above is one of the "BUTTON" values (call it BUTTONVALUE), meaning this value turns on some BUTTON. So your original bit string (wParam) has this BUTTON value set to "on" (look at the third bit from the left in wParam). You now want to turn it off, but at the same time you don't want to change any other bits. You want to turn BUTTONVALUE "inside-out", and apply it to wParam using "&".

    To turn the number inside-out, you negate the BUTTONVALUE value (you make every 0 turn into 1, and every 1 turn into a 0) by applying the bitwise ~:
    Code:
    ~0010000000000000 == 1101111111111111
    Now you have your value that you can "&" with your wParam value:
    Code:
    0111000011110010
    &
    0010000000000000
    ------------------
    0101000011110010  <-- our new set of bits, with the third bit from the left now turned off.
    So the whole sequence would look like this:
    Code:
    wParam = wParam & (~BUTTONVALUE) -->
    0111000011110010 = 0111000011110010 & (~0010000000000000 )-->
    0111000011110010 = 0111000011110010 &  1101111111111111 -->
    
    which finally yields this:
    wParam = 0101000011110010
    It doesn't take a lot of work to understand this. All you need to know is what and, or, and not do, as well as shifting left and right. Then come up with the appropriate sequence of operations to achieve your goal.

    One advice -- Never say that you don't understand this stuff in a general C++ programming forum, unless you're a rank beginner and have no idea about C++ (or any) programming, and especially so if you're doing non-trivial programming (as you tend to want to do if we look at your other posts). Instead, learn this on your own, as again, this is a fundamental aspect of programming, and especially in C++ (and most other languages).

    Regards,

    Paul McKenzie
    thanks for all.
    the problem are the books\tutorials, why? because they tell us what is &,|,~, but only show us with bits and not like you show me
    thanks for all... thanks

  7. #7
    Join Date
    Apr 1999
    Posts
    27,424

    Re: about logical bits

    Quote Originally Posted by Cambalinho View Post
    thanks for all.
    the problem are the books\tutorials, why? because they tell us what is &,|,~, but only show us with bits and not like you show me
    thanks for all... thanks
    Note -- I edited my post, as I had copied / pasted a wrong bit pattern, and expanded on the sequence of events.

    The issue I think is that many are taught what the bitwise operations are, but never taught what they can be used for or how to use them in a real program.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Apr 2009
    Posts
    626

    Re: about logical bits

    Quote Originally Posted by Paul McKenzie View Post
    Note -- I edited my post, as I had copied / pasted a wrong bit pattern, and expanded on the sequence of events.

    The issue I think is that many are taught what the bitwise operations are, but never taught what they can be used for or how to use them in a real program.

    Regards,

    Paul McKenzie
    true.. thanks for all.. thank you

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center