Bit operations (including OR, AND, XOR, NOR, NAND, XNOR, and common applications such as &0xFF)

Table of contents

Edit

1. Bitwise AND operation

2. Bitwise OR operation

3. Negation operation

4. If the XOR operation is different, it is 1, and if it is the same, it is 0.

Edit

5. Left shift operation

6. Right shift operation

Common uses:

C language bit operation explanation:



Bit operations (including common applications such as OR, AND, XOR, NOR, NAND, The binary representation corresponds exactly to computer memory, and each cell (bit) can be set to on (1) or off (0). Moreover, bit operations are much faster than normal operators, because bit operations are a low-level operation, but they are more difficult to understand.

Other additions:

1. Except for ~, all bitwise operators are ear and eye operators.

2. Operations can only be performed on integer or character data, otherwise an error will be reported.

Common bitwise operators

1. Bitwise AND operation

 For example: the result of 7&5 is 5, the calculation is as follows:

Common uses:

Clear: If you want to clear a cell to zero, AND it with a value where all bits are 0

Get the number (commonly keep the lower eight bits, or the lower 16 bits, common in images):

For example, X=1001110101, after the operation

2. Bitwise OR operation

For example: the result of 7|5 is 7, the calculation is as follows:

Common uses:

Negative complement

Set some ''bits'' of a number to 1. For example: set the lower 4 bits of Z=10100110 to 1, Z|00001111 can meet the purpose.

3. Negation operation

For example: the result of ~7 is -8, the calculation is as follows:

Common uses:

Make the lowest bit of a number 0, for example: Q&~1, the value of ~1 is 1111111111111110. After the AND operation, the lowest bit must be 0. The priority of ~ is higher than arithmetic operators, relational operators, and logical operators. wait

4. If the XOR operation is different, it is 1, and if it is the same, it is 0.

For example: the result of 10^-9 is -3. The calculation is as follows:

That is, 0000 1010 ^ 1111 0111 = 1111 1101 (complement code). The original code is 1000 0011, which is 10^-9 = -3

Common uses:

Flip a specific bit: For example, Q=10110110, flip the lower 4 bits of Q, and use X ^ 0000 1111 = 1011 1001 to get

Different from 0, keep the original value: Q^0000 0000 = 10110110

Swap A and B:


The above exchange method is not recommended. There is a corresponding addition and subtraction method to exchange values ​​(as shown below). It is also not recommended. It is difficult to understand and not as efficient as the intermediate temporary variable method.

5. Left shift operation

 Shift all the binary bits of an operand to the left by a certain number of bits (the left binary bits are discarded and the right bits are padded with 0s)

For example: X =18; X= X <<2, the result is 72. Calculation is as follows:

After the above left shift by one bit, X =

6. Right shift operation

Shift all the binary digits of a number to the right by a certain number of bits. Positive numbers are left padded with 0, negative numbers are padded with 1 on the left, and the right side is discarded.

For example: X =18; X= X >>2, the result is 4. Calculation is as follows:

Common uses:

Each right shift of the operand by one bit is equivalent to dividing the number by 2 (rounding down).

Notice:

The shift operator generates a new value in C++ but does not modify the original value, for example:


The two output values ​​of the above code are both 108, that is, the value of x will not be modified. The expression x<<2 uses the value of x to generate a new value, just like x+3 will generate a new value, but it will not Just like x will be modified, if you want to use the shift operator to modify the value of a variable, you must use the assignment operator. You can use the regular assignment operator or the <= operator, such as:


Some other applications of bit operations:

 1. Determine odd and even numbers

For any number except 0, use X&1==1 as a logical judgment, for example:


2. Determine whether a certain binary number is 1



//For example ,  determine whether the fifth digit of

 


4. Determine whether two numbers have different signs


5. Data encryption


The above output is:

Initial date: Me and my motherlandEncrypt
date: 混丝屋拉虖
Decode date: Me and my motherland

6. Take the absolute value (high efficiency)


or


For other operations, please refer to https://zhuanlan.zhihu.com/p/148790042

In addition, let’s briefly talk about some symbols, such as OR, AND, XOR, NOR, NAND, XNOR and their implementation.


 

C language bit operation explanation:

//Define two 8-bit variables
unsigned char Byte1 = 0x15;
unsigned char Byte2 = 0xf6;

//A byte is divided into 8 bits. They are called differently, but they all have the same meaning.
//Call 1: Bit 7, Bit 6, Bit 5, Bit 4, Bit 3, Bit 2, Bit 1. Bit 0.  
//Call 2: Bit7 Bit6 Bit5. Bit4 Bit3 Bit2 Bit1 Bit0  
//Call three: No. 8 No. 7 No. 6 No. 5 No. 4 No. 3 No. 2 No. 1
//The third nomenclature is not recommended, the second one is recommended

Byte1 |= (0x01 << 3); //This sentence means: Set Bit3 of Byte1 to 1
Byte2 &= ~(0x01 << 6); //This sentence means: Set Bit6 of Byte2 to 0

Detailed explanation of the counting process of /*1, Byte1 |= (0x01 << 3);:
  1), |= is called or, etc., which is similar to +=. a|=b is a=a|b, a+=b is a =a+b. So Byte1 |= (0x01 << 3); is Byte1 = Byte1|(0x01 << 3);
  2), 0x01 is expressed in 8-bit binary: 0000 0001 (Note: Byte1 is 8-bit data, so 8 bits are used Binary, if Byte1 is 16-bit data, use 16-bit binary...)
       and then shift it to the left by 3 bits, it becomes 0000 1000.
  3). The value of Byte1 is 0x15, that is, 0001 0101. Byte1 or above (0x01 << 3), which is the bitwise OR of 0001 0101 and 0000 1000. The result is 0001 1101. Compared with the original value
        of
      Byte1
  , The difference is that Bit3 is set to 1

  2. Detailed explanation of the counting process of Byte2 &= ~(0x01 << 3);:
  1), &= is called and, a&=b is a=a&b. So Byte2 &= ~(0x01 << 3); is Byte2 = Byte2&(~(0x01 << 3));
  2), 0x01 is expressed in 8-bit binary: 0000 0001 
       and then shifted to the left by 6 bits, it becomes 0100 0000
       After shifting left and then inverting, it becomes 1011 1111
  3). The value of Byte2 is 0xf6, which is 1111 0110. Byte2 and the above ~ (0x01 << 6) are the bitwise AND of 1111 0110 and 1011 1111. 1111 0110 &
        1011
      1111
  The result is 1011 0110. Compared with the original value of Byte1, the difference is that Bit6 is set to 0
*/

Knowledge of converting hexadecimal values ​​into decimal values

/*
Related knowledge:
Methods for converting hexadecimal numbers into decimal values
. For example: Convert the hexadecimal number 0xAE into a decimal number.

Method 1:
Convert to binary and then convert to decimal binary:
The binary representation of 0xAE: 1 0 1 0 1 1 1 0 The
corresponding decimal value for each digit: 128 64 32 16 8 4 2 1
The final decimal value is 128 + 32 + 8+ 4+2 = 174

Method 2: 
Hexadecimal is directly converted into decimal value
0xAE: A is 10, E is 14, and the base is 16,
so 0xAE is equal to 10x16 raised to the 1st power + 14x16 raised to the 0th power = 174

*/
 

Guess you like

Origin blog.csdn.net/s_nshine/article/details/132506906