Deep understanding of C + + Shift operators _c language

Source: Internet
Author: User
Tags bitset bitwise bitwise operators logical operators

On the logical shift, the arithmetic shift can be seen in the Thunder deep large part of the question. A problem.

Previously seen in C + + standard, the behavior of the shift operator (<<, >>) out of bounds is not certain:

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

I did not delve into the problem at that time. A few days ago a netizen letter asked about this, I found that this and intelcpu shift operation. Below is the Netizen's letter and my reply:

Hello! Operator << as an efficient operation in bit operations, but I encountered a problem: The following in the VC environment to find a very confused place, the following callout.

#include <stdio.h>

void Main ()

{

  unsigned int i,j;

  i=35;

 

  Why do the following two left-shift operations result differently?

  j=1<<i;//J is 8

  j=1<<35;//J to 0

}

I don't know where I don't understand.

The reason is such:i=35;j=1<<i; these two sentences in VC did not do optimization, will be compiled into the following machine instructions:

mov DWORD ptr [i],23h

MOV eax,1

mov Ecx,dword ptr [i]

SHL EAX,CL

mov DWORD ptr [J],eax

In the SHL sentence, eax=1,cl=35. When INTELCPU executes the SHL instruction, the CL and the 31 are first and then manipulated to limit the number of left shifts by less than or equal to 31. Because of & 31 = 3, this instruction is equivalent to moving 1 left 3 digits, resulting in 8.

And j=1<<35; is a constant operation, VC even if not optimize, the compiler will directly calculate the results of 1<<35. The VC compiler finds that 35 is greater than 31 o'clock, and the result is set to 0 directly. The machine instructions generated by this line of code are:

mov DWORD ptr [j],0

In both cases, if you turn on the VC compiler's optimization switch (for example, compile to release version), the compiler will set the result directly to 0.

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

http://hovertree.com/menu/cpp/

The following is a description of the number of SHL instruction restriction shifts in the Intel documentation:

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

1. Masking code

It's a string of 2. Bitwise AND operation of the target field, shielding the current input bit.

The source code and mask after the logical operation of the new operand. A logical operation such as an OR operation is used. and operations. Used to convert uppercase letters in ASCLL code to lowercase letters.

2. With or XOR or converted into complement operation

3. Usage: Mask (&)

4. Usage: Open bit (|)

5. Usage: Close bit (&~)

6. Usage: Transpose position (^)

7. Place the value of Bit_number position 1 value |= 1 << bit_number;

8. Place the value of Bit_number position 0 value &= ~ (1 << bit_number);

9.value & 1 << bit_number If the position has been set to 1, the result of the expression is a value other than 0

/C + + provides bitwise logical operators and SHIFT operators. They can only be used for shaping and character types. Bitwise operators operate on each bit without affecting the left and right two bits, unlike the general operators (&&| | !) is the entire number of operations.

A Bit logical operators

1. ~ Counter by position

Change 1 to 0, 0 to 1

EG:

~ (10011010)

(01100101)

Note:

VC + + compiler, calculated ~10, the result is-11. Why not 5?

10 of the binary representation of 1010, by the counter should be 0101, that is, the decimal 5, why do we get-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) with the 15-bit

15 = 00000000 00000000 00000000 00001111

~10 = 00000000 00000000 00000000 00000101 =-11

2. & Bitwise Access and

Only two operands are 1 result is 1, otherwise 0

10 = 00000000 00000000 00000000 00001010

12 = 00000000 00000000 00000000 00001100

&

8 = 00000000 00000000 00000000 00001000

3. | by bit or

Two operands any one is 1 and the result is 1.

10 = 00000000 00000000 00000000 00001010

12 = 00000000 00000000 00000000 00001100

|

14 = 00000000 00000000 00000000 00001110

4. ^ per-bitwise XOR OR

Two operands are different to 1, the same is 0

10 = 00000000 00000000 00000000 00001010

12 = 00000000 00000000 00000000 00001100

^

14 = 00000000 00000000 00000000 00000110

5. Usage: Mask

Masks are set to open (1) by & (bit and), and some bits are set to OFF (0). See mask 0 as opaque and 1 transparent.

EG:

If only the second to third digit is displayed

107 = 0110 1011

6 = 0000 0110

&

2 = 0000 0010

6. Usage: Open bit

The open bit is a special location that opens a value by | (bit or), while keeping the other bits unchanged. This is because and 0 bits or both are 0, and 1 bits or both are 1.

EG:

If only the second to third digit is opened

107 = 0110 1011

6 = 0000 0110

|

111 = 0110 1111

7. Usage: Close bit

Close some bits

EG:

such as closing the second to third digit

107 = 0110 1011

6 = 0000 0110

& ~

105 = 0110 1001

8. Usage: Transpose position

If one is 1, transpose to 0, and if one is 1, transpose to 0

EG:

such as transpose the second to third place

107 = 0110 1011

6 = 0000 0110

^

105 = 0110 1101

Two Shift operator

    1. << Move Left

The left-shift operator moves each digit of the operand's value to the left, and the number of digits that are moved is determined by the right-hand operand, with 0 padding on the right

EG:

such as transpose the second to third place

0110 = 1011 <<2

<<

172 = 1010 1100

In the computer because it's a 32-bit

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

<<

428 = 0000 0000 0000 0000 0000 0001 1010 1100

    1. >> Move Right

The right shift operator moves each bit of the operand's value to the right, and the number of digits that are moved is determined by the operand to the right, with a 0 padding for the left dropped digits

EG:

such as transpose the second to third place

0110 = 1011 >>2

>>

26 = 0001 1010

First, the traditional C-mode bit operation:

1. Basic Operation:

Use a unsigned int variable to act as a bit container.

2. Operator:

| Bitwise OR operator: RESULT=EXP1|EXP2 when at least one of the corresponding bits in EXP1 and EXP2 is 1 o'clock, the corresponding bit in result is 1, otherwise 0.

& Bitwise AND operator::result=exp1&exp2; when EXP1 and Exp2 correspond to all 1 o'clock, the corresponding bit in result is 1, otherwise 0.

^ Bitwise XOR OR Operator: RESULT=EXP1^EXP2; when the corresponding bit in EXP1 and EXP2 is different, the corresponding bit in result is 1, otherwise it is 0.

~ Reverse operator: Reverses all bits in a bit container, 1 becomes 0, and 0 becomes 1.

<< bitwise LEFT Operator: Exp<<n, all the bits in the container are shifted to the left n bit, and the vacated bits are filled with 0.

>> bitwise RIGHT Operator: Exp>>n, moves all the bits in the container to the right n bit, and the vacated bits are filled with 0.

The |=,&=,^= corresponds to the composite operator of the |&^ three operators respectively.

3. Common operation

Here we assume that there is a result of the unsigned int variable used to store the scores of 32 students (by and without using 0 and 1 respectively), so that results have 33 digits (result from right to left, starting from 0 to calculate the number of digits, in this case 0 bits are wasted).

(a) The 27th digit is set to pass (set to 1) other bits unchanged:

result|= (1<<27)//any bit value and 1 bitwise OR operation its value is 1, and with 0 bitwise AND operation its value unchanged

(b) The 27th digit is set to fail (set to 0).

result&=~ (1<<27)//arbitrary bit value and 0 bitwise AND operation its value is 0, and 1 is bitwise AND operation its value unchanged

(c) Reverse the value of the 27th digit.

result^= (1<<27)//arbitrary bit value and 1 for bitwise XOR OR operation its value is 1, and 0 is bitwise XOR and operation its value is unchanged

Second, C + + in the Bitset container

1. header file:

#include <bitset>

2. Declare a container:

(a) Declare an empty container with a specified number of digits (all bits set to 0): bitset<int> bits;

(b) A container that declares a specified number of digits and initializes a specified number of digits to the corresponding value: bitset<n> bits (int);

Bitdet<int> bits (string&)

Summary: The bitset in the template class passes the number of bits of the container, and the constructor parameter initializes the corresponding value in the container from right to left through an int or a string& value.

The basic usage of 3.bitset:

Operation

Function

Usage

Test (POS)

is the POS position 1?

A.test (4)

Any ()

Is any bit 1?

A.any ()

None ()

Is there a bit 1?

A.none ()

Count ()

A decimal number with a value of 1 digits

Count ()

Size ()

Number of bit elements

Size ()

[POS]

Access POS bit

A[4]

Flip ()

Flip All Bits

A.flip ()

Flip (POS)

Flip Pos bit

A.flip (4)

Set ()

Place all Locations 1

A.set ()

Set (POS)

Place POS Position 1

A.set (4)

Reset ()

Place all Locations 0

A.reset ()

Reset (POS)

Place POS Position 0

A.reset (4)

4.bitset and traditional C-bit operation and string conversion

You can export a container to a string by to_string () member, and you can also use a to_long () member to export the container to a traditional C-style bit container. Such as:

unsigned long bits = Bits.to_long ();

Sting str (bits.to_string ());

The above in-depth understanding of the C + + shift operator is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.