C# AND between numbers

madcrow74

Newcomer
Joined
Jul 1, 2003
Messages
15
Location
Italy
I am trying to have an AND done between two numbers...
I expose the whole problem.
I have a ToolBar with some buttons. The buttons are by itself indexed.
My function must enable/disable the buttons due to the numerical value which I pass to him.
The value is one and contains all the buttons to be enabled, it is a series of bits converted in decimal.
The AND must be done between this value and a mask of bits, which changes at second of the button which I am verifying.
If returns the masks ( in decimal ) then the button will be enabled, otherwise it will be disabled.
Afterwards the code:

Code:
public void abilita(long mint)
  {
    for(int i=0;i<toolbarMain.Buttons.Count;i++)
    {
      if((mint && Math.Pow(2,i))==Math.Pow(2,i))
      {
        toolbarMain.Buttons[i].Enabled = true;
      }
      else
      {
        toolbarMain.Buttons[i].Enabled = false;
      }
    }
  }

The C# does not accept that the AND is done between two numbers, of any kind ... Is it normal?
 
Two things:

1. You want to use the bitwise & operator, not the logical && operator.

2. Math.Pow() returns a double, so you'll have to cast the result to long to do the ANDing.

Like this:

C#:
    public void abilita(long mint)
    {
        for (int i = 0; i < toolbarMain.Buttons.Count; i++)
        {
            toolbarMain.Buttons[i].Enabled =
                (mint & (long) Math.Pow(2, i)) == (long) Math.Pow(2, i);
        }
    }

Having said that, I wouldn't use Pow() in this situation. It gets very long-winded, looking at that; and all those method calls can't be good. I'd have thought something like this would be a good solution:

C#:
    public void abilita(long mint)
    {
        for (int i = 0; i < toolbarMain.Buttons.Count; i++)
            toolbarMain.Buttons[i].Enabled = (mint & 1 << i) != 0;
    }
 
Optimization is great, but readability counts for a lot! I'm not sure how you're deciding what buttons to enable/disable, but you might want to consider using an enum for the buttons. An enum can be specified with a [Flags] attribute which is ideal for things like this.

Here's some sample code with a new version of your abilita function:
C#:
[Flags]
public enum ToolbarEnum
{
	NoButton = 0,
	Button1 = 1,
	Button2 = 2,
	Button3 = 4,
	Button4 = 8,
	Button5 = 16,
	Button6 = 32
}


public void abilita(ToolbarEnum enabledButtons)
{
	long mint = (long)enabledButtons;
	for (int i = 0; i < toolbarMain.Buttons.Count; i++)
		toolbarMain.Buttons[i].Enabled = (mint & (1 << i)) != 0;
}


// Here's how you would call the new function
// to enable buttons 1, 2 and 5:
abilita(ToolbarEnum.Button1 | ToolbarEnum.Button2 | ToolbarEnum.Button5);

-Nerseus
 
Yes, it's ok...
But my main goal is to remember the status of a toolbar and a mainmenu with another function (that already have) then passing the result to abilita. So it's not very important to have an enumerator. But it's good example for another thing i have to do, thank u :-)
 
Back
Top