🔒 Locked
Syntax question (&=)
Advertisement
Original Post
I'm reading a book on Visual C++.net and I have a syntax question. There's a line of code that sets a flag true to disable posting of the "Untitled" text at the top of a new (unsaved) document. This line is as follows: cs.style &= ~FWS_ADDTOTITLE; I've never seen this done before, but I think I can surmise what's happening. Does the &= set the following bit to true, regardless of the other flag settings? If that is the case, what is the '~' for? The book says the ADDTOTITLE style is on, otherwise I would just think that the tilde represents a negation (though that's not native to C++ syntax) and that the FWS_ADDONTITLE should be set false. Can someone clear this up for me?
Whenever you AND a bit with 1, it stays the same, and whenever you AND a bit with 0, it gets set to 0. So if you have an 8-bit value with one bit set:
00010000
And you want to combine it with another variable in such a way that this bit is unset, while all the rest are unchanged, you would AND with a value that looks like this:
11101111
Well it just so happens this is the inverse of the original value. Viola! It's really just bit logic, so you can experiment with it until you feel fully comfortable with what's going on.
00010000
And you want to combine it with another variable in such a way that this bit is unset, while all the rest are unchanged, you would AND with a value that looks like this:
11101111
Well it just so happens this is the inverse of the original value. Viola! It's really just bit logic, so you can experiment with it until you feel fully comfortable with what's going on.
What confuses me is the equals sign ('&='). I've always just used the '&' alone. Is this because we're ANDing the entire style byte and the = operator says do:
And if that's the case, what does the tilde do?
cs.style[0] & FWS_ADDTOTITLE[0];cs.style[1] & FWS_ADDTOTITLE[1];cs.style[2] & FWS_ADDTOTITLE[2];cs.style[3] & FWS_ADDTOTITLE[3];cs.style[4] & FWS_ADDTOTITLE[4];cs.style[5] & FWS_ADDTOTITLE[5];cs.style[6] & FWS_ADDTOTITLE[6];cs.style[7] & FWS_ADDTOTITLE[7];And if that's the case, what does the tilde do?
Quote:
Original post by civguy Quote:It's standard C++ (what made you think otherwise?), and it really is bitwise not (negation).
Original post by CyberSlag5k
I would just think that the tilde represents a negation (though that's not native to C++ syntax)
! is the NOT operator in C++. I've never used the tilde in C++ to represent a NOT. That is what I was asking.
But thank you for answering the question of what the tilde is.
Quote:
Original post by civguy Quote:
Original post by CyberSlag5k
What confuses me is the equals sign ('&=')
"a @= b" is shorthand for "a = a @ b" where @ is almost any operator.
Ha. I can't believe I didn't think about that.
Quote:Actually, a tilde inverts all the bits, a ! evaluates to true if the expression is 0.
Original post by CyberSlag5k Quote:
Original post by civguy Quote:It's standard C++ (what made you think otherwise?), and it really is bitwise not (negation).
Original post by CyberSlag5k
I would just think that the tilde represents a negation (though that's not native to C++ syntax)
! is the NOT operator in C++. I've never used the tilde in C++ to represent a NOT. That is what I was asking.
But thank you for answering the question of what the tilde is.
for example(2 is 01 in binary, and 3 is 11):
~0 == 1;
~2 == 1;
~3 == 0;
!0 == true;
!2 == false;
!3 == false;
Quote:There are logical and bitwise operators, and they shouldn't be confused with each other. E.g. && is logical and, & is bitwise and. Similarly ! is logical not, ~ is bitwise not.
Original post by CyberSlag5k
! is the NOT operator in C++. I've never used the tilde in C++ to represent a NOT.
Here's some examples to illustrate the difference:
!101011b = 000000b
!110111b = 000000b
!000000b = 000001b
~101011b = 010100b
~110111b = 001000b
~000000b = 111111b
Understanding the logic is left as an excercise for the reader.
Bitwise and Logical are different. && is logical and, & is bitwise and. ! is logical not, ~ is bitwise not.
Here is an example:
~0101 (expression)
----
1010 (result)
[edit: woah, 4 posts while I typed this...]
Here is an example:
~0101 (expression)
----
1010 (result)
[edit: woah, 4 posts while I typed this...]
Alright I think I see now. Lemme review just to make sure, though.
cw.style &= ~FWS_ADDTOTITLE; => cw.style = cw.style &= ~FWS_ADDTOTITLE;
That explains the &= (which was rather silly of me not to notice).
Now, FWS_ADDTOTITLE is some string of bits, let's stay 00000010, which represents the "Add to title" style. ~ is the bitwise NOT operator (as we've learned), whereas ! is the logical NOT operator. So, ~FWS_ADDTOTITLE = 11111101, which is ANDed to whatever cw.style is. ANDing the 1 bits won't change anything, but ANDing the 0 bit disables the ADDTOTITLE style feature.
Tada! Have I got everything straight now? I'll asume I do unless somebody posts otherwise.
In any case, thanks alot everyone!
cw.style &= ~FWS_ADDTOTITLE; => cw.style = cw.style &= ~FWS_ADDTOTITLE;
That explains the &= (which was rather silly of me not to notice).
Now, FWS_ADDTOTITLE is some string of bits, let's stay 00000010, which represents the "Add to title" style. ~ is the bitwise NOT operator (as we've learned), whereas ! is the logical NOT operator. So, ~FWS_ADDTOTITLE = 11111101, which is ANDed to whatever cw.style is. ANDing the 1 bits won't change anything, but ANDing the 0 bit disables the ADDTOTITLE style feature.
Tada! Have I got everything straight now? I'll asume I do unless somebody posts otherwise.
In any case, thanks alot everyone!
Quote:
Actually, a tilde inverts all the bits, a ! evaluates to true if the expression is 0.
for example(2 is 01 in binary, and 3 is 11):
~0 == 1;
~2 == 1;
~3 == 0;
!0 == true;
!2 == false;
!3 == false;
~0 is platform dependant (and by platform, I mean hardware platform).
If you're dealing with 2-bit numbers, then ~0 would be 3 (11b)
With 32-bit numbers, ~0 is 4294967295 (11111111111111111111111111111111b)
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement