For example: 9&; 5 The writable formula is as follows: 00001001(two's complement of 9)&; 00000101(twos complement of 5) 000001(twos complement of1) can be seen in 9&; 5= 1。 Bitwise AND operation is usually used to clear some bits or keep some bits. For example, clear the upper eight bits of A and keep the lower eight bits. 255 operation (the binary number of 255 is11111). main(){inta=9,b=5,c; c = a & ampb; printf(a=%d\nb=%d\nc=%d\n,a,b,c); } The bitwise OR operator' |' is a binocular operator. Its function is the binary OR of two numbers involved in the operation. As long as one of the two corresponding binaries is 1, the resulting bit is 1. The two numbers involved in the operation are complementary.
For example: 9|5 can be written like this:? 0000 100 1? |? 00000101= 000011(decimal 13) displays 9 | 5 =13main () {inta = c = a | b; printf(a=%d\nb=%d\nc=%d\n,a,b,c); } Bitwise XOR operator'' is a binocular operator. Its function is that the binary corresponding to the two numbers involved in the operation is different or, and when the corresponding binary is different, the result is 1.
The participating operands still appear as complements.
For example, 9 5 can be written like this:? 0000 100 1? ? 00000101= 00001100 (decimal12) main () {inta = 9; a=a^ 15; printf(a=%d\n,a); The negative operator ~ is a monocular operator of right combination. Its function is to negate every binary number involved in the operation bit by bit.
For example, the negation of ~9 is ~( 100 1), and the result is: 0 1 10 left shift operator "<< This is a binocular operator. Moving n bits to the left is equal to multiplying by the n power of 2. Its function puts "
1) Example: a
2) Example: int i =1; i = i & lt& lt2; //Move the value in I to the left by 2 bits, that is, the binary of 1 is 000 ... 0001(the number of zeros before1is related to the number of digits in int, and there are 3 1 zeros in gcc of 32-bit computer), and it will become 000 .. 065433. So moving 1 bit to the left is equivalent to multiplying by 2, so moving n bits to the left is multiplying by the n power of 2 (signed numbers are not completely applicable, because moving left may lead to sign changes, and the reasons are explained below).
One problem that needs attention is: the leftmost sign bit of int type and the situation of moving out. As we know, int is a signed integer, and the leftmost 1 bit is the sign bit, that is, 0 is positive 1 is negative, then overflow will occur when shifting, for example, int I = 0x40000000/16400000, binary 0100000.
In C language, most significant bit is discarded. When 1 is discarded, the value of I becomes 0. A special case of left shift is that when the number of digits shifted to the left exceeds the maximum number of digits of this numerical type, the compiler will use the number of digits shifted to the left to remove the maximum number of digits of the model, and then shift according to the remainder, such as: int i = 1, j = 0x80000000. //Let int be 32-bit I = I
In short, shifting to the left means: abandoning the highest bit and 0 making up the lowest bit. Right shift operator ">; > this is a binocular operator. Move n bits to the right and divide by the n power of 2.
Its function is to shift all the bits of the left operand of "> > by several bits to the right," >; > the number on the right specifies the number of digits to move.
For example, let a= 15, a >;; & gt2 means to move 0000111to the right to 000000 1 1 (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, most significant bit is 0, and when it is a negative number, the sign bit is 1, and whether most significant bit is 0 or 1 depends on the compilation system. Turbo C and many systems are defined as 1.
The right shift handles the sign bit differently from the left shift: for signed integers, such as int type, the right shift will keep the sign bit unchanged.
For example: int I = 0x80000000i = I>>1; The value of //i will not become 0x40000000, but 0xc00000000. That is to say, for a signed number, after the sign bit is shifted to the right, the positive number complements 0 and the negative number complements 1. For a signed number, when it moves to the right, the sign bit will move with it: it is positive, the highest bit is 0, and when it is negative, the sign bit is 65438. That is, the arithmetic right shift in assembly language. Similarly, when the number of digits moved exceeds the length of the type, the remainder will be taken and then moved by one bit. Whether the highest bit is 0 or 1 depends on the compilation system. Turbo C and many systems are defined as 1. Negative number10100110 >; 5 (assuming the word length is 8 bits), you get11111. In short, in C, the left shift is a logical/arithmetic left shift (both are exactly the same), and the right shift is an arithmetic right shift, which will be maintained.
The ratio is much higher. x & gt& gt 1; //Equivalent to x/= 2x
2 and 0 phase ∧, keep the original value.
3. Exchange two values without temporary variables. We can exchange variable values without introducing other variables. XOR operation can be realized: a = a^b;; //
( 1)b = a^b; //
(2)a = a^b; //
(3) XOR operation satisfies the associative law and commutative law. According to the properties of XOR operation, AA = 0 for any integer; Proof: (A in step (2) a) A = A B = (replace B with B in step (1)) A (A B) = B; (b) b = ab in step (3) = (b in step (1) and a in step (2) are replaced) A B A A B = A A A B = A clears the bit of 1 in a, and the corresponding bit of b is 0. Then let both &; Operation, you can achieve the purpose of clearing a.
Take some designated bits of a number, take some bits of a number, put some bits of b number in 1, and sum some bits of a number with 1 bitwise.
Method number A and method number B with one bit reserved are executed &; Operation, the number b in 1 bit is reserved in the number a, and the rest bits are zero.
Judging the parity will be the parity of the variable A. A performs bitwise AND operation with 1. If the result is 1, a is an odd number. A performs bitwise AND operation with 1. If the result is 0, A is an even number. Judge whether the int variable A is odd or even A &;; 1 = 0 Even number A &;; 1 = 1 odd number
Take the k-th bit (k = 0, 1, 2 ... the size of the int variable A (int)), that is, A >;; & gtk & amp 1
Clear the k bit of int variable a, that is, a = a &;; ~( 1 & lt; & ltk)
Set the kth position of int variable a 1, that is, A = A | (1
Int-type variables are cyclically shifted to the left k times, that is, a = a
The variable a of type int moves to the right k times, that is, a = a >;; & gtk | a<& lt 16-k (let sizeof(int)= 16)
Average value of integers
For two integers x, y, if the average value is (x+y)/2, overflow will occur, because x+y may be greater than INT_MAX, but we know that their average value will definitely not be modulo? What's the matter with you? /DIV & gt; Int average(int x, int y) // Returns the average value of x, y {return (x&; y)+((x^y)>; & gt 1); }
For the number x >, judge whether the integer is a power of 2; = 0, judge whether he is a power of 2 Boolean power 2 (int x) {return ((x&; (x- 1))= = 0)& amp; & amp(x! =0); }
Swap two integers void swap (int x, int y) without temp {x = y;; y ^= x; x ^= y; } PHP:$ a = ' DD '; $ b = ' bb$ a = $ a ^ $ b; $ b = $ a ^ $ b;
$ a = $ a ^ $ b; Echo $a,', $ b;; 10 calculates the absolute value intabs (int x) {int y; y = x & gt& gt3 1 ; Returns (x y)-y; //Or: (x+y) y)
Modular operations are converted into bit operations (no overflow). A% (2^n) is equivalent to a&(22 n-1)12+02 multiplication operation (no overflow). A * (2^n) is equivalent to a <<N 13. A/(2 n) is equivalent to one >:>n cases:12/8 =12 > & gt3 14. A% 2 is equivalent to a&; 1 15 if(x = = a)x = b; else x = a; Equivalent to x = a b x
The inverse of 16 x is (~x+ 1)abc.