A review of the bitwise operators of C language

Source: Internet
Author: User
Tags bitwise bitwise operators

Reprinted and amended, original connection: http://www.cnblogs.com/911/archive/2008/05/20/1203477.html

A bitwise operation is a binary-based operation. In system software, it is often necessary to deal with bits problems.

the C language provides 6 bit manipulation operators .

These operators can only be used for integer operands, that is, only for signed or unsigned char,short,int and long types.

List of bit operators provided by the C language:
operator meaning description
& Bitwise AND if two corresponding bits are 1, then the result value of the bit is 1, otherwise 0 ( a false false)
| As long as there is one in the bitwise or two corresponding bits, the result value of the bit is 1 ( one true )
^ Bitwise XOR or if the two bits values of the participating operations are the same as 0, otherwise 1 (different is true )
~ Inverse ~ is a unary operator, used to reverse a binary number bitwise, will be 0 to 1, 1 to 0
<< left shifts all bits of a number to the left n bits, right 0
>> right shifts the bits of a number to the right by n bits, to the right end of the low is discarded, for unsigned number, high 0

1. Bitwise-AND operator (&)

Participate in the operation of the two data, press bits for "and" operation. If two corresponding bits are 1, the result value of the bit is 1; otherwise 0. The 1 here can be understood as the logic in which the true,0 can be interpreted as false in logic. 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.

Example: 3&5

the binary encoding of 3 is 11 (2). (in order to differentiate between decimal and other binaries, this article provides that all non-decimal data is appended with parentheses after the data, and the binary is marked as 2 in parentheses)

The basic unit of memory storage data is byte (byte), and a byte consists of 8 bits (bit). Bits are the smallest unit used to describe the amount of computer data. In a binary system, each 0 or 1 is a bit. 11 (2) is made up to one byte, then 00000011 (2).

The binary code of 5 is 101 (2), which is made up to one byte, then 00000101 (2)

Bitwise AND Operation:
00000011 (2)
&00000101 (2)
00000001 (2)
3&5=1

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

The use of bitwise AND:
(1) Zeroing
If you want to clear a storage unit, even if all of its bits is 0, just find a binary number, where each bit meets the following conditions:

The original number is 1 bits, and the corresponding bit in the new number is 0. Then make the two & operations, you can achieve the goal of clear 0.

Example: The original number is 43, that is 00101011 (2), another number, set it to 148, that is, 10010100 (2), the two bitwise AND operation:
00101011 (2)
&10010100 (2)
00000000 (2)

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

(2) Take some of the points in a number to locate
If you have an integer a (2byte), you want to take the low byte, you only need to put a and 8 1 bitwise with.
A 00101100 10101100
B 00000000 11111111
C 00000000 10101100


(3) retention refers to positioning:
With a number of "bitwise-and" operations, this number takes 1 in that bit.
For example, there is a number 84, or 01010100 (2), which would like to keep the 3,4,5,7,8 bit from the left, the operation is as follows:
01010100 (2)
&00111011 (2)
00010000 (2)
namely: a=84,b=59
C=a&b=16

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

2, bitwise OR operator (|)
Two corresponding bits as long as there is a value of 1, the result of this bit is 1. In the case of logic or arithmetic, one is true.

Application: Bitwise OR operation is commonly used to set a value of 1 for a certain bit of data.

For example, if you want to change the lower 4 bits of a number A to 1, you only need to

The A and 17 (8) are bitwise OR arithmetic.

3, Xor operator (^)
0 if the two bits values of the participating operations are the same, otherwise 1
namely 0∧0=0,0∧1=1,1∧0=1, 1∧1=0
Application:
(1) Turn specific bits upside down
With a number of 01111010 (2), want to make it low 4-bit flip, that is, 1 change 0,0 1. It can be "XOR" with 00001111 (2),

That
01111010
^00001111
01110101
The low 4 bits of the result of the operation are just the flip of the low 4 bits of the original number. As you can see, the few that you want to flip will be 1 to the ∧ operation.

(2) with 0-phase "XOR", retain the original value
Example: 012^00=012
00001010
^00000000
00001010
The original number is retained because 1 and 0 of the original number have an XOR operation of 1,0^0 0.

(3) Exchange two values without temporary variables
For example: A=3, i.e. one (2); b=4, i.e. 100 (2).
To swap the values of a and B, you can use the following assignment statements:
A=a∧b;
B=b∧a;
A=a∧b;

a=011 (2) (∧) b=100 (2)
a=111 (2) (a∧b result, A has become 7) (∧) b=100 (2)
b=011 (2) (b∧a result, B has become 3) (∧) a=111 (2)

A=100 (2) (the result of A∧b, A has become 4)
is equivalent to the following two steps:
① executes the first two assignment statements: "A=A∧B;" and "b=b∧a;" equivalent to B=b∧ (A∧B).
② executes a third assignment statement: A=a∧b. Since the value of a is equal to (A∧B), the value of B is equal to (B∧A∧B),

Thus, the equivalent of A=a∧b∧b∧a∧b, that is, the value of a equals a∧a∧b∧b∧b, equals B. This is not recommended and is not recommended for use in projects!

In a statement in C, it is best that the value of a variable is allowed to be changed only once, like x = + +, which is undefined behavior. There is no rule in C that the above is always correct.
In addition, using XOR or swap variables will not speed up the operation (instead, it will be slower, with six reads and three writes plus three XOR), and will not save space (the intermediate variable tmp is usually used as a register instead of a memory space store).

This technique is all about dealing with a sick interview, knowing it, and never put it in the product code. If the values of a and B are the same, the XOR will cause both A and B values to be 0, which is a great hidden danger in the program, so this is just an "interview technique".

4, "Inverse" operator (~)
Unary operator, which is used to calculate the binary inverse of integers, that is, 1 on each bits of the operand becomes 0, and 0 becomes 1.

5. Left shift operator (<<)
The left shift operator is used to move a number of each bits left several bits, the number of bits moved by the right operand is specified (the right operand must be non-negative ), the right side of the vacated bits with 0 to fill, high-left overflow to discard the high.

Shift left 1 is equal to the number multiplied by 2, the left Shift 2 is equal to the number multiplied by 2*2=4,15<<2=60, that is, by 4. However, this conclusion only applies if the number of the left-hand-abandoned high-level does not contain 1.

Suppose that an integer is stored in one byte (8 bits), and if A is an unsigned integer variable, then when the a=64 is shifted to the left one when the overflow is 0, and the left 2 bits, the overflow high position contains 1.

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 unsigned numbers, the high is 0. For signed numbers, some machines will fill the left empty portion with a sign bit (i.e. "arithmetic shift"), while others fill the left empty portion with 0 (i.e. "logical shift").

Attention:

For unsigned numbers, move left high to 0 when moving right;

For signed values, if the original sign bit is 0 (the number is positive), the left side is also moved in 0. if the sign bit turns out to be 1 (that is, negative), the left-hand side is 0 or 1, depending on the computer system used . Some systems move into 0, some

The system moves into 1. Moving into 0 is called "logical shift", i.e. simple shift, and moving into 1 is called "Arithmetic shift".

The right shift operation is equivalent to dividing by 2

7, bitwise operation Assignment operator

The bitwise operator and the assignment operator can form compound assignment operators.
For example: &=, |=, >>=, <<=,∧=
Example: A & = b equals A = A & B
A << = 2 equals a = a << 2

A review of the bitwise operators of C language

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.