Understanding and implementation of binary switch

There is always a variety of state keeping in development. If it's all fields, it's quite troublesome. Using binary state storage is much more convenient. Now you can save 32 switches for an int.  

10111 = 2^4+2^2+2^1+2^0=16+4+2+1=23

The first switch (the first bit on the right of 10111) 23 & 2 ^ (1-1) = 23 & 1 = 1 binary is 1 bit operation 1 > > (1-1) = 1 means on

The second switch (the second bit on the right of 10111) 23 & 2 ^ (2-1) = 23 & 2 = 2 binary is 10 bit operation 2 > > (2-1) = 1 means on

The third switch (the third bit on the right of 10111) 23 & 2 ^ (3-1) = 23 & 4 = 4 binary is 100 bit operation 4 > > (3-1) = 1 means on

The 4th switch (the 4th bit on the right of 10111) 23 & 2 ^ (4-1) = 23 & 8 = 0 binary is 0 to close

The 5th switch (the 5th bit on the right of 10111) 23 & 2 ^ (5-1) = 23 & 16 = 16 binary is 10000 bit operation 16 > > (5-1) = 1 means on

. . .

The nth switch (the nth bit on the right of 10111) 23 & 2 ^ (n-1) = a binary is a bit operation a > > (n-1) = 1 represents X

package com.yh.tools.binarySwitch;

/**
 * @Description: Use the binary form of an int to represent the status of up to 32 (32-bit) switches. 10111 from right to left is on switch, five positions in total. Decimal is represented by 23
 * @CreateDate: 2018/8/2 14:31
 * @UpdateRemark: The modified content
 * @Version: 1.0
 */
public class BinarySwitch {
    /**
     * Query the current index status, [0,1];
     * Check the switch status at index position 0=false 1=true
     * @param value Stored switch data int32
     * @param index How many?
     * @return Value of current location
     */
    static boolean value_bit(int value, int index) {
        return (value >> --index & 1) > 0;
    }

    /**
     * Storage data after processing int32
     *
     * @param value Stored switch data int32
     * @param index How many?
     * @param set   Switching state
     * @return Value of current position [0,1]
     */
    static int value_bit(int value, int index, boolean set) {
        index--;
        if (set) {
            value = 1 << index | value;
        } else {
            value = ~(1 << index) & value;
        }
        return value;
    }


    public static void main(String[] args) {
        int data = 0;
        data = BinarySwitch.value_bit(data, 1, true);
        data = BinarySwitch.value_bit(data, 2, true);
        data = BinarySwitch.value_bit(data, 3, true);
        data = BinarySwitch.value_bit(data, 4, false);
        data = BinarySwitch.value_bit(data, 5, true);
        System.out.println("data:" + data + " data_bin:" + Integer.toBinaryString(data));

        data = BinarySwitch.value_bit(data, 1, false);
        data = BinarySwitch.value_bit(data, 3, false);
        data = BinarySwitch.value_bit(data, 4, true);
        System.out.println("data:" + data + " data_bin:" + Integer.toBinaryString(data));

        System.out.println(BinarySwitch.value_bit(data, 1));
        System.out.println(BinarySwitch.value_bit(data, 2));
        System.out.println(BinarySwitch.value_bit(data, 3));
        System.out.println(BinarySwitch.value_bit(data, 4));
        System.out.println(BinarySwitch.value_bit(data, 5));


        /** out :
         data:23 data_bin:10111
         data:26 data_bin:11010
         false
         true
         false
         true
         true
         */
        System.out.println(Integer.toBinaryString(64));
        System.out.println(Integer.toBinaryString(32));
        System.out.println(64&32);//0

    }


}

Posted on Sun, 12 Jan 2020 11:01:44 -0500 by spectacell