C language bit operators

C language bitwise operators include:

                              & bitwise and | bitwise or ~bitwise negation ^ bitwise exclusive or >> left shift << right shift

Bit operators are part of the C language, and this part is not difficult, but there are still some things to pay attention to after careful consideration

First, let's summarize some bit operators:

      1. Bit operators can only operate on integer data (including character data), and cannot operate on floating-point numbers.

      2. The right operand range of the left shift right shift operator can only be [0,31]

              (1) The left operand must be an integral type of char and short after being implicitly converted to int before the operation

              (2) The range of the right operand is [0,31]

              (3) Left shift operator rules: remove high bits and fill low bits with 0

              (4) Right shift operator rules: the high-order complement sign bit is removed from the low-order bit

              (5) The left shift operator is equivalent to multiplying by 2 to the Nth power, which is more efficient than the four operators

              (6) The right shift operator is equivalent to dividing by 2 to the Nth power, which is more efficient than the four operators

     3. There is no short-circuit rule for bitwise operators (logical operators have such rules), all operands will have values

     4. The efficiency of bitwise operators is higher than that of four operators and logical operators

     5. Priority of bit operators: four operators < bit operators < logical operators

Second, say the following operators need to pay attention to:

   1. Bit operators, logical operators, and four operators should be avoided from appearing in one expression at the same time. If they must appear, try to use () to separate them to express the order of operations.

   2. The result of the bitwise operator is an integer instead of 0 or 1, and the result of the logical operator is 0 or 1

   3. Bit operator precedence > Logical operator

3. Now I start to write 2 programs to test the above conclusions (platform Ubuntu10 gcc compiler)

#include <stdio.h>

int main(void)
{
   unsigned char i = 0x01;
   unsigned int j = 0x80000000;
   unsigned int k = 1;
   unsigned int m = 3;
   //printf("%d\n",3.14&0x01); //error because bit operators can only operate on integer numbers (char and short automatically convert to int)
   //printf("%d\n",3.14<<1); //error same as above
   printf("%d,0x%.8x\n",sizeof(1),1);//4 and 0x00000001 indicate that the default data type of literal is int
   printf("%d\n",k << 2);          //1*2^2 = 4
   printf("%d\n",1 << 2);          //1*2^2 = 4
   
   printf("%d\n",k << 33);         //1<<2^(33%32) = 1*2^1=2
   printf("%d\n",1 << 33);         //0
   //Through the above test, we found that the literal and the definition have the same number, and the gcc compiler handles it differently
   //When defining a number, when shifting left or right, when the number of removed digits is exceeded, the defined number will be modulo the maximum number of digits, and then shifted
   //In the case of literal data, when the number of removed digits is exceeded, it is 0, including signed data
   printf("%d\n",k << 255);        //1<<2^(255%32) = 1*2^31=0x80000000 = -2^31
   printf("%d\n",1 << 255); //0 exceeds removal digits
   
   printf("%d\n",m >> 1); //3/2^1 = 1 or 0011 >> 1 = 0001 = 1
   printf("%d\n",3 >> 1); //3/2^1 = 1 or 0011 >> 1 = 0001 = 1
   
   printf("%d\n",m >> 33);         //3/2^(33%32) = 1
   printf("%d\n",3 >> 33); //0 exceeds the number of digits removed
   
   printf("%d\n",m >> 255);        //3/2^(255%32) = 0
   printf("%d\n",3 >> 255); //0 exceeds the number of digits removed
   
   printf("%d\n",0x80000000>>33); //0 The signed bit data is also 0 when it exceeds the removed number of digits
   printf("%d\n",0x80000000>>255); //0 The signed bit data is also 0 when it exceeds the removed number of digits
   
   printf("%d\n",-1 >> 1); //-1 memory value = FFFF FFFF >> 1 = FFFF FFFF = -1   
   printf("%d\n", 0x01 << 2 + 3); //+ operator has higher precedence than << so 0x01 << (2 + 3) = 32
    
   printf("%d\n", 3 << -1); //1 Different compilers, different results, gcc = 1, BCC = 0 vs = a large negative number (the smallest number of int)                      
}
#include <stdio.h>

int main(void)
{
   unsigned int i,j,k;                   
   i = 0;
   j = 0;
   k = 0;
   if(++i | ++j & ++k)
   {
       printf("Run here ...\n");
       printf("i = %d\nj = %d\nk = %d\n",i,j,k);//i = 1 j = 1 k = 1
   }
   i = 0;
   j = 0;
   k = 0;
   if(++i || ++j && ++k)
   {
       printf("Run here ...\n");
       printf("i = %d\nj = %d\nk = %d\n",i,j,k);//i = 1 j = 0 k = 0
   }  
   return 0;                       
}

Through the above test, the bit operation has no short-circuit rule, and the bit operation is calculated as an integer, not like the logical operator has a short-circuit rule, and the result is 0 or 1

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324733615&siteId=291194637