Various things like bit flags in Java

What is the bit flag?

Please see the link below as a detailed person has already written it. Flag management by bit operation Game flag management is efficient and easy with bit operations

What do you want to talk about

I want to talk.

The rest is the code

int

    //Definition.
    //You can write it freely, but if you write it by shift operation, you can see the value to be defined next at a glance..
    final static int COM_LOLI = 1 << 0;
    final static int COM_MOM = 1 << 1;
    final static int COM_SIS = 1 << 2;

    /**
     * next flag:0x0008
     * <p>
     *How to write comments that I saw in the Android Framework.
     *If you write the value to be defined next like this, it's handsome.
     */


     //Create.
     int flags = COM_LOLI | COM_SIS;

     //Flag.
     flags |= COM_MOM;

     //Flag down.
     flags &= ~COM_SIS;

     //Check the flag.
     boolean isOn = (flags & COM_LOLI) != 0;

     //At least 101010 when outputting logs...Put out in the format of.
     Integer.toBinaryString(flags);

    /**
     *Creating a method for log output is troublesome to maintain..
     */
    static String decodeFlags(final int flags) {
        StringBuilder builder = new StringBuilder();

        if ((flags & COM_LOLI) != 0) {
            builder.append("LOLI").append(" ");
        }
        if ((flags & COM_MOM) != 0) {
            builder.append("MOM").append(" ");
        }
        if ((flags & COM_SIS) != 0) {
            builder.append("SIS").append(" ");
        }
        return builder.toString();
    }

BitSet

    //There is also a BitSet.
    BitSet bitSet = new BitSet();

    //Flag.
    bitSet.set(2);

    //Flag down.
    bitSet.clear(1);

    //Check the flag.
    boolean isOn = bitSet.get(3);

    //But this is a class used when performing bit operations in earnest
    //It's a little tricky to use as a flag.
    //I was happy if there was toInteger or toLong
    //Maybe it's because we can't guarantee the variable length bit array that is the purpose of the class..

EnumSet


    /**
     *First define the enum.
     */
    enum Complex {
        LOLI("Woooooooooooo"),
        MOM("Mama!"),
        SIS("There are 12 people!");

        public final String cry;

        Complex(final String cry) {
            this.cry = cry;
        }
    }

    //Create.
    EnumSet<Complex> flags = EnumSet.of(Complex.LOLI, Complex.SIS);

    //Flag.
    flags.add(Complex.MOM);

    //Flag down.
    flags.remove(Complex.SIS);

    //Check the flag.
    boolean isOn = flags.contains(Complex.SIS);

    //Looping on the whole element
    for (Complex complex : Complex.values()) {
        Log.d("allCry", complex.cry);
    }

    //Convenient to loop or rotate with only the current element
    for (Complex complex : flags) {
        Log.d("limitedCry", complex.cry);
    }

    //Information on "what to do if the flag is set"
    //Very convenient because it can be taken out immediately

Conclusion

Use EnumSet unless you have a reason to put it in and out of the DB as it is with speed, memory saving, or int. Regarding the speed, EnumSet was also written in Effective Java that it was fast enough.

Also, the guy who defined the constants of the int flag that represents the same state in * various classes * * in different base notation *, ** sir !! **

Recommended Posts

Various things like bit flags in Java
Various threads in java
Implementation of like function in Java
Implement something like a stack in Java
Parallel and parallel processing in various languages (Java edition)
Changes in Java 11
Rock-paper-scissors in Java
Pi in Java
FizzBuzz in Java
Things to watch out for in Java equals
I want to do something like "cls" in Java
Things to watch out for in future Java development
[Java] Things to note about type inference extended in Java 10
[java] sort in list
Read JSON in Java
Make Blackjack in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
Callable Interface in Java
Comments in Java source
Azure functions in java
Format XML in Java
Simple htmlspecialchars in Java
Boyer-Moore implementation in Java
Various pensions in Japan
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Ping commands in Java
Heapsort implementation (in java)
Zabbix API in Java
ASCII art in Java
Compare Lists in Java
POST JSON in Java
Express failure in Java
Create JSON in Java
Date manipulation in Java 8
What's new in Java 8
Java random, various processing
Use PreparedStatement in Java
What's new in Java 9,10,11
Parallel execution in Java
Initializing HashMap in Java
Things you often use when doing web development in Java
Things to be aware of when writing code in Java