Link to home
Start Free TrialLog in
Avatar of curiouswebster
curiouswebsterFlag for United States of America

asked on

Is this bitwise &?

I see a line of code in a woring C# program that looks like:

control.IsVisible = control.IsVisible & true;

I am at a loss as to what this means.

Does anyone know?

Thanks,
newbieweb

Avatar of loopfinity
loopfinity

this is binary end operator.
code taken form :http://weblogs.asp.net/alessandro/archive/2007/10/02/bitwise-operators-in-c-or-xor-and-amp-amp-not.aspx

regards.
protected void Page_Load(object sender, EventArgs e)
{
byte a = 7;
byte b = 9;
int orComputed = a & b;
Response.Write(string.Format("<br />{0} & {1} Result :{2}", a, b, orComputed));
}

Output is :
7 & 9 Result :1

Open in new window

Avatar of kaufmed
Yes, a single ampersand denotes bitwise-AND.
To go further, the expression sets the value of IsVisible to true if IsVisible is false, and false if IsVisible is true. It basically flips the value of IsVisible, which could also be accomplished via a NOT:

control.IsVisible = !control.IsVisible;
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of curiouswebster

ASKER

I also concluded the code did nothing.  Please respond now if you disagree...
Well...   do  I feel sheepish....  on second glance (and thx to carl_tawn's dilligence) I agree, it does not flip the value. That explains why they won't let me play with the bits at work...     =  )
Is that line part of a larger block? Maybe it was intended to do something at some point, but is now redundant.
I'm not sure about the rest of the code. I was asked to explain this bizarre line, and also concluded it did nothing.

Thanks.