Eclixal
Eclixal

Reputation: 73

How to use binary with enum?

I want to use binary for save parameter of one user, I have an enum:

public enum Test {
    TEST1, (= 00000001)
    TEST2, (= 00000010)
    TEST3, (= 00000011)
    TEST4; (= 00000100)
}

When the user left send their choice on database with a binary addition (example 00000001 + 00000100 = ‭00000101‬). But when user rejoins and tries to get Test1 and Test2 it doesn't work, I have tried with binary operation, without having expected results.

Upvotes: 1

Views: 826

Answers (1)

Sunil Kanzar
Sunil Kanzar

Reputation: 1260

I don`t know what you want to do with this kind of enum but you can:

public enum Test {
    TEST1(0b00000001),
    TEST2(0b00000010),
    TEST3(0b00000011),
    TEST4(0b00000100);

    private final int value;

    private Test(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }
}

And you can get back value using getValue() method and you can change the data type of value as par your requirement.

If it is same as in your given code then you should use byte.

0b00000001
Start your number with 0b.

And this is how you can assign binary value to the variable

Upvotes: 2

Related Questions