C, C + + in |, | |, &, &&, XOR, ~,! Detailed calculation

Source: Internet
Author: User
Tags arithmetic bitwise bitwise operators

due to contact with the lower level of data processing to the following use of finishing (reference network)
bit arithmetic The Operation component of a bitwise operation can only be integer or character data, and the bitwise operation considers the operand as a bit string information composed of binary bits, and completes the specified operation by bit, and obtains the result of the bit string information. The bitwise operators are: & (Bitwise and), | (bitwise OR), ^ (bitwise XOR), ~ (bitwise reversed). Where the bitwise negation operator is the monocular operator and the rest is the binocular operator. The bitwise operators take precedence from high to low, in the order of ~, &, ^, |, where ~ 's binding direction is from right to left, and precedence is higher than arithmetic operators, the rest of the operators are bound from left to right, and the precedence is lower than the relational operator.       (1) Bitwise AND operator (&)
        Bitwise AND Operation calculates the corresponding bits of the two operation components according to the following rules:
        0 & 0 = 0, 0 & 1 = 0, 1 & 0 = 0, 1 & 1 = 1. The
is the same as 1 bit, the result is 1, otherwise the result is 0.
        For example, the internal representation of 3 is
        00000011
        5 is internally expressed as
        00000101
        3&5 The result is
        00000001
        bitwise AND operations have two typical uses, one is to take a certain number of bits of string information, such as the following code to intercept the minimum 7 bits of x: X & 0177. The other is to let a certain variable hold a certain number of bits, the remaining position 0, such as the following code to keep x only the lowest 6 bits: x = x & 077. The above use is to design the good one constant, the constant only the required bit is 1, the unwanted bit is 0. Use it to bitwise with the specified bit string information.       (2) Bitwise OR operator (|)
        bitwise OR operation calculates the corresponding bits of the two operation components according to the following rules:
        0 | 0 = 0, 0 | 1 = 1, 1 | 0 = 1, 1 | 1 = 1
that is, as long as there is a bit of 1, the result is 1, otherwise 0.
        for example, 023 | 035 result is 037.
        A typical use of a bitwise OR operation is to place a number of bits of a bit string information into 1. If you are going to get the right 4 to 1, the other bits are the same as the other bits of the variable J, available in logic or Operation 017|j. To assign this result to the variable J, it can be written as:
        j = 017|j       (3) Bitwise XOR Operator (^)
        bitwise XOR computes the corresponding bit of the two operand components according to the following rules:
        0 ^ 0 = 0, 0 ^ 1 = 1, 1 ^ 0 = 1, 1 ^ 1 = 0
i.e. the corresponding bit has the same value, the result is 0, and the result is 1.
        For example, the 013^035 result is 026.
        xor operation means that the corresponding bit values of the two operand components are different, the difference is 1, the same is 0. A typical use of bitwise XOR is to find the inverse of a certain number of bits of information in a bit string. If the desired integer variable J of the right 4 bits of the inverse, with a logical XOR operation 017^j, can be obtained J Right 4 bits of information, that is, the original 1 bit, the result is 0, the original 0 bit, the result is 1. (4) Bitwise inverse operator ( ~)
the bitwise negation is the monocular operation, which is used to find the inverse of a bit string information, that is, which is 0 bits, the result is 1, and which is 1 bits , the result is 0. For example, the result of the 0XFFF8 is.
inverse operations are often used to generate constants unrelated to the implementation of the system. To make the variable x 6 position 0, the remaining bits will be the same, the code x = x & ~077 can be implemented. The above code is independent of the integer x with 2 bytes or 4 byte implementations.
when two different lengths of data are performed for bitwise operations (for example, Long data and int type data), the right end of the two operational components is aligned for bitwise operation. If the short number is positive, the high is filled with 0, and if the short number is negative, the high position is filled with 1. If the short is an unsigned integer, the high level is always filled with 0.
bit operation is used to compute the bit string information and get the result of bit string information. The following code can remove the bit string information of the integer variable K with the rightmost 1 information bit: ((k-1) ^k) & K. Shift Operations The shift operation is used to move integer or character data as a binary information string as a whole. There are two operators:
<< ( shift left) and >> (shift right)
The shift operation is binocular operation, there are two operational components, the left component is the shift data object, the right component value is the shift bit number. The shift operation regards the left operation component as a bit string information composed of binary, and shifts it to the left or right to obtain a new bit string information.
The shift operators have precedence over the arithmetic operators, which are higher than the relational operators, and are bound from left to right. (1 ) left shift operator (<<)
The left -shift operation shifts a bit string information to the left of the specified bit, and the right-side vacated bits are supplemented with 0. For example 014<<2, the result is 060, or 48.
when you move left, the right end of the empty is supplemented with 0, and the information of the bits moved out is discarded. In a binary number operation, if the information is not lost due to movement, the 1 bits per left shift is equivalent to multiply by 2. such as 4 << 2, the result is 16. (2) Right shift operator (>>)
The right-shift operation shifts a bit string information to the right of the specified bit, and the information for the bits moved out of the right side is discarded. For example 12&GT;&GT;2, the result is 3. In contrast to the left shift, for small integers, each right shifts 1 bits, which is equivalent to dividing by 2. When you move right, you need to be aware of the sign bit problem. For unsigned data, when moving right, the left-side vacated bits are supplemented with 0. For signed data, if the pre-shift sign bit is 0 (positive), the left-hand is also supplemented with 0, and if the pre-shift sign bit is 1 (negative), the left-hand is 0 or 1 supplemented, depending on the computer system. For negative right shift, the system called 0 complements is "logical right Shift", and the system with 1 complements is "arithmetic right shift". The following code illustrates the right-shift approach to the system used by the reader on the computer:
printf ("%d\n\n\n", -2>>4);
If the output is-1, the arithmetic is shifted to the right, and the output is a large integer, then the logical right shift.
The combination of shift operation and bitwise operation can realize many complex computations related to bit string operation. The position of the variable is numbered from right to left, from 0 bits to 15 bits, and the expression referring to the position is a positive integer not exceeding 15. The following code has the meanings shown in the comments on the right:
~ ( ~0 << N)
( x >> (1 p-n)) & ~ (~0 << N)
new |= ((Old >> row) & 1) << (15–k)
s &= ~ ( 1 << j)
for (j = 0; ((1 << J) & s) = = 0; j)
;===================================================================================================

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 this bit is 1, otherwise 0
| In the bitwise or two corresponding bits, as long as there is a value of 1, the result of that bit is 1.
^ Bitwise XOR or if the two bits value of the participating operation is the same as 0, otherwise 1
~ 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 (&)
      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 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. If, a=true,b=true, A∩b=true for example: 3&5 3 is a binary code of 11 (2). (in order to differentiate between decimal and other binaries, this article stipulates that all non-decimal data are appended with parentheses after the data, annotated with the binary in parentheses, and binary labeled as 2). 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 encoding of 5 is 101 (2) and is 00000101 (2)
Bitwise AND Operation:
00000011 (2)
&00000101 (2)
00000001 (2)
3 &5=1
C language code:
#include <stdio.h>
Main ()
{
int a=3;
int b = 5;
printf ("%d", a&b);
}
Bitwise AND Uses:
(1) Clear 0
If you want to clear a storage unit, even if all of its bits is 0, just look for a binary number, where each bit meets the criteria:

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)
C Language 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 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
C Language Source code:
#include <stdio.h>
Main ()
{
int a=84;
int B = 59;
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.


For example: 60 (8) |17 (8), octal 60 and octal 17 are bitwise OR operations.
00110000
|00001111
00111111
C Language Source code:
#include <stdio.h>
Main ()
{
int a=060;
int b = 017;
printf ("%d", a|b);
}
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 make a and 17 (8) bitwise OR operations.


3. Exchange two values without a temporary variable
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) (the result of A∧b, A has become 7)
(∧) b=100 ( 2)
b=011 (2) (the result of B∧a, 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.
It's amazing!
C Language 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);
}


4, "Inverse" operator (~)
He is a unary operator, which is used to find the binary inverse of integers, that is, 1 of each bits on the operand is changed to 0, and 0 becomes 1.
Example: ~77 (8)
Source:
#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 a non-negative

Value), the right-hand vacated bits are filled with 0, and the high-left-shift overflow is discarded.
For example: The binary number of a is shifted to the left 2 bits, the right side vacated the bit 0, left overflow bit discarded. If a=15, i.e. 00001111 (2), shift left 2

The position was 00111100 (2).
Source:
#include <stdio.h>
Main ()
{
int a=15;
printf ("%d", a<<2);
}
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 applies only to the

A number that is discarded when the left shift is dropped does not contain a 1 case.
Suppose that an integer is stored in one byte (8 bits), and if A is an unsigned integer variable, then when a=64, the left one is overrun by 0.

, while moving the left 2 bits, the overflow high has 1.


6. Right shift operator (>>)
The right-shift operator is used to move the bits of a number to the right of several bits, the number of bits moved is specified by the right operand (the right operand must be a non-negative

Value), move to the right end of the low is discarded, for unsigned number, high 0. For signed numbers, some machines will be left blank

Fill with the sign bit (i.e. "arithmetic shift"), while others fill the left empty part with 0 (i.e. "logical shift"). Note

Meaning: For unsigned numbers, the left-hand side moves to 0 when you move right, and if the original sign bit is 0 (positive) for signed values, the left side is also shifted

Into the 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".
Example: The value of a is octal number 113755:
a:1001011111101101 (in binary form)
a>>1:0100101111110110 (logical right shift)
a>>1:1100101111110110 (when arithmetic right shifts)
in some systems, A>>1 has an octal number of 045766, while on some other systems it is possible to get 145766. Turbo C and some other C

The compilation takes the arithmetic right shift, that is, when the signed number is shifted to the right, if the sign bit is 1, the left side of the high is 1.
Source:
#include <stdio.h>
Main ()
{
int a=0113755;
printf ("%d", a>>1);
}


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

C, C + + in |, | |, &, &&, XOR, ~,! Detailed calculation

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.