“This is the 39th day of my participation in the First Challenge 2022. For details: First Challenge 2022”

1. The introduction

Operators are something every programmer is exposed to when they start programming. Operators can be roughly divided into the following categories:

  • Arithmetic operator

    +, -, *, / and so on

  • The assignment operator

    =, +=, etc

  • Relational operator

    = =,! =, >, etc

  • Logical operations

    , &&, | |!

  • An operation

    &, | and so on

Bitwise operations are often seen in technical code, and anyone who has studied the HashMap source code or the thread pool source code will see bitwise operations in the source code. In the business development basically can not see the bit operation, is not suitable for the use of bit operation in the business, of course not. Here’s an example of how to use bitwise operations to make your business code look great in an instant. The basics can also play out in business code in a technical project feel. Clever use of bit operations in business development to make your business code instantly high!

Example 2.

The following is an example of the way I used it in the development process.

Scenario: A Restful interface is provided for external users to query user information, including id, name, age, gender, mobile phone number, and home address. However, different access parties may require different data. Access party 1 requires the ID, name, and age, and Access Party 2 requires the ID, name, and mobile phone number. In this case, of course, the service provider can give both parties all the fields of the interface. But technically give them what they want? So how to provide solutions.

Plan 1:

Directly provide interfaces for both sides individually, simple and quick. But if there are 10 interfaces that all have the same fields on the demander then provide 10 interfaces to different access parties. Obviously this is not appropriate.

Scheme 2:

Provides a full interface that contains the fields of 10 contacts, which is the union of 10 contacts from a collection perspective. In this way, the amount of data returned to the front end increases. If the list is enlarged, the amount of data will be even more.

Solution 3:

By clever bit manipulation.

Here is a code to demonstrate how to do this:

@JsonInclude(JsonInclude.Include.NON_NULL)
public class User {

    private Long id;

    private String name;

    private Integer age;

    private Short sex;

    private String moblie;

    private String address;
	// omit get set code
}
Copy the code

The Controller interface code is as follows:

@RestController
@RequestMapping("/user")
public class UserController {


    /** * type: treat type as a 32bit, representing id, name, age, gender, mobile phone number, and home address in descending order. * Each bit of 0 indicates that data is not displayed to the front end, and 1 indicates that data is displayed to the front end. * Example: * binary :111111 means all display when converted to decimal integer 63, means all display * binary :101111 means all display when converted to decimal integer 47, means all display except the name * * Int sum = 0; * the sum | = (1 < < 2) -- this display gender * *@param id
     * @param type
     * @return* /
    @GetMapping("/{id}")
    public User getUserById(@PathVariable("id") String id, @RequestParam(value = "type", defaultValue = "63") int type){

        // How to handle type according to design?
        User user = new User();

        // Check whether the home address is displayed
        if((type & 1) != 0) {// Splice SQL-- simulation
            user.setAddress("Home address");
        }

        // Check whether the mobile phone number is displayed
        if((type & (1<<1)) != 0) {// Splice SQL-- simulation
            user.setMoblie("Mobile phone number");
        }

        // check whether gender is displayed
        if((type & (1<<2)) != 0) {// Splice SQL-- simulation
            user.setSex((short) 1);
        }

        // check whether the age is displayed
        if((type & (1<<3)) != 0) {// Splice SQL-- simulation
            user.setAge(10);
        }

        // check whether the name is displayed
        if((type & (1<<4)) != 0) {// Splice SQL-- simulation
            user.setName("Ants carry elephants.");
        }

        // check whether ID exists
        if((type & (1<<5)) != 0) {/ / stitching SQL
            user.setId(100L);
        }

        returnuser; }}Copy the code

The above code is the idea of the implementation. Let’s run the code to see what happens:

Same idea as before

Tips: The use of bitwise operations in interfaces is just an idea that can be used in other places besides interface design

Code address: github.com/mxsm/spring…

3. Summary

  • And use or transfer operation (|) to pass the final value
  • The back end is handled by and (&)
  • The operator’s solution is limited in this case because a bit is either 0 or 1. So you can only have two choices.

In general this avoids the fact that you write each combination as an integer. Using bits to represent collocations solves the problem instantaneously. Make your business code instantly pop.

I am ant back elephant, the article is helpful to you like to pay attention to me, the article has incorrect place please give correct comments ~ thank you