C + + Shift operators

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

For logical shift, arithmetic shift can be found in thunder deep large part of the question. A problem.

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

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

I did not delve into the question at that time. A few days ago a netizen letter asked this matter, I discovered, this and intelcpu shift operation is related. Here is the letter from the Netizen and my reply:

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

#include <stdio.h>

void Main ()

{

unsigned int i,j;

i=35;

Why do the following two left-shift operations not work as a result?

j=1<<i; J is 8

j=1<<35; J is 0

}

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

The reason for this is that the:i=35;j=1<<i; these two sentences are not optimized in the case of VC, 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 SHL's sentence, eax=1,cl=35. When INTELCPU executes the SHL instruction, CL is first performed with 31 and operates to limit the number of left shifts to less than or equal to 31. Because the & 31 = 3, such an instruction is equivalent to moving 1 to the left 3 bits, the result is 8.

And j=1<<35; is a constant operation, VC even do not optimize, the compiler will also directly calculate the results of 1<<35. VC compiler found that 35 is greater than 31 o'clock, will directly set the result to 0. This line of code compiles the machine instructions that are generated:

mov DWORD ptr [j],0

In both cases, if the VC compiler's optimization switch is turned on (such as compiling to release version), the compiler will directly set the result to 0.

Therefore, in a C + + language, the shift operation should not exceed the bounds, otherwise, the result is not predictable.

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

The following is an explanation of how many times the SHL directive restricts shifts in the Intel documentation:

The destination operand can be a register or a memory location. The count operand can be a 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.

1. Mask

is a string of 2 binary to the Target Field bitwise AND operation, shielding the current input bits.

The source code and mask are logically computed to derive the new operand. A logical operation, such as an OR operation, is used. and operation. Used to change the uppercase letter in ASCLL code to lowercase letters.

2. With or XOR or convert to complement operation

3. Usage: Mask (&)

4. Usage: Open bit (|)

5. Usage: Close bit (&~)

6. Usage: Transpose position (^)

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

8. Place value 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 not a 0 value

A/C + + provides a bitwise logical operator and a shift operator. The two can only be used for shaping and character types. Bitwise operators operate on each bit without affecting the left and right two bits, which differs from the regular operators (&&| | !) is the entire number of operations.

A Bitwise logical Operators

1. ~ Bitwise REVERSE

Change 1 to 0, change 0 to 1

EG:

~ (10011010)

(01100101)

Note:

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

The binary representation of 10 is 1010, the bitwise inverse should be 0101, that is, 5 of the decimal, why do you draw-11?

VC is a 32-bit compiler, so

10 = 00000000 00000000 00000000 00001010

~ = 11111111 11111111 11111111 11110101 = 11

Can be passed through a mask (bit and) with 15 bits and

15 = 00000000 00000000 00000000 00001111

~ = 00000000 00000000 00000000 00000101 = 11

2. & Bitwise Take with

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. | Bitwise-Take 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. ^ Bitwise XOR OR

Two operands differ by 1, same as 0

10 = 00000000 00000000 00000000 00001010

12 = 00000000 00000000 00000000 00001100

^

14 = 00000000 00000000 00000000 00000110

5. Usage: Mask

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

EG:

If only 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 intact. This is because and 0 bits or both are 0, and 1 bits or both are 1.

EG:

If you only open the second to third bit

107 = 0110 1011

6 = 0000 0110

|

111 = 0110 1111

7. Usage: Close bit

Close some bits

EG:

If you close the second to third bit

107 = 0110 1011

6 = 0000 0110

& ~

105 = 0110 1001

8. Usage: Transpose bit

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

EG:

If you transpose the second to third position

107 = 0110 1011

6 = 0000 0110

^

105 = 0110 1101

Two Shift Operators

    1. << left Shift

The left shift operator is to move each bit of the operand's value to the left, the number of bits moved to the right of the operand, and the number of digits to the right is filled with 0.

EG:

If you transpose the second to third position

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 1100

    1. >> Right Shift

The right-shift operator is to move each bit of the operand's value to the right, the number of bits to be shifted to the right operand, and the number of bits left to fill with 0.

EG:

If you transpose the second to third position

107 = 0110 1011 >>2

>>

26 = 0001 1010

One, 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 the corresponding bits in EXP1 and EXP2 are 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 not the same, the corresponding bit in result is 1, otherwise 0.

~ Invert operator: Reverses all bits in the bit container, 1 becomes 0, and 0 becomes 1.

<< bitwise left shift operator: Exp<<n, moves all the bits in the container to the left n bits, and the vacated bits are filled with 0.

>> bitwise Right SHIFT operator: Exp>>n, shifts all the bits in the container to the right n bits, and the vacated bits are filled with 0.

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

3. Common operation

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

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

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

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

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

(c) Reverses the value of the 27th bit.

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

Ii. bitset Containers in C + +

1. header file:

#include <bitset>

2. Declare a container:

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

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

Bitdet<int> bits (string&)

Summary: Bitset The type parameter 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 by an int or a string& value.

Basic usage of 3.bitset:

Operation

Function

Usage

Test (POS)

is the POS bit 1?

A.test (4)

Any ()

Is any bit 1?

A.any ()

None ()

Is there a bit of 1?

A.none ()

Count ()

Decimal value is a bit of 1

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 positions 1

A.set ()

Set (POS)

Place POS Position 1

A.set (4)

Reset ()

Place all positions 0

A.reset ()

Reset (POS)

Place POS Position 0

A.reset (4)

4.bitset vs. traditional C-bit manipulation and string conversion

You can export the container to a string string through the to_string () member, or you can export the container to a traditional C-style bit container with the To_long () member. Such as:

unsigned long bits = Bits.to_long ();

Sting str (bits.to_string ());

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

C + + Shift operators

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.