C + + Shift operators

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

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

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

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. The following is a letter from the Netizen and my reply:

Hello! Operator << as an efficient operation in bit operations, but I encountered a problem: The following in the VC environment found a very ambiguous place, the following annotations.

#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 this:i=35;j=1<<i; is compiled into the following machine instructions in the case of VC not being optimized:

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 runs the SHL command, CL is first and then operated with 31 to limit the number of left shifts to less than or equal to 31. Since the & 31 = 3, this 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, the compiler will directly set the result to 0 if the VC compiler's optimizer switch is turned on (for example, compiled to release version number).

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

The following is a description of how the SHL directive limits the number of 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 the 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. How to use: Mask (&)

4. How to use: Open bits (|)

5. How to use: Close bit (&~)

6. How to use: Transpose a bit (^)

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 Assuming that 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

Ability to pass through masks (bits and) with 15-bit and

15 = 00000000 00000000 00000000 00001111

~ = 00000000 00000000 00000000 00000101 = 11

2. & Bitwise Take with

Only two operands are 1 results 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 a random 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. How to use: Masks

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 the second to third digit is displayed

107 = 0110 1011

6 = 0000 0110

&

2 = 0000 0010

6. How to use: open bits

The open bit is a special location that opens a value by | (bit or), keeping the other bits intact at the same time. This is due to the 0-bit or both 0, and 1-bit or both 1.

EG:

If you just open the second to third place

107 = 0110 1011

6 = 0000 0110

|

111 = 0110 1111

7. How to use: Turn off bits

Close some bits

EG:

If you close the second to third bit

107 = 0110 1011

6 = 0000 0110

& ~

105 = 0110 1001

8. How to use: Transpose a bit

Suppose that a one is 1 and transpose to 0, assuming that 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 vacated on the right side 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 bit 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 |=,&=,^= respectively |&^ the compound operator of three kinds of operators.

    3. Frequently used operations

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

    (a) Setting the 27th position to pass (set as 1) other bits unchanged:

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

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

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

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

    result^= (1<<27)//random 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 A type parameter in a template class passes the number of bits of a container, while a constructor parameter initializes the corresponding value in the container from right to left by an int or a string& value.

    Basic use of 3.bitset:

    Operation

    Function

    How to use

    Test (POS)

    is the POS bit 1?

    A.test (4)

    Any ()

    is the discretionary 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

    The ability to output a container to a string string via the to_string () member, and also to 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 ());



C + + Shift operators

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.