C language bitwise (2)

Source: Internet
Author: User
Tags bitwise operators

Bitwise operations are binary operations. In system software, binary bitwise is often required. The C language provides six operations

Operator. These operators can only be used for integer operands, that is, they can only be used for signed or unsigned char, short, Int, and long types.
List of bitwise operators provided by C language:
Operator description
& Bitwise AND if the two corresponding binary bits are both 1, The result value of this bit is 1, otherwise it is 0
| If one of the bitwise OR Two corresponding binary bits is 1, The result value of this bit is 1.
^ Bitwise OR 0 if the two binary values involved in the operation are the same; otherwise, 1
~ Reverse retrieval ~ It is a unary operator used to reverse a binary number by bit. It is about to change 0 to 1 and Change 1 to 0.
<Left shift is used to shift all the binary bits of a number to N places left, and 0 to the right
> Shift right: shifts the binary digits of a number N places to the right, and moves them to the low position on the right to be discarded. For the unsigned number, add 0 to the high position.

1. bitwise AND operator (&)

Bitwise AND refers to the two data involved in the operation, and the "and" operation is performed in binary bits. If both of the corresponding binary bits are 1,

The result value of this bit is 1; otherwise, it is 0. Here, 1 can be interpreted as true in logic, and 0 can be interpreted as false in logic. Bitwise

The actual and logical operation rules are the same. The logical "and" requires that the number of operations be true and the result is true. If,

If a = true and B = true, then a bytes B = true. For example, the binary encoding of 3 & 5 3 is 11 (2 ). (In order to distinguish between decimal and other hexadecimal systems

No. All non-decimal data is enclosed by brackets, which indicate the hexadecimal notation, And the binary value is marked as 2) memory storage data.

The basic unit is byte. A byte consists of eight bits. Bit is the minimum unit used to describe the data size of a computer. II

In the hexadecimal system, each 0 or 1 is a single bit. If 11 (2) is supplemented into one byte, It is 00000011 (2 ). The binary code of 5 is

101 (2), complement it into a byte, then 00000101 (2)
Bitwise AND operation:
00000011 (2)
& 00000101 (2)
00000001 (2)
3 & 5 = 1
C LanguageCode:
# Include <stdio. h>
Main ()
{
Int A = 3;
Int B = 5;
Printf ("% d", A & B );
}
Usage of bitwise AND:
(1) resetting
If you want to clear a storage unit, even if all its binary bits are 0, you only need to find a binary number, each of which meets the following conditions:

The original number is 1, and the corresponding digit in the new number is 0. Then perform the & Operation on the two to achieve the goal of clearing.
For example, if the original number is 43, that is, 00101011 (2), find another number and set it to 148, that is, 10010100 (2), bitwise and operation of the two:
00101011 (2)
& 10010100 (2)
00000000 (2)
C LanguageSource code:
# Include <stdio. h>
Main ()
{
Int A = 43;
Int B = 148;
Printf ("% d", A & B );
}
(2) locate certain indicators in a number
If you have an integer a (2 byte) and want to take the lower byte, you only need to bitwise A and 8 1.
A 00101100 10101100
B 00000000 11111111
C 00000000 10101100
(3) Retention refers to positioning:
Perform the bitwise AND operation with a number. This number is equal to 1.
For example, if you want to retain the numbers 84, I .e. 01010100 (2) from the left, the numbers 3, 4, 5, 7, and 8 are as follows:
01010100 (2)
& 00111011 (2)
00010000 (2)
That is, a = 84, B = 59
C = 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 (|)
If one of the two binary bits is 1, The result value of this bits is 1. In logic or operations, it is true.

.
For example, 60 (8) | 17 (8), bitwise OR operation is performed on October 60 and October 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 operations are usually used to set a certain bit value of a data to 1. For example, if you want to change the 4-digit low of A to 1, you only need

Perform bitwise OR operations on a and 17 (8.

3. "XOR" Operator (^)
The rule is: if the two binary values involved in the calculation are the same, the value is 0; otherwise, the value is 1.
0 then 0 = 0 then 1 = 1 then 0 = 1 then 1, 1 then 1 = 0
Example: 00111001
Limit 00101010
00010011
C language source code:
# Include <stdio. h>
Main ()
{
Int A = 071;
Int B = 052;
Printf ("% d", a ^ B );
}
Application:
(1) enable specific positioning to flip
There is a number of 01111010 (2), and you want to lower it by 4 bits, that is, 1 to 0 to 0 to 1. You can perform the "XOR" operation on it and 00001111 (2,

That is:
01111010
^ 00001111
01110101
The lower 4 bits of the calculation result is exactly the four bits of the original number. It can be seen that the number of bits to be flipped will be 1 for the number of bits to be processed by the merge operation.

You can.
(2) The original value is retained if it is different from 0.
Example: 012 ^ 00 = 012
00001010
^ 00000000
00001010
The original number is retained because the values of 1 and 0 in the original number are exclusive or calculated to 0, 0 ^ 0, and 0.
(3) exchange two values without temporary variables
For example, a = 3, that is, 11 (2); B = 4, that is, 100 (2 ).
To swap values of A and B, use the following value assignment statement:
A = A then B;
B = B then;
A = A then B;
A = 011 (2)
(Bytes) B = 100 (2)
A = 111 (2) (a has changed to 7)
(Bytes) B = 100 (2)
B = 011 (2) (B has a result, B has changed to 3)
(Bytes) A = 111 (2)

A = 100 (2) (a has changed to 4)
This is equivalent to the following two steps:
① Execute the first two assignment statements: "A = A then B;" and "B = B then a;" is equivalent to B = B then B ).
② Execute the third value assignment statement: A = A then B. Because the value of A is equal to (a then B), the value of B is equal to (B then a then B ),

Therefore, it is equivalent to a = A then B then a then B, that is, A is equal to a then B, equal to B.
Amazing!
C language source code:
# Include <stdio. h>
Main ()
{
Int A = 3;
Int B = 4;
A = a ^ B;
B = B ^;
A = a ^ B;
Printf ("A = % d B = % d", a, B );
}

4. "inverse" Operator (~)
It is a unary operator used to evaluate the binary anticode of an integer, that is, to change the value 1 of each binary bit of the operand to 0 to 1.
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 remove multiple digits from the left of each binary number. The number of digits to be moved is specified by the right operand (the right operand must be non-negative ).

Value), and the right-side empty bits are filled with 0, and the high left overflow is discarded.
For example, if the binary number of A is shifted to two places, the empty bit on the right is supplemented with 0, and the overflow bit on the left is discarded. If a = 15, that is, 00001111 (2), Move 2 to the left.

The value is 00111100 (2 ).
Source code:
# Include <stdio. h>
Main ()
{
Int A = 15;
Printf ("% d", a <2 );
}
The value of one left shift is equal to the number multiplied by 2, and the value of two left shifts is equal to the number multiplied by 2*2 = <2 = 60, that is, 4. However, this conclusion only applies

When the number is shifted to the left, the overflow Discard high does not contain 1.
Assume that an integer is stored in one byte (8 bits). If expression A is an unsigned integer variable, the overflow value is 0 when expression A = 64 and expression a shifts one bit left.

When the Left shift is 2 bits, the overflow high contains 1.

6. Right Shift Operator (>)
The right-shift operator is used to shift the binary digits of a number to several places. The number of digits to be moved is specified by the right-hand operand (the right-hand operand must be non-negative.

Value), the low position to the right is discarded. For the unsigned number, the high position is supplemented by 0. For the signed number, some machines will leave the blank part on the left.

Fill with the symbol bit (that is, "arithmetic shift"), while other machines use 0 to fill the left blank part (that is, "logical shift "). Note

Meaning: For the unsigned number, move the upper position on the left to 0 when shifting the right. For the signed value, if the original signed bit is 0 (This number is positive), move the value on the left as well.

Enter 0. If the original symbol bit is 1 (negative), the left side is moved to 0 or 1, depending on the computer system used. Some systems move to 0, some

System migration 1. Moving 0 is called "logical shift", that is, simple shift; moving 1 is called "arithmetic shift ".
For example, the value of a is 113755 in octal:
A: 1001011111101101 (in binary format)
A> 1: 0100101111110110 (logical right shift time)
A> 1: 1100101111110110 (arithmetic shift to the right)
In some systems, a> 1 gets 045766 bits, while in other systems, 145766 is possible. Turbo C and some other C

The compilation uses the arithmetic right shift, that is, when the number of symbols is shifted to the right, if the symbol bit is originally 1, the left side is shifted to 1.
Source code:
# Include <stdio. h>
Main ()
{
Int A = 0113755;
Printf ("% d", a> 1 );
}

7. bitwise assignment operator

Bitwise operators and value assignment operators can form a composite value assignment operator.
Example: <=, |=, >>=, <=, Signature =
For example, a & = B is equivalent to a = A & B
A <= 2 is equivalent to a = A <2

From: http://blog.sina.com.cn/s/blog_52543ad50100ak0s.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.