operator meaning description
&
Bitwise AND if two corresponding bits are1, the result value of the bit is1, otherwise the0
|
Bitwise OR two corresponding bits as long as there is one for1, the result value of the bit is1
^ Bitwise
XOR or if the two bits values of the participating operations are the same0, otherwise the1
~ Take counter ~is a unary operator that is used to reverse the bitwise of a binary number and will0Change1and Will1Change0
<<
left shifts all bits of a number to the leftNbit, right complement0
>>Move
Right to move the bits of a number to the rightNbit, move to the right end of the low is discarded, for unsigned number, high0A bitwise operation is a binary-based operation. In system software, it is often necessary to deal with bits problems.Clanguage provides a6The single -digit manipulation operator. These operators can only be used for integer operands, that is,
only for signed or unsigned
char,short,int with a long type. 1, the bitwise-AND operator (&)Bitwise AND refers to: the two data that participates in the operation, the "and" operation according to Bits. If two corresponding bits are 1, the result value of this bit is1; otherwise0. Here's1that can be understood as a logicaltrue,0that can be understood as a logicalfalse. Bitwise AND is actually the same as the logical "and" arithmetic rules. The logic of "and", the demand for the operation of the whole truth, the result is true. If,A=true,b=true,theA∩b=trueFor example:3&5 3Binary encoding isOne (2). (in order to differentiate between decimal and other binary, this article stipulates that all non-decimal data are appended with parentheses after the data, and the binary is marked as2the basic unit of memory storage data is bytes (Byte), a byte is represented by a8Digit (bit)are composed. Bits are the smallest unit used to describe the amount of computer data. In binary systems, each0or1is a bit. Will be One(2) is made up of one byte, it is00000011(2). 5Binary encoding is101(2), which is a single byte, is00000101(2)Bitwise AND Operations: 00000011 (2) &00000101 (2) 00000001 (2)3&5=1CLanguage code:#include <stdio.h>main () {int A=3;int b = 5;printf ("%d", a&b);} Use of bitwise AND: (1) Qing 0If you want to clear a storage unit, even if all of its bits are0, just look for a binary number, where each bit meets the criteria:The original number is1bit, the corresponding bit in the new number is0. And then make them&operation, we can achieve the goal of clear 0. Example: The original number is +, i.e.00101011(2), find another number, set it to148, i.e.10010100(2), bitwise AND operation of the two:00101011(2)&10010100(2)00000000(2)CLanguage source code:#include <stdio.h>main () {int A=43;int b = 148;printf ("%d", a&b);} (2) Take some of the points in a number to locateIf there is an integerA (2byte),To take the low byte, simplyawith the8a1bitwise AND can. A 00101100 10101100b 00000000 11111111c 00000000 10101100 (3) reserved refers to positioning:With a number of "bitwise-and" operations, this number is taken at that bit1.For example, there is a number -, i.e.01010100(2), you want to take the first one from the left3,4,5,7,8bits are retained and the operation is as follows:01010100 (2) &00111011 (2) 00010000 (2) namely:a=84,b=59c=a&b=16cLanguage source code:#include <stdio.h>main () {int A=84;int b = 59;printf ("%d", a&b);} 2, the bitwise OR operator (|)Two corresponding bits as long as there is one for1, the result value of the bit is1. In the case of logic or arithmetic, one is true. For example: -(8)|17(8),the octal -with eight binary -perform a bitwise OR operation. 00110000|0000111100111111cLanguage source code:#include <stdio.h>main () {int A=060;int b = 017;printf ("%d", a|b);} Application: Bitwise OR operation commonly used to set the value of some bits of a data1. For example, if you want to make a numberathe Low4bit to1, you only need to add theawith the -(8) to perform a bitwise OR operation. 3, Exchange two values, without temporary variables such as: A=3, i.e. One(2); b=4, i.e. -(2). To swap the values of a and B, you can use the following assignment statement: a=a∧b;b=b∧A;A=a∧b;A=011 (2)(∧) b=(2)A=111 (2)(a∧bthe results,ahas become 7)(∧) b=(2)b=011 (2)(b∧athe results,bhas become 3)(∧) a=111 (2)A= -(2) (a∧bthe results,ahas become 4)is equivalent to the following two steps: ① executes the first two assignment statements: "A=A∧B;" and "b=b∧a;" Equalsb=b∧(A∧b). ② executes a third assignment statement: A=a∧b. Becauseathe value equals (a∧b),bthe value equals (b∧a∧b),Therefore, the equivalenta=a∧b∧b∧a∧b, i.e.athe value equals A∧a∧b∧b∧b, which equals B. It's amazing! CLanguage 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);} " operator~)He is a unary operator, which is used to find the binary inverse of integers, that is, each of the bits on the operands1into0,0into1. For example:~77 (8)Source code: #include <stdio.h>main () {int a=077;printf ("%d", ~a);} 5, left-shift operator (<<)The left shift operator is used to shift the bits of a number to the left by several bits, the number of bits moved is specified by the right operand (the right operand must be non-negative), and the right-hand empty bit is0to fill, the high-left-shift overflow discards the high. For example, theaBinary number left shift2Bit , the right vacated by the bit complement0, the left overflow bit is discarded. Ifa=15,that00001111(2), move left2Bit to00111100(2). Source code: #include <stdio.h>main () {int a=15;printf ("%d", a<<2);} Move left1the bit equals the number multiplied by the2, move left2the bit equals the number multiplied by the2*2=4,15<<2=60, which was multiplied by 4. However, this conclusion applies only to theA number that is overrun when left shifted is not included in the high1the situation. Suppose that an integer is stored in one byte (8 bits), and if A is an unsigned integer variable, the a= -When you move left one, the overflow is0, and move left2bit, the overflow high is included in the1. 6, right-shift operator (>>)The right-shift operator is used to move the bits of a number to the right of a number of bits, the number of bits moved is specified by the right operand (the right operand must be non-negative), the low end of the right side is discarded, and for the unsigned number, the high complement0. For signed numbers, some machines will be left blankFill (i.e. "arithmetic shift") with the sign bit, while others use the left empty part0(i.e. "logical shift"). NoteMeaning: For unsigned numbers,left high move in when right shift0; for signed values,if the original symbol bit is0 (the number is positive),the left is also the shiftInto0. If the symbol bit is originally1 (that is negative),the left side moves in0or is1,depends on the computer system you are using. Some systems move in0,someSystem move in1. Move in0called "logical Shift".,that is, simply shifting; moving in1is called "Arithmetic shift". Cases:aThe value is an octal number113755:A:1001011111101101(in binary form)A>>1:0100101111110110 (when logical right shifts)A>>1:1100101111110110 (when arithmetic right shifts)In some systems, A>>1have octal numbers045766,and on other systems, what you might get is145766. Turbo Cand some otherCThe compilation takes the arithmetic right shift,that is, when the signed number is shifted to the right,if the symbol bit is originally1, to the left of the high1. Source code: #include <stdio.h>main () {int a=0113755;printf ("%d", a>>1);} 7, bitwise operation Assignment OperatorsThe bitwise operator and the assignment operator can form compound assignment operators. For example: &=, |=, >>=, <<=,∧=Cases:A & = bequivalenta = A & B
Bit operators provided by the C language