C language bit operators: and or or or reverse left and right shift details

  • 2020-04-02 01:16:11
  • OfStack

Bit operations are operations performed on a binary basis. In system software, it is often necessary to deal with binary problems. The C language provides six bit operation operators. These operators can only be used for integer operands, that is, for signed or unsigned char,short,int, and long types.

List of bit operators provided by C language:
Operator meaning description
If both of the corresponding binary bits are 1, the resulting value of that bit is 1, or 0
As long as one of |'s bitwise or two corresponding binary bits is 1, the resulting value of that bit is 1
^ is 0 by bit or if the two binary values involved in the operation are the same, otherwise it is 1
~ invert ~ is a unary operator used to invert a binary number by bit
< < Left shift is used to shift all the binary bits of a number to the left by N bits, and to complement 0 to the right
> > Right shift moves the binary bits of a number to the right by N bits, and the lower bits to the right end are discarded. For unsigned Numbers, the higher bits are supplemented by 0

1. "bitwise and" operator (&)
Bitwise and refers to: the operation of two data, according to the binary "and" operation. If both of the corresponding binary bits are 1, the resulting value of that bit is 1. Otherwise it's 0. The 1 here can be interpreted as true in logic, and the 0 here as false in logic. Bitwise is consistent with the actual and logical operation of "and". The logical "and" requires the operand to be true. If, A=true,B=true, A studying B=true, e.g. : 3&5, the binary code of 3 is 11(2). (in order to distinguish decimal from other bases, this article stipulates that all data that is not decimal is followed by parentheses, in which the decimal is indicated, and the binary is marked as 2.) the basic unit for storing data in memory is Byte, a Byte consists of 8 bits (bits). Bit is the smallest unit used to describe the amount of data in a computer. In a binary system, each 0 or 1 is a bit. If 11 (2) is made up to one byte, it is 00000011 (2). The binary code of 5 is 101 (2). If it is made up to one byte, it is 00000101 (2).

Bitwise and operation:
00000011 (2)
& 00000101 (2)
00000001 (2)
So 3&5 is 1

C language code:


#include <stdio.h>
main()
{
int a=3;
int b = 5;
printf("%d",a&b);
}

Bitwise and purpose:

(1) reset
If you want to zero a storage location, even if all its binary bits are 0, you only need to find a binary number, where each bit meets the following conditions:

The bit that was 1 in the original number is 0 in the new number. Then make the two carry on & operation, can achieve the goal of clearing zero.
Example: the original number is 43, i.e. 00101011 (2). Find another number and set it as 148, i.e. 10010100 (2).
00101011 (2)
& 10010100 (2)
00000000 (2)

C language source code:


#include <stdio.h>
main()
{
int a=43;
int b = 148;
printf("%d",a&b);
}

(2) take some specified bits in a number
If you have an integer a(2byte), all you need to do to get the lower byte is to bitsum a with 8 ones.
A 00101100, 10101100,
B, 00000000, 11111111
C 00000000 10101100

(3) reserve the specified bit:
A "bitwise and" operation with a number that takes 1 in that bit.
For example, if there is a number 84, namely 01010100 (2), we want to keep the number 3, 4, 5, 7, and 8 bits from the left. The calculation is as follows:
01010100 (2)
& 00111011 (2)
00010000 (2)
I.e., a = 84, b = 59
      C = kathi Jones from a&b = 16

C language source code:


#include <stdio.h>
main()
{
int a=84;
int b = 59;
printf("%d",a&b);
}

2. Bitwise or operator (|)
As long as one of the two corresponding bits is 1, the resulting value of that bit is 1. In the words of logic or operations, a true is true.
For example: 60 (8) |17 (8), bitwise or operation of octal 60 with octal 17.
00110000
| 00001111
00111111

C language source code:


#include <stdio.h>
main()
{
int a=060;
int b = 017;
printf("%d",a|b);
}

Application: bitwise or operation is often used to assign a value of 1 to certain bits of data. For example, if you want to change the lower number of a to 1 by 4 bits, all you need to do is perform a bitwise or operation with 17 (8).

3. Exchange two values without using temporary variables
For example: a = 3, that is, 11 (2); B is equal to 4, which is 100 (2).
To swap the values of a and b, the following assignment statement can be used:


     A = a Sunday afternoon b;
     B = b Sunday afternoon a;
     A = a Sunday afternoon b;
 A = 011(2)
     ( Sunday afternoon ) b = 100(2)
 A = 111(2) ( a Sunday afternoon b As a result of, a Has become 7) 
     ( Sunday afternoon ) b = 100(2)
 B = 011(2) ( b Sunday afternoon a As a result of, b Has become 3) 
     ( Sunday afternoon ) a = 111(2)
 A = 100 ( 2 ) ( a Sunday afternoon b As a result of, a Has become 4) 

Equivalent to the following two steps:
Execute the first two assignment statements: "a = a Sunday afternoon b;" And "b = b Sunday afternoon a;" The same thing as b=b Sunday afternoon (a Sunday afternoon b).
Then execute the third assignment statement: a = a Sunday afternoon b. Since the value of a is equal to a Sunday afternoon b, and the value of b is equal to b Sunday afternoon b,

So it's the same thing as a=a inverted b inverted b inverted a inverted b, or the value of a is equal to a inverted b inverted b inverted b inverted b, which is equal to b.
Isn't that amazing?

C language source code:


#include <stdio.h>
main()
{
int a=3;
int b = 4;
a=a^b;
b=b^a;
a=a^b;
printf("a=%d b=%d",a,b);
}

4. "invert" operator (~)
It is the unary operator used to find the binary antithesis of integers, that is, to change the 1 on each bit of the operand into 0,0 into 1 respectively.
For example: ~ 77 (8)

The source code:


#include <stdio.h>
main()
{
int a=077;
printf("%d",~a);
}

5. Left shift operator ( < < )
The left shift operator is used to move the binary bits of a number to the left by several bits. The moving bits are specified by the right operand (the right operand must be non-negative).
For example, move the binary number of a to the left by 2 bits, the empty bits on the right complement 0, and the overflow bits on the left discard. If a is equal to 15, which is 00001111 (2), move 2 to the left
Bit is 00111100 (2).

The source code:


#include <stdio.h>
main()
{
int a=15;
printf("%d",a<<2);
}

So if I move 1 bit to the left, that's going to be the same thing as multiplying this number by 2, and if I move 2 bits to the left, that's going to be the same thing as multiplying this number by 2 times 2 is 4,15 is less than 2 is 60, so I'm multiplying by 4. But this conclusion only applies to that

When the number is moved to the left, the high position discarded by overflow does not contain 1.
Suppose that an integer is stored in a byte (8 bits). If a is an unsigned integer variable, when a = 64, the overflow value is 0 when moving one bit to the left, while when moving two bits to the left, the overflow value contains 1.

6. Right shift operator ( > > )
The right shift operator is used to shift the binary bits of a number to the right by several bits. The moving bits are specified by the right operand (the right operand must be non-negative). For signed Numbers, some machines fill the left blank with a sign bit (the "arithmetic shift"), while others fill the left blank with a zero (the "logical shift"). note
For unsigned Numbers, when moving to the right, the left high position moves into 0; For a signed value, if the original sign bit is 0(the number is positive), then the left side is also shifted
The 0. If the symbol bit was originally 1(that is, a negative number), the left side is moved to 0 or 1, depending on the computer system used. Some systems go to 0, some systems go to 1. Moving in 0 is called logical shift, that is, simple shift. Moving in one is called an arithmetic shift.

Example: the value of a is the octal number 113755:
    A :1001011111101101 (in binary form)
    a. > > 1: 0100101111110110 (logical right shift)
    a. > > 1: 1100101111110110 (arithmetic right shift)
    In some systems,a > > 1 gets the octal number 045766, while on other systems you might get 145766. Turbo C and some other C's
The compiler USES arithmetic shift to the right, that is, if the symbol bit is 1, the left side is 1.

The source code:


#include <stdio.h>
main()
{
int a=0113755;
printf("%d",a>>1);
}

7. Bit operation assignment operator
Bit operators and assignment operators can form a compound assignment operator.
    For example: &=, |=, > > =, < < Sunday afternoon = =
    A & = b is equivalent to a = a & b
                a. < < Lambda is equal to 2 is the same thing as a is equal to a < < 2


Related articles: