Question

I'm translating a part of code from C# program to Java where is defined a [Flag] enum like this:

[Flags]
public enum ClientFlags
{
    None        = 0x00000000,
    Flag1       = 0x00000001,
    Flag2       = 0x00000002
    ...

And on runtime make mask operations like like

ClientFlags.Flag1| ClientFlags.Flag2

in my java code i've replicated same class without enum:

public static byte None             = (byte)0x0;
public static byte Flag1            = (byte)0x01;
public static byte Flag2            = (byte)0x02;

But when i made same operations like

byte flags = ClientFlags.Flag1 | ClientFlags.Flag2

then result is different!! How I can replicate same operations in java? Can you help me?

Was it helpful?

Solution

Well there couldn't be any result at all (including 'different' one) since the code won't compile. Result of bitwise OR operation is int, so you have to change flags type to int or convert result to byte:


int flags = ClientFlags.Flag1 | ClientFlags.Flag2;

or


byte flags = (byte)(ClientFlags.Flag1 | ClientFlags.Flag2);

Then you'll get the same result as in C# which is 3.

If you mean that with .NET you can output result as 'Flag1, Flag2' then you probably will have to implement this by yourself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top