🔒 Locked
MSDN Sample code
Advertisement
Original Post
I got this following code from a MSDN sample..
BOOL bVisible = ((m_wndNavDialogBar.GetStyle() & WS_VISIBLE) != 0);
pCmdUI->SetCheck(bVisible);
OK.. In the first line, why do they have that != 0 part in the end? I could not understand what's the point.
Edited by - jho on 8/31/00 1:47:39 PM
Probably just someguy''s idea of making code more readable. The other thing is that (m_wndNavDialogBar.GetStyle() & WS_VISIBLE) is a long int or something, and the != will ensure that it''ll be typecasted properly into BOOL. Or else they really wanted it to be 1, intead of wherever the WS_VISIBLE flag is... who knows?
random-nomad
[still searching]
random-nomad
[still searching]
If you want you can just ignore that. It basically does the same thing when you leave it without the != 0 so you can just forget about what you saw in there. It makes it clear to newbies, and the compiler itself what you''re doing. Some languages such as Pascal will require you to have that != 0 ( or <> 0 in Pascal) in order to compile. It''s a good coding style and makes it clearer. Wow, I just said the same thing twice
-------------------------------
That's just my 200 bucks' worth!
..-=gLaDiAtOr=-..
-------------------------------
That's just my 200 bucks' worth!
..-=gLaDiAtOr=-..
Because doing a bitwise-AND on a value will either return 0, or any positive value. In your example, if the WS_VISIBLE flag WAS on, it would return the value of WS_VISIBLE, hence your BOOL value would be equal to WS_VISIBLE (not 1 or 0).
Then,if you said:
if (bVisible == TRUE)
{
// process code
}
it would NOT process the code, because TRUE is equal to 1, and bVisible wouldn''t be.
Then,if you said:
if (bVisible == TRUE)
{
// process code
}
it would NOT process the code, because TRUE is equal to 1, and bVisible wouldn''t be.
Yes, those explanations are correct. That''s why I said it will "BASICALLY" do the same thing 
but then again, if you were to say
if ( bVisible )
{
// process code
}
it WILL process it correctly
-------------------------------
That's just my 200 bucks' worth!
..-=gLaDiAtOr=-..
quote:
Then,if you said:
if (bVisible == TRUE)
{
// process code
}
but then again, if you were to say
if ( bVisible )
{
// process code
}
it WILL process it correctly
-------------------------------
That's just my 200 bucks' worth!
..-=gLaDiAtOr=-..
I figured it out, Mr Random-Nomad is correct.
Getstyle() & WS_Visible are both DWORD,
BOOL is a byte I think,
Who knows what different compiles to do when you go BOOL x = Some_DWORD. Maybe it just gets the Least significant 8 bits to fit into the BOOL, then if you have a DWORD like 11...11100000000 then you''ll get the wrong answer.
So it is actually not just for clarity but actually safety.
Getstyle() & WS_Visible are both DWORD,
BOOL is a byte I think,
Who knows what different compiles to do when you go BOOL x = Some_DWORD. Maybe it just gets the Least significant 8 bits to fit into the BOOL, then if you have a DWORD like 11...11100000000 then you''ll get the wrong answer.
So it is actually not just for clarity but actually safety.
jho, it''s a lot more complicated than that. The only thing you need to know is that BOOL is defined as an INTeger so it doesn''t really matter. If the DWORD value contains ANYTHING at all, and even if the bool value was assigned the LSB(least significant bit), it would be 1 no matter what. So that means it''s true. That''s why when you say if ( something ) { /* do whatever */} it works, because it looks at the LSB, and if the value was for example 57 (binary = 0111001) the LSBwould be one, so the if() statement evaluates to true. BUT!!! if you say if ( something == TRUE){} or if ( something == 10 }{} then it doesn''t check for boolean values, but instead works with numbers and does a comparing between the two (and not simply test if it''s true or not), which is why if ( something == TRUE ) will return false if something does not equal TRUE (or 1) the way it is with the GetStyle() & WS_VISIBLE thing. if WS_VISIBLE is not equal to TRUE then it wouldn''t work. They''re both constants, and are numbers, so it does a number check (compare). I just hope you understood what I just said. If not come and read it in a week or so and you will
-------------------------------
That's just my 200 bucks' worth!
..-=gLaDiAtOr=-..
-------------------------------
That's just my 200 bucks' worth!
..-=gLaDiAtOr=-..
Yes I understand exactly what you mean.. it simply maps arbitary numbers to 1 and 0 for safty.. like your If (...) sample
Sorry I just found out that
bool is 1 byte
BOOL is 4 bytes
Just starting to do some real windows programming just trying to make sure I know all these little basics
Sorry I just found out that
bool is 1 byte
BOOL is 4 bytes
Just starting to do some real windows programming just trying to make sure I know all these little basics
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement