EnumUtilities icon indicating copy to clipboard operation
EnumUtilities copied to clipboard

BitwiseNot issue

Open clemexjeap opened this issue 1 year ago • 7 comments

I have an enum like this:

    [Flags]
    public enum AxesMask
    {
        None = 0,
        X = 1,
        Y = 2,
        Z = 4,
        A = 8,

        XY = X | Y,
        XYZ = X | Y | Z,
        All = X | Y | Z | A,
    }

if I do:

AxesMask axes = AxesMask.Y | AxesMask.A;
var notResult = EnumUtil<AxesMask>BitwiseNot(axes);

I would expect "notResult" to be AxesMask.X | AxesMask.Z; but instead I get -11 ???

Is that a known issue? Is there a work around?

clemexjeap avatar Nov 10 '22 18:11 clemexjeap

@clemexjeap That is what bitwise not is supposed to do. You should get the equivalent of ~(2|8) which is -11.

binki avatar Nov 11 '22 14:11 binki

Sorry, I accidentally sent that too early.

You are interested in a non-bitwise operation. It would be possible to add that as a new feature.

binki avatar Nov 11 '22 14:11 binki

That would be nice. The idea is just to "NOT" all the flags

clemexjeap avatar Nov 11 '22 15:11 clemexjeap

So, one way to do that is to have an “All” flag like you have and do a bitwise exclusive OR against that. Many flags enums do not have such an All member and there’s no way to really correctly automatically determine what this is. So the strategy my PR uses is to OR all of the flags together and then to AND that with the NOT of your enum. Maybe I should have just fixed that to use XOR… But, anyway, it’s a start and at least works for your use case.

binki avatar Nov 11 '22 15:11 binki

Sounds good to me.

One other nice to have:

// Determine if multiple flags are set bool isCombined = EnumUtil<YourEnum>.IsCombinedFlag(YourEnum.Foo | YourEnum.Bar, YourEnum.Bar);

clemexjeap avatar Nov 12 '22 15:11 clemexjeap

@clemexjeap I do not understand how that would be helpful. Did you actually mean to pass two arguments to IsCombinedFlag() or did you only mean to pass one argument?

Please open a new issue with a description of what that is and what utility it has. We can discuss it further and, if it seems useful to me, I could prototype an implementation.

P.S., as it is, I am not sure if this library is even going to accept my PR or if a new release will be published to NuGet for my PR. This library has two purposes:

  1. Provide a type-safe wrapper for enum.
  2. Provide high performance enum manipulation.

On the new dotnet core framework, the method used by this library to attain high performance actually results in lower performance. So the library author has less motivation to continue updating it.

I personally think that having the type-safe wrapper for enum manipulation is useful, even if high performance cannot be achieved. But, yeah, this just isn’t as active anymore.

@arogozine Do you have an idea of if you can check these things out someday? ;-)

binki avatar Nov 12 '22 16:11 binki

Thanks for your comment @binki.

For the IsCombinedFlag() thing. I would like to know if multiple flags (bits) are set. ex/ EnumUtil.IsCombinedFlag(YourEnum.Foo | YourEnum.Bar) == yes EnumUtil.IsCombinedFlag(YourEnum.Foo) == no

Sorry my previous example was wrong

clemexjeap avatar Nov 12 '22 16:11 clemexjeap