Skip to main content
gamedev.net
🔒 Locked

Syntax question (&=)

Started by CyberSlag5k Jan 12, 2005 at 4:18 PM 12 replies 950+ views
Original Post
CyberSlag5k
CyberSlag5k
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?
Without order nothing can exist - without chaos nothing can evolve.
Zipster
Zipster
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.
Ra
Ra
Quote:
Original post by CyberSlag5k
If that is the case, what is the '~' for?
~ is bitwise NOT. &= is the same as foo = foo & bar, which is bitwise AND.

cs.style &= ~FWS_ADDTOTITLE; unsets the FWS_ADDTOTITLE flag.
Ra
CyberSlag5k
CyberSlag5k
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:

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?
Without order nothing can exist - without chaos nothing can evolve.
civguy
civguy
Quote:
Original post by CyberSlag5k
I would just think that the tilde represents a negation (though that's not native to C++ syntax)
It's standard C++ (what made you think otherwise?), and it really is bitwise not (negation).
civguy
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.
CyberSlag5k
CyberSlag5k
Quote:
Original post by civguy
Quote:
Original post by CyberSlag5k
I would just think that the tilde represents a negation (though that's not native to C++ syntax)
It's standard C++ (what made you think otherwise?), and it really is bitwise not (negation).


! 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.
Without order nothing can exist - without chaos nothing can evolve.
CyberSlag5k
CyberSlag5k
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.
Without order nothing can exist - without chaos nothing can evolve.
Roboguy
Roboguy
Quote:
Original post by CyberSlag5k
Quote:
Original post by civguy
Quote:
Original post by CyberSlag5k
I would just think that the tilde represents a negation (though that's not native to C++ syntax)
It's standard C++ (what made you think otherwise?), and it really is bitwise not (negation).


! 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.
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;
Ra
Ra
~ is bitwise NOT, meaning it'll invert all the bits. ! is logical NOT, meaning it'll change true to false and false to true.
Ra
civguy
civguy
Quote:
Original post by CyberSlag5k
! is the NOT operator in C++. I've never used the tilde in C++ to represent a NOT.
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.

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.
choffstein
choffstein
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...]
CyberSlag5k
CyberSlag5k
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!
Without order nothing can exist - without chaos nothing can evolve.
daerid
daerid
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)
daerid@gmail.com

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.