# Bitwise operators of basic operators in C language (6)

Time：2021-8-5

For more compact data, C programs can store information with independent bits or multiple bits combined together. File access permission is a common application case. The bit operator allows processing of independent bits in a byte or larger data unit: any bit or multiple bits can be cleared, set, or inverted. You can also move the bit pattern of an integer to the right or left. ### C language provides six bit operators:

&Bitwise AND

|Bitwise OR

^Bitwise XOR

~Reverse

< < move left ### Bit operation application formula

To clear and reverse, use and, a certain position is available or

To reverse and exchange, use XOR easily

### Operation rules

1. & and operation: the result bit is 1 only when the corresponding two binary bits are 1, otherwise it is 0（ False = = 0)

2. | or operation: when one of the two corresponding binary bits is 1, the result bit is 1, otherwise it is 0（ True if true = = 1)

3. ^ XOR operation: if the corresponding binary is different, it is 1, otherwise it is 0（ (the difference is true = = 1, and the same is false = = 0)

Note: (1) the result of the same integer phase ^ is 0

(2) One number ^ the other number twice, the result remains the same

(3) Integer phase ^ is independent of order

4. ~ inverse operation: inverse all binary bits of an integer, including sign bits (0 to 1, 1 to 0, true and false interchanged)

5. < < shift left: shift all binary bits of an integer to the left by N bits, discard the high bit (including the symbol bit), and fill the low bit with 0.

Shifting n bits to the left is actually multiplied by 2 to the nth power (except for the change of sign bit after moving, the change of sign bit will change positive and negative)

6. > > shift right: shift all binary bits of an integer to the right by N bits, and the sign bit remains unchanged. The vacancy in the high position is a positive number to fill 0;

Is a negative number, and the high order is supplemented by 0 or 1, depending on the provisions of the compiling system; Shifting n bits to the right is actually divided by 2 to the nth power

### Operator priority

Among the median operators in C language, the priority order is: ~, < >, &, ^, |, & =, ^ =, | =, < =, > =. ### Boolean operator

#### 1. Bitwise and operation “&”

The bitwise and operator “&” is a binocular operator. Its function is to sum the binary phase corresponding to each of the two numbers involved in the operation. Only when the corresponding two binary bits are 1, the result bit is 1, otherwise it is 0. The number involved in the operation appears in the form of complement.

For example, 9 & 5 can be written as follows: 00001001 (binary complement of 9) & 00000101 (binary complement of 5) 00000001 (binary complement of 1). 9 & 5 = 1.

Bitwise and operations are usually used to clear 0 or reserve some bits. For example, clearing the upper eight bits of a to 0 and reserving the lower eight bits can be used for a-255 operation (the binary number of 255 is 0000000011111111).

main(){

int a=9,b=5,c;

c=a&b;

printf(“a=%d/nb=%d/nc=%d/n”,a,b,c);

}

#### 2. Bitwise OR operation “|”

The bitwise OR operator ‘|’ is a binocular operator. Its function is the binary phase or corresponding to each of the two numbers involved in the operation. As long as one of the corresponding two binary bits is 1, the result bit is 1. Both numbers involved in the operation appear as complements.

For example, 9|5 can be written as follows: 00001001|00000101

00001101 (decimal 13) visible 9|5 = 13

main(){

int a=9,b=5,c;

c=a|b;

printf(“a=%d/nb=%d/nc=%d/n”,a,b,c);

}

#### 3. Bitwise XOR operation “^”

The bitwise exclusive or operator ‘^’ is a binocular operator. Its function is that the corresponding binaries of the two numbers involved in the operation are different or, when the two corresponding binaries are different, the result is 1. The participating operands still appear as complements.

For example, 9 ^ 5 can be written as the following formula: 0000101 ^ 0000101 (decimal is 12)

main(){

int a=9;

a=a^15;

printf(“a=%d/n”,a);

}

#### 4. Inverse operation “~”

The negation operator “~” is a monocular operator with right associativity. Its function is to negate the binary bits of the numbers involved in the operation.

For example, the operation of ~ 9 is: ~ (0000000000001001), and the result is 1111110110

### Shift Operators

Shift left and shift right operations realize the shift of each binary bit of a number from left to right by several bits.

#### 5. Shift left operation “<

Shift left operator “<

For example, a < < 4 refers to moving each binary of a to the left by 4 bits. If a = 00000011 (decimal 3), it will be 00110000 (decimal 48) after moving 4 bits left.

#### 6. Shift right operation “> >”

The shift right operator “> >” is a binocular operator. Its function is to shift all the binary bits of the operand on the left of “> >” to the right by several bits, and the number on the right of “> >” specifies the number of bits to be moved.

For example, if a = 15 and a > > 2, it means that 000001111 is shifted to 00000011 (decimal 3). It should be noted that for signed numbers, when moving to the right, the sign bit will move with it. When it is a positive number, the highest bit is supplemented by 0, while when it is a negative number, the sign bit is 1, and the highest bit is supplemented by 0 or 1, depending on the provisions of the compilation system. Turbo C and many systems are specified as supplement 1.

main(){

unsigned a,b;

printf(“input a number: “);

scanf(“%d”,&a);

b=a>>5;

b=b&15;

printf(“a=%d/tb=%d/n”,a,b);

}

Please take another example!

main(){

char a=’a’,b=’b’;

int p,c,d;

p=a;

p=(p<<8)|b;

d=p&0xff;

c=(p&0xff00)>>8;

printf(“a=%d/nb=%d/nc=%d/nd=%d/n”,a,b,c,d);

}

When performing bitwise and or, it is best to use hexadecimal. In the program, 0x01 represents 0000 0001

Therefore, the highest bit mandatory 1 of character type a can be as follows: a = a|0x80. Others can be followed by analogy!

I’ll share it here today. I hope it will help you learn!

Article source: https://blog.csdn.net/cxyol/article/details/631591 ## Windows terminal uses nushell+oh-my-posh to beautify

First look at the renderings Please point out if the steps are wrong. Install nushell from github Download from github releaseRelease 0.66.2 · nushell/nushell (github.com) github release download speed may be slow, you can download it hereDownload (serctl.com) Install windows terminal in microsoft store Install oh-my-posh on Microsoft Store Winget can also be used, but […]