Binary operations and usage scenarios in java
This article introduces the knowledge of "binary operation and usage scenario in java". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Knowledge points of this article
Using binary usage scenarios in java
Declare binary data in java
Splicing binary data in java
Binary usage scenarios are used for identification
Binary means that there are only two numbers 0 and 1. This is similar to many scenes in our reality, such as male / female, yes / no, read or not. So for the description of a subject, we can have a binary string to identify a series of flag.
(the knife has been renamed Lixin. As for the reason, because I am making a Mini Program now, I expect to meet with you soon.)
If there is a heart, there is also a description of the binary string 1101. The first 1 stands for male and the second 1 for short. The third 0 stands for ugliness and the third 1 for stupidity. In this way, we only use 4 bits to complete the storage of this information. 4 digits are converted to decimal, with a maximum of 15. You can store so much information with a byte.
Declare binary data
In general, there are three ways: 1, read from String. two。 Turn from decimal number. 3. Declare directly, and the code is as follows:
Public class BinaryTools {public static void main (String [] args) {/ / 1. Read binary String s = "1101" from String; Integer integer = Integer.valueOf (s, 2); / / 13 System.out.println (integer); / / 2. Read binary int i1 = 520; / / 1000001000 System.out.println (Integer.toBinaryString (i1)) from decimal number; / / 3. Directly declare binary int i2 = 0b1101; / / 13 System.out.println (i2);}} stitching binary data
The position operator is used here. Such as 1001. Each represents the state of four different attributes. We must get the gender by attribute, and put it in the fourth position from right to left. Get short to third place from right to left. Go down in turn. The code is as follows:
Public static void main (String [] args) {int I = 0b1; int j = 0b1; int k = 0b0; int m = 0b1; / / 1000 int i1 = I