XOR operation and conversion from decimal to binary in Flutter

In Flutter, XOR operation can be realized by "^" operator. The XOR operation is a logical operator that returns 1 when the corresponding bits of the two operands are different, and 0 otherwise. Here is a simple example:

int a = 5; // 二进制为 101
int b = 3; // 二进制为 011
int result = a ^ b;
print(result); // 输出2,二进制为 110

In this example, a ^ bthe result of the operation is 2, 5because 3the binary representations of and differ only in the last bit. Therefore, the final result is binary 010, which is 2 in decimal.

5 in binary

To convert a decimal number into a binary number, you need to use the method of "dividing by 2 and taking the remainder", as follows:

  1. Divide the decimal number to be converted by 2 to get the quotient and remainder.
  2. Continue to divide the quotient by 2 and repeat the above steps until the quotient is 0.
  3. Arrange the remainder obtained each time from bottom to top, which is the binary representation of the decimal number. For the number 5, it can be converted to binary as follows:
5 ÷ 2 = 2……1(余数为12 ÷ 2 = 1……0(余数为01 ÷ 2 = 0……1(余数为1

Then arrange the remainder obtained each time from bottom to top, and the result is the binary number 101.

3 binary calculation process
3 ÷ 2 = 1……1(余数为11 ÷ 2 = 0……1(余数为1

Then arrange the remainder obtained each time from bottom to top, and the result is the binary number 11.


The XOR operation is a binary operation where each bit of two binary numbers is 0 if they are the same and 1 if they are different. For example, when XORing the number 5 (101 in binary) and the number 3 (011 in binary), their binary bits look like this:

101
^ 011
  ---
  110

Therefore, the result is 110 in binary, which is 6 in decimal. The exclusive OR operation can also be represented by the symbol "^", for example, in the Dart programming language, "a ^ b" is used to calculate the exclusive OR between a and b.

XOR operation has the following calculation rules:

  1. 0 ^ 0 = 0,0 ^ 1 = 1,1 ^ 0 = 1,1 ^ 1 = 0。
  2. XORing any number with 0 results in itself.
  3. XORing any number with itself results in 0.
  4. XOR operation satisfies associative law and commutative law.

The following is an example of XOR operation. Suppose we want to XOR the number 6 (110 in binary) and the number 3 (011 in binary). Their binary bits are as follows:

110
^ 011
  ---
  101

Therefore, the result is 101 in binary, which is 5 in decimal.
The XOR operation is widely used in computer science, especially in cryptography, where it can be used to encrypt and decrypt data, as well as for checksum verification. Also, it can be used to swap the values ​​of two numbers without resorting to temporary variables.


To convert decimal to binary, you need to use the method of "dividing by 2 and taking the remainder". The specific rules are as follows:

  1. Divide a decimal number by two to get the quotient and remainder.
  2. Then divide the quotient by two to get the quotient and remainder.
  3. Repeat this process until the quotient is equal to 0.
  4. Then arrange the remainder obtained each time from bottom to top, and the corresponding binary number is obtained. For example, to convert the decimal number 47 to binary, follow these steps:
47 ÷ 2 = 23……1 
23 ÷ 2 = 11……1 
11 ÷ 2 = 5……1 
5 ÷ 2 = 2……1 
2 ÷ 2 = 1……0 
1 ÷ 2 = 0……1 

Finally, arrange the remainder obtained each time from bottom to top, and the result is the binary number 101111, which is the binary representation of the decimal number 47.

If you have needs related to applets, APPs, official accounts, and websites, you can contact me through private messages

If you are interested, you can pay attention to my comprehensive official account: biglead

Guess you like

Origin blog.csdn.net/zl18603543572/article/details/130555252