madcrow74 Posted July 7, 2003 Posted July 7, 2003 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: 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? Quote
_SBradley_ Posted July 7, 2003 Posted July 7, 2003 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: 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: public void abilita(long mint) { for (int i = 0; i < toolbarMain.Buttons.Count; i++) toolbarMain.Buttons[i].Enabled = (mint & 1 << i) != 0; } Quote
madcrow74 Posted July 7, 2003 Author Posted July 7, 2003 Great! very optimized code, i like it :-D Quote
*Experts* Nerseus Posted July 7, 2003 *Experts* Posted July 7, 2003 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: [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 Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
madcrow74 Posted July 8, 2003 Author Posted July 8, 2003 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 :-) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.