Bitwise operators include: & (bitwise AND), | (bitwise OR), ^ (bitwise
Exclusive or ),~ (Bitwise inversion ).
The priority ranges from high to low ~ , &, ^, |,
Some simple applications of bitwise operators:
Bitwise AND operation have two typical usage: one is to take a single bit string
Information, for example, the following code intercepts the minimum 7 digits of X: x
& Amp; 0177. Second, let a variable retain a few digits, and the remaining positions are 0.
, As shown in the following code, keep X at least 6 bits: x = x & 077
.
A typical usage of bitwise OR operations is
Which is 1. If you want to obtain the rightmost 4 as 1, other bits and changes
Other bits of the number J are the same. Available logic or operation 017 | j
A typical usage of bitwise exclusive or operation is to obtain the information of a single bit string.
The inverse of several digits of information. For example, you want to obtain the rightmost 4 bits of integer variable J.
Information inversion. You can use the logic XOR operation 017 ^ J to obtain J.
The inverse of the rightmost 4 bits, that is, the first bits, and the result is 0,
The original value is 0 and the result is 1. Exchange two values.
For example, a = 3, B = 4. If you want to swap the values of A and B, you can
Use the following value assignment statement:
A = a ^ B; B = B ^ A; A = a ^ B;
Inverse operations are often used to generate constants irrelevant to system implementations.
If you want to set the minimum position of variable X to 0, the remaining BITs remain unchanged, available
Code X = x &~ 077. The above code and integer x use 2
Bytes are independent of 4 bytes.
When bitwise operations are performed on two data with different lengths (for example, long
Type data and INT type data ).
Alignment for bitwise operations. If the number of short values is positive, 0 is used for the high value.
Fill up; if the number of short values is negative, use 1 to fill up the number of upper values. If
If the short value is an unsigned integer, the high value is always filled with 0.
Bitwise operations are used to perform operations on the opposite string information to obtain the Bit String information.
. The following code removes the bit string of integer variable k:
The rightmost information bit of interest is 1: (k-1) ^ K) & K.
For signed data, if the first sign bit before the shift is 0 (positive
Number), then the left end is supplemented with 0; if the first symbol is shifted
If the value is 1 (negative), the left end is supplemented with 0 or 1, depending on the Calculation
Machine System. For the right shift of negative numbers, the system that uses 0 is
"Logical right shift". The system supplemented with 1 is "arithmetic right shift"
. The following code specifies the right of the system on which the reader runs.
Shift Method:
Printf ("% d/n",-2> 4 );
If the output result is-1, arithmetic shifts to the right.
If it is a big integer, it is the logical right shift.
Combined with bitwise operations, shift operations can achieve many bitwise string operations.
Related complex computing. Set the right-to-left sequence for variable bit encoding.
Number, from 0 to 15 bits. The expressions related to the positioning are not super
A positive integer over 15. The following code has their right note
Meaning:
(1) judge whether int type variable A is an odd or even number, especially
Is the determination of large numbers
A & 1 = 0 even
A & 1 = 1 odd
(2) Take the K-bit (k =, 2…) of int-type variable ......
Sizeof (INT ))
A> K & 1
(3) Clear the K bit of int variable A by 0
A = &~ (1 <K)
(4) Place the K position of int type variable A 1
A = A | (1 <K)
(5) int type variable loop shifts left K times
A = A <k | A> 16-k (set sizeof (INT) = 16)
(6) int type variable A shifts K times to the right
A = A> K | A <16-k (set sizeof (INT) = 16)
(7) Implement strings whose minimum N bits are 1 and whose remaining bits are 0
Information:
~ (~ 0 <n)
(8) truncate the n-bit information on the right of variable X starting from the P-bit:
(X> (1 + p-n ))&~ (~ 0 <n)
(9) intercept the row bit of the old variable and load the bit information
Set to the 15-k digit of the new variable.
New | = (old> row) & 1) <(15-K)
(10) If S is not equal to all 0, the code looks for the rightmost 1
Number of BITs J:
For (j = 0; (1 <j) & S) = 0; j ++ );