C-language bit arithmetic

Source: Internet
Author: User
Tags bitwise bitwise operators

C language bit operation
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, then a∩b=true for example: 3&5 3 is the 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), which is made up to one byte, then 00000101 (2) Bitwise AND Operation:
00000011 (2)
&AMP;00000101 (2)
00000001 (2)
3&5=1
C Language code:
#include <stdio.h>
void Main ()
{
int a=3;
int b = 5;
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 conforms to the condition: the original number is 1 bits, 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>
void 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>
void 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>
void 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, Xor operator (^)
His rule is: if the two bits values of the operation are the same, then 0, otherwise 1, or 0∧0=0,0∧1=1,1∧0=1,1∧1=0.
Cases:
00111001
∧00101010
00010011
C Language Source code:
#include <stdio.h>
void Main ()
{
int a=071;
int b = 052;
printf ("%d", a^b);
}
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), i.e.:
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) (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), so 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>
void 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 (~)
It is a unary operator, which is used to find the binary inverse code 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>
void Main ()
{
int a=077;
printf ("%d", ~a);
}

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.
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), move the left 2 bits 00111100 (2).
Source:
#include <stdio.h>
void 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 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"). Note: For unsigned numbers, the left high is moved to 0 when you move right, and if the original sign bit is 0 (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, and some systems move 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&GT;&GT;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 compilers use 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>
void 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: &=, |=, >>=, <<=,∧=
Cases:
A & = B equals A = A & B
A << = 2 equals a = a << 2

A logical operators

1.& bit and arithmetic

1) Arithmetic rules

The essence of bit and operation is to take the two data that participates in the operation, and make the logic and operation according to the corresponding binary number bitwise. For example: Int constants 4 and 7 perform bitwise AND operations as follows:

4=0000 0000 0000 0100 &7 = 0000 0000 0000 0111 = 0000 0000 0000 0100

For negative numbers, the operation is performed by its complement. For example: for example: Int Constants-4 and 7 perform bitwise AND operations as follows: -4=1111 1111 1111 1100 &7 = 0000 0000 0000 0111 = 0000 0000 0000 0100

2) Typical applications

(1) Zeroing

Clear 0: Quickly to a piece of data unit data zeroing, will be all of its bits is 0. For example, an integer number a=321 the operation of zeroing all its data to a=a&0x0.

321 = 0000 0001 0100 0001 &0 = 0000 0000 0000 0000 = 0000 0000 0000 0000

(2) Get a specified bit of data

Gets the pointing position of a data. For example, the operation of getting the low eight-bit data for an integer number a= is A=a&0xff.

321 = 0000 0001 0100 0001 & 0xFF = 0000 0000 1111 11111 = 0000 0000 0100 0001

Operation for high eight-bit data with integer a= is a=a&0 xFF00. ==a&0xff00==

321 = 0000 0001 0100 0001 & 0xff00 = 1111 1111 0000 0000 = 0000 0001 0000 0000

(3) Warranty The special location of the data area  

preserves the special location of the data area. For example, the data operation for the 第7-8位 (starting at 0) bit of the integer a= is: 110000000

= 0000 0001 0100 0001 & 384 = 0000 0001 1000 0000 = 0000 0001 0000 0000

2. | bitwise OR Operation

1) The essence of the operation rule

Bit or operation is to logically or operate a bitwise of the two data participating in the operation according to the corresponding binary number. For example: the Int constants 5 and 7 perform a bitwise OR operation with the expression 5|7, the result is as follows: 5 = 0000 0000 0000 0101 | 7 = 0000 0000 0000 0111 = 0000 0000 0000 0111

2) main purpose
(1) Set a point of data positioning. For example, an integer number of a=321, the operation to set its low eight-bit data to 1 is a = A|0xff.

321 = 0000 0001 0100 0001 | 0000 0000 1111 1111 = 0000 0000 1111 1111
Logical Operators | | Differences from bitwise OR operator |
The condition or operator (| |) executes the logical OR operation of a bool operand, but computes the second operand only if necessary. x | | Y, x | Y is different, if x is true, Y is not evaluated (because the result of the or operation is true regardless of the Y value). This is referred to as a "short circuit" calculation.

3. ^ Bit XOR

1) Arithmetic rules

The essence of the bitwise XOR operation is that the two data of the participating operation are logically XOR by the corresponding binary digits. The result of the corresponding bit is true only if the binary number of the corresponding bit is mutually exclusive. For example: the int type constants 5 and 7 perform bitwise XOR and the expression is 5^7, the result is as follows:

5 = 0000 0000 0000 0101^7 = 0000 0000 0000 0111 = 0000 0000 0000 0010

2) Typical applications

(1) Positioning flip

Positioning Flip: Set the point of a data positioning, change 1 to 0, 0 for 1. For example, integer number a=321, the operation to turn its low eight-bit data to a bitwise:

A = A^0xff;

(2) Value Exchange
Numeric Exchange. For example a = 3,b = 4. In example 11-1, there is no need to introduce a third variable, and the data exchange can be realized by bitwise operation. The following actions enable the exchange of A, B, two data:

A = A^b;
b = b^a;
A = A^b;
4. ~ Bit non-

The essence of bit non-operation is the logical non-operation of the two data that participates in the operation, and bitwise the corresponding binary number.
Two Displacement operator
1. Bit left shift
The essence of the left shift operation is to shift the binary value of the corresponding data to the left bit by bit, and fill 0 in the vacated position, the highest bit overflow and discard.

For example:

int A, B;
A = 5;
b = a << 2;
Then B = 20, the analysis process is as follows:
(a) 10 = (5) 10 = (0000 0000 0000 0101) 2

b = a << 2;
b = (0000 0000 0001 0100) 2 = (20) 10
We can see from the above example that the bitwise operation can achieve twice times multiplication. The operation speed of the displacement operations is much higher than that of multiplication. Therefore, when the multiplication operation of the data is processed, the displacement operation can be used to obtain a faster speed.
It is suggested that all the multiplication operations of 2 are converted to displacement operations, which can improve the efficiency of the program operation.
2. Bit right Shift
The essence of the bitwise right SHIFT operation is to shift the binary values of the corresponding data to the right bit by bit, and discard the numbers that are out of bounds. If the current number is an unsigned number, the high is 0. For example:
Int (a) 10 = (5) 10 = (0000 0000 0000 0101) 2
b = a >> 2;
b = (0000 0000 0000 0001) 2 = (1) 10
If the current data is a signed number, when the right shift is made, the left 0 or 1 is determined by the sign bit. If the sign bit is 0, the left side is 0, but if the sign bit is 1, depending on the computer system, there may be different processing methods. You can see that the bitwise right SHIFT operation can be achieved by dividing the divisor to 2.
Prompt to convert all of the 2 Division operations into displacement operations, which can improve the efficiency of the program operation
3. Compound bitwise operators
Compound bitwise operators are also available in the C language, as follows:
&=,! =, >>=, <<=, and ^=
For example: a&=0x11 is equivalent to a= a&0x11, other operators and so on.
Different types of integer data in the mixed type of bit operations, the right-side alignment principle is processed, according to the data length of data processing, the data length of small data to the left 0 or 1. For example, when char a and int b perform bitwise operations, they are processed by int, and char A is converted to integer data and is 0 on the left.
The complement principle is as follows:
1) for signed data: If A is a positive integer, the left-hand is 0, and if a is negative, the left-hand is 1.
2) for unsigned data: 0 on the left.
4. Example
Example 11-2 obtains an n-bit binary data with unsigned data starting at p bit. Assuming that the right side of the data is aligned, the No. 0-digit binary number is at the right-most end of the data, and the resulting result requires right alignment.
#include
/*getbits: Gets the N-bit binary number starting from P bit */
unsigned int getbits (unsigned int x, unsigned int p, unsigned n)
{
unsigned int A;
unsigned int b;
A = x >> (p+1);
b = ~ (~0<
Return a&b;
}
It is suggested that the effective range of the basic data types of this system should be understood first in the program development of a platform, and the bit operations involved are evaluated, especially the boundary data to ensure the calculation is correct.

C-language bit arithmetic

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.