
January 6th, 2014, 06:49 PM
#1
[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)

January 6th, 2014, 10:50 PM
#2
Re: about logical bits
Originally Posted by Cambalinho
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:
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:
Regards,
Paul McKenzie
Last edited by Paul McKenzie; January 6th, 2014 at 11:05 PM.

January 7th, 2014, 02:18 AM
#3
Re: about logical bits
Originally Posted by Cambalinho
please anyone explain to me these, because i continue confused
Here's a article about lowlevel bit manipulation,
http://www.catonmat.net/blog/lowlev...elymustknow/

January 7th, 2014, 07:43 AM
#4
Re: about logical bits
Last edited by Cambalinho; January 7th, 2014 at 07:46 AM.

January 7th, 2014, 02:35 PM
#5
Re: about logical bits
Originally Posted by Cambalinho
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:
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:
And apply & with the wParam value. Here is where the ~ operator comes into play.
Say you have this value:
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 "insideout", and apply it to wParam using "&".
To turn the number insideout, 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 nontrivial 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 04:35 PM.

January 7th, 2014, 03:06 PM
#6
Re: about logical bits
Originally Posted by Paul McKenzie
Assume that your wParam is this:
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:
And apply & with the wParam value. Here is where the ~ operator comes into play.
Say you have this value:
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 "insideout", and apply it to wParam using "&".
To turn the number insideout, 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 nontrivial 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

January 7th, 2014, 04:39 PM
#7
Re: about logical bits
Originally Posted by Cambalinho
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

January 7th, 2014, 04:50 PM
#8
Re: about logical bits
Originally Posted by Paul McKenzie
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

Forum Rules

Click Here to Expand Forum to Full Width
This a Codeguru.com survey!
