Write binary, gesture must be Sao, save fields, save bandwidth, increase efficiency...

Keywords: Java Linux Database

introduce

Last week, I was confronted with a colleague about an interface. My front-end colleague asked me if I had written the interface document incorrectly, how many exception tags are on an order, and should I not return an array?Why only one number was returned.

Because this interface is a call to other micro services, I am also confused. Ask my colleague to confirm that he only returned a single bit of my status.I immediately understood, because Linux permissions do the same thing, and then I found their code to confirm, as I thought, to see how to do it


It's really simple, just an enumeration class like this

public enum EXTEND_FLAG_ENUM {

    OVER_WEIGHT(1, "overweight"),
    OVER_CUBAGE(1 << 1, "Supersquare"),
    LATE(1 << 2, "A bit late"),
    SLOW(1 << 3, "Relax");

    public int value;
    public String name;

    EXTEND_FLAG_ENUM(int value, String name) {
        this.value = value;
        this.name = name;
    }

    public static int addFlag(int org, EXTEND_FLAG_ENUM newFlag) {
        return org | newFlag.value;
    }

    public static int removeFlag(int org, EXTEND_FLAG_ENUM oldFlag) {
        return org & (~oldFlag.value);
    }
    public static boolean hasFlag(int org, EXTEND_FLAG_ENUM oldFlag) {
        return (org & oldFlag.value) > 0;
    }
}

Use 4 binaries to represent the status of the order
|Binary|Represents State|Decimal|
|--|--|--|
| 0001 | Overweight | 1 |
| 0011 | Overweight, Oversquare | 3 |
| 1011 | Overweight, Oversquare, Slow | 11|
| 1111 | Overweight, Oversquare, Late, Slow | 15|
A brief explanation of AND or NON-OPERATION

And operations (0 out of 0, 1 out of 1)
|Numbers|Secondary System|
|--|--|
| A | 1 0 1 0 |
| B | 1 1 0 0 |
|A & B|1 0 0 0 |
Or operation (1 out of 1; 0 out of 0)
|Numbers|Secondary System|
|--|--|
| A | 1 0 1 0 |
| B | 1 1 0 0 |
|A | B|1 1 1 0 |
Non-operation (1 out of 10; 0 out of 1)
|Numbers|Secondary System|
|--|--|
| A | 1 0 1 0 |
| ~A | 0 1 0 1 |

The following asserts that the test passed

@Test
public void showTest() {
    // Order exception label initially 0
    int extendFlag = 0;
    // Mark order overweight
    extendFlag = EXTEND_FLAG_ENUM.addFlag(0, EXTEND_FLAG_ENUM.OVER_WEIGHT);
    assertTrue(EXTEND_FLAG_ENUM.hasFlag(extendFlag, EXTEND_FLAG_ENUM.OVER_WEIGHT));
    // Mark order super party
    extendFlag = EXTEND_FLAG_ENUM.addFlag(extendFlag, EXTEND_FLAG_ENUM.OVER_CUBAGE);
    // The order is really overweight and overweight
    assertTrue(EXTEND_FLAG_ENUM.hasFlag(extendFlag, EXTEND_FLAG_ENUM.OVER_WEIGHT));
    assertTrue(EXTEND_FLAG_ENUM.hasFlag(extendFlag, EXTEND_FLAG_ENUM.OVER_CUBAGE));
}

The front end gets an integer and resolves the corresponding state

@Test
public void showTest2() {
    // [1]
    System.out.println(getExtendFlag(1));
    // [1, 2, 8]
    System.out.println(getExtendFlag(11));
    // [1, 2, 4, 8]
    System.out.println(getExtendFlag(15));
}

public List<Integer> getExtendFlag(int num) {
    List<Integer> numList = new ArrayList<>();
    int temp = 1;
    while (temp < 16) {
        if ((num & temp) >= 1) {
            numList.add(temp);
        }
        temp = temp << 1;
    }
    return numList;
}

You see, a number records multiple states at the same time, saves database fields and bandwidth, makes the program more scalable, and adds a new state by adding only one enumeration property.

Other Sao operations in binary

General operation Sao Operations
n * 2 n << 1
n / 2 n >> 1
n % 1 == 1 n & 1 == 1

Nothing else. Bit operation is fast

To determine if a number is an exponent of 2, I was asked during an interview

bool isPowerOfTwo(int n) {
    if (n <= 0) return false;
    return (n & (n - 1)) == 0;
}

The exponential binary form of 2 must be 1000, and the binary form of exponential-1 of 2 must be 111, so the operation must be 0

There are many more binary Sao operations, but they are not commonly used. Here are just a few of the common ones

Welcome to your attention

It's a surprise to watch the reply to the pdf directory, and to visit www.erlie.cc with a huge amount of video resources

Reference Blog

Some interesting and useful bitwise operations
[1] https://mp.weixin.qq.com/s/Z37PpJ5ZuK3pwEvaFuBxBg
Tips Summary Bit Operations Loading Guide
[2] https://blog.csdn.net/m0_37907797/article/details/103120886

Posted by joeman3429 on Wed, 12 Feb 2020 18:05:04 -0800