# C ++ shift operator and shift operator

Source: Internet
Author: User
Tags bitset

C ++ shift operator and shift operator

Previously, we saw that in C ++ standards, the behavior of the shift operator (<,>) out of the bounds is not determined:

The behavior is undefined if the right operand is negative, orgreater than or equal to the length in bits of the promoted left operand.

I did not go into this issue at the time. A netizen wrote a letter a few days ago asking about this, and I found that this is related to the shift operation of IntelCPU. The following is a letter from the netizen and my reply:

Hello! Operator <as an efficient operation in bitwise operations, But I encountered a problem: the following is a problem that is hard to understand in the VC environment, which is marked below.

# Include <stdio. h>

Void main ()

{

Unsigned int I, j;

I = 35;

// Why are the results of the two left-shift operations different?

J = 1 <I; // j is 8

J = 1 <35; // j is 0

}

I don't know where it is.

The reason is as follows: I = 35; j = 1 <I; the two statements are compiled into the following machine commands without VC optimization:

Mov dword ptr [I], 23 h

Mov eax, 1

Mov ecx, dword ptr [I]

Shl eax, cl

Mov dword ptr [j], eax

In the shl statement, eax = 1, cl = 35. When IntelCPU executes the shl command, it first performs and operations on cl and 31 to limit the number of shifts left to 31 or less. Because 35 & 31 = 3, this command is equivalent to moving 1 to three places, and the result is 8.

J = 1 <35; a constant operation. Even if VC is not optimized, the compiler directly calculates the result of 1 <35. When the VC compiler finds that 35 is greater than 31, the result is directly set to 0. The machine commands generated by this line of code compilation are:

Mov dword ptr [j], 0

In the above two cases, if the optimization switch of the VC compiler is enabled (for example, compiled into the Release version), the compiler will directly set the result to 0.

Therefore, in C/C ++, the shift operation should not exceed the limit. Otherwise, the result is unpredictable.

The following describes how the shl command limits the number of shifts in Intel documents:

The destination operand can be a register or a memory location. the count operand can be an immediate value or register CL. the count is maskedto 5 bits, which limits the count range to 0 to 31. A special opcode encodingis provided for a count of 1.

It is a string of binary operators that perform bitwise AND operations on the target field, shielding the current input bit.

The source code and mask are logically computed to obtain new operands. Logical operations such as OR are required. AND operation. For example, replace uppercase and lowercase letters with lowercase letters in the ASCLL code.

2. bitwise OR conversion to complement

4. Usage: Enable a bit (|)

5. Usage: Close the BIT (&~)

6. Usage: transpose (^)

7. Set the bit_number position of Value to 1 Value | = 1 <bit_number;

8. Set the bit_number position of Value to 0 Value & = ~ (1 <bit_number );

9. value & 1 <bit_number if this position has been set to 1, the expression result is a non-zero value.

C/C ++ provides bitwise logical operators and shift operators. They can only be used for shaping and regression. Bitwise operators operate on each operator without affecting the left and right sides. This is different from conventional operators (& | !) Is to operate the entire number.

I. bitwise logical operators

1 .~ Bitwise Inversion

Change 1 to 0 and 0 to 1.

EG:

~ (10011010)

(01100101)

Note:

VC ++ compiler, computation ~ 10. The result is-11. Why is it not 5?

The binary value of 10 is 1010, And the bitwise decimal value is 0101, that is, 5 in decimal format. Why is it-11?

VC is a 32-bit compiler, so

10 = 00000000 00000000 00000000 00001010

~ 10 = 11111111 11111111 11111111 11110101 =-11

You can use the mask (bit and)

15 = 00000000 00000000 00000000 00001111

~ 10 = 00000000 00000000 00000000 00000101 =-11

2. & bitwise AND

Only the result of both operands is 1. Otherwise, the value is 0.

10 = 00000000 00000000 00000000 00001010

12 = 00000000 00000000 00000000 00001100

&

8 = 00000000 00000000 00000000 00001000

3. | bitwise OR

If either of the two operands is 1, the result is 1.

10 = 00000000 00000000 00000000 00001010

12 = 00000000 00000000 00000000 00001100

|

14 = 00000000 00000000 00000000 00001110

4. ^ bitwise OR

The two operands are 1 and 0 respectively.

10 = 00000000 00000000 00000000 00001010

12 = 00000000 00000000 00000000 00001100

^

14 = 00000000 00000000 00000000 00000110

Mask is used to set some bits to on (1) and some bits to off (0) through & (bit and ). The mask 0 is not transparent, and 1 is transparent.

EG:

For example, only the second and third digits are displayed.

107 = 0110 1011

6 = 0000 0110

&

2 = 0000 0010

6. Usage: Enable a bit

The enable bit is to open the special location of a value through | (bit or), while keeping the other bits unchanged. This is because 0 or both are 0, and 1 or both are 1.

EG:

For example, only the second and third places can be opened.

107 = 0110 1011

6 = 0000 0110

|

111 = 0110 1111

7. Usage: Disable bits

Disable some bits

EG:

For example, disable the second and third places.

107 = 0110 1011

6 = 0000 0110

&~

105 = 0110 1001

8. Usage: transpose

If one digit is 1, it is transposed to 0. If one digit is 1, it is transposed to 0.

EG:

Such as transpose the second and third places

107 = 0110 1011

6 = 0000 0110

^

105 = 0110 1101

Ii. Shift Operators

1. <Move left

The Left shift operator moves each bit of the operand value to the left. The number of digits to be moved is determined by the right operand. The number of digits left blank on the right is filled with 0.

EG:

Such as transpose the second and third places

107 = 0110 1011 <2

<

172 = 1010 1100

In the computer, because it is a 32-bit

107 = 0000 0000 0000 0000 0000 0000 0110 1011 <2

<

428 = 0000 0000 0000 0000 0000 0001 1010

1. > Right shift

The right-shift operator moves each bit of the operand value to the right. The number of digits to be moved is determined by the right-hand operand. The number of dropped digits on the left is filled with 0.

EG:

Such as transpose the second and third places

107 = 0110 1011> 2

>

26 = 0001 1010

1. Basic operations:

Use an unsigned int variable as a bit container.

2. operators:

| Bitwise OR operator: result = exp1 | exp2; when at least one of the bits in exp1 and exp2 is 1, the bits in result is 1; otherwise, the bits are 0.

& Bitwise AND operator: result = exp1 & exp2; when all the corresponding bits in exp1 and exp2 are 1, the corresponding bits in result are 1; otherwise, the value is 0.

^ Bitwise OR or operator: result = exp1 ^ exp2; when the corresponding bits in exp1 and exp2 are not the same, the corresponding bits in result are 1; otherwise, the value is 0.

~ Inverse Operator: reverses all bits in the bit container, and 1 Changes to 0 to 1.

<Bitwise left shift operator: exp <n, shifts all bits in the container to the left, and fills the empty bits with 0.

> Shift right by bit operator: exp> n. Shift all bits in the container to the right, and fill the empty bits with 0.

| =, & =, ^ = Respectively | & ^ compound operators of the three operators.

3. Common Operations

Here we assume that there is an unsigned int variable for the result to store the scores of 32 students (0 and 1 for pass and fail respectively ), in this way, the result has 33 bits (the number of BITs is calculated from right to left and from 0. In this example, 0 bits are wasted ).

(A) Set the 27th bits to pass (set as 1) and the other BITs remain unchanged:

Result | = (1 <27) // any bit value and 1 are bitwise or the operation value is 1, while the bitwise with 0 is the same as the operation value.

(B) Set 27th bits to fail (set to 0 ).

Result & = ~ (1 <27) // any bitwise value is equal to 0 for bitwise and operation. Its value is equal to 0 for bitwise AND operation with 1 for its value unchanged.

(C) Reverse the value of the 27th-bit.

Result ^ = (1 <27) // any bitwise value differs from 1 by bit or the value of the operation is 1, while the bitwise value of 0 is the same as the value of the operation.

Ii. bitset container in C ++

# Include <bitset>

2. Declare a container:

(A) declare an empty container with the specified number of digits (all bits are set to 0): bitset <int> bits;

(B) declare a specified number of digits and initialize the specified number of bits into a corresponding value container: bitset <n> bits (int );

Bitdet <int> bits (string &)

Summary: The type parameters in the bitset template class pass the number of containers, while the constructor parameters use an int or string & value to initialize the corresponding values in the container from right to left.

3. Basic usage of bitset:

 Operation Function Usage Test (pos) Is the pos bit 1? A. test (4) Any () Is any bit 1? A. any () None () Is no bit 1? A. none () Count () The decimal point with a value of 1 Count () Size () Number of bit Elements Size () [Pos] Access pos A  Flip () Flip all bits A. flip () Flip (pos) Flip pos bit A. flip (4) Set () Place all positions 1 A. set () Set (pos) Set pos position 1 A. set (4) Reset () Set all positions to 0 A. reset () Reset (pos) Set pos position 0 A. reset (4)

4. bitset and traditional C-bit operations and String Conversion

You can use the to_string () member to convert the container to a string. You can also use the to_long () member to output the container to a traditional C-style bit container. For example:

Unsigned long bits = bits. to_long ();

Sting str (bits. to_string ());

Recommended: http://www.cnblogs.com/roucheng/p/cppjy.html

Related Keywords:
Related Article

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.