Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BitwiseNot issue #10

Open
clemexjeap opened this issue Nov 10, 2022 · 7 comments
Open

BitwiseNot issue #10

clemexjeap opened this issue Nov 10, 2022 · 7 comments

Comments

@clemexjeap
Copy link

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?

@binki
Copy link
Contributor

binki commented Nov 11, 2022

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

@binki
Copy link
Contributor

binki commented Nov 11, 2022

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.

@clemexjeap
Copy link
Author

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

binki added a commit to binki/EnumUtilities that referenced this issue Nov 11, 2022
Convenience to invert all flags on an enum.

Fixes arogozine#10.
@binki
Copy link
Contributor

binki commented Nov 11, 2022

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.

@clemexjeap
Copy link
Author

Sounds good to me.

One other nice to have:

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

@binki
Copy link
Contributor

binki commented Nov 12, 2022

@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? ;-)

@clemexjeap
Copy link
Author

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

binki added a commit to binki/EnumUtilities that referenced this issue Jan 3, 2023
Convenience to invert all flags on an enum.

Fixes arogozine#10.
@binki binki mentioned this issue Jan 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants