Topic: DMD0350

Help File Version: 2.10.0.636

Math Shift Right Operator


The Math Shift Right operator ( >> ) causes the bits in left operand to be shifted to the right by the number of positions specified by the right operand. A Right Shift is a logical shift which means that the bits that are shifted off the end are discarded. The bit positions that have been vacated by the Right Shift operation are filled with the sign bit.

 

The number of bit positions to shift should be between 1 and the maximum number of bit positions in the left operand. Executing a Right Shift with a bit count higher than the number of bit positions in the left operand is not an error but will yield a result of 0 if the left operand is positive or a value of -1 if the left operand is negative. Executing a Right Shift by 0 bits has no effect.

 

Note: when using the Right Shift operations with Signed integer values be aware that bit 31 is the sign bit and the ON/OFF status of that bit will be maintained throughout the Right Shift process.

 

Note: executing a Right Shift with a negative value for the bit count is undefined; the result will be 0.

 

Unsigned Integers: assume V0 = 0x1234

 

V1 = V0 >> 5

 

V1 = 0x1234 >> 5

 

0000_0000_0000_0000_0001_0010_0011_0100 - initial bit pattern

0000_0000_0000_0000_0000_0000_1001_0001 - after 5th right shift

 

V1 = 0x91

 

One operand is an unsigned integer memory locations, the other is an integer constant.

 

The 16-bit integer is sign-extended to 32-bits when it is loaded into the math stack.

 

The lower 5 bits are shifted out. The remaining 11 bits are shifted into the lowest 11 bit positions. The upper 5 positions are filled with 0's.

 

The result is placed in an unsigned integer memory location.

 

 

Signed Integers: assume D0 = -12345678

 

D1 = D0 >> 5

 

D1 = -12345678 >> 5

 

1111_1111_0100_0011_1001_1110_1011_0010 - initial bit pattern

1111_1111_1010_0001_1100_1111_0101_1001 - after 1st right shift

1111_1111_1101_0000_1110_0111_1010_1100 - after 2nd right shift

1111_1111_1110_1000_0111_0011_1101_0110 - after 3rd right shift

1111_1111_1111_0100_0011_1001_1110_1011 - after 4th right shift

1111_1111_1111_1010_0001_1100_1111_0101 - after 5th right shift

 

D1 = -385803

 

One operand is an unsigned integer memory locations, the other is an integer constant.

 

The lower 5 bits are shifted out. The remaining 11 bits are shifted into the lowest 11 bit positions. The upper 5 positions are filled with 1's. The sign bit of the original value will be maintained.

 

The result is placed in an unsigned integer memory location.

 

N1 = N0 >> 5

 

N1 = -12345 >> 5

 

1111_1111_1111_1111_1100_1111_1100_0111 - initial bit pattern

1111_1111_1111_1111_1110_0111_1110_0011 - after 1st right shift

1111_1111_1111_1111_1111_0011_1111_0001 - after 2nd right shift

1111_1111_1111_1111_1111_1001_1111_1000 - after 3rd right shift

1111_1111_1111_1111_1111_1100_1111_1100 - after 4th right shift

1111_1111_1111_1111_1111_1110_0111_1110 - after 5th right shift

 

N1 = -386

 

One operand is an unsigned integer memory locations, the other is an integer constant.

 

The 16-bit integer is sign-extended to 32-bits when it is loaded into the math stack.

 

The lower 5 bits are shifted out. The remaining 11 bits are shifted into the lowest 11 bit positions. The upper 5 positions are filled with 1's. The sign bit of the original value will be maintained.

 

The result is placed in an unsigned integer memory location.

 

 


See Also:

Add

Subtract

Multiply

Divide

 

Modulus / Remainder

Raise to a Power

 

Less Than

Less than or Equal To

 

Equal To

Not Equal To

 

Greater Than

Greater Than or Equal To

 

Logical AND

Logical OR

 

Bit-wise AND

 

Bit-wise OR

Bit-wise XOR

 

Shift Left

Shift Right

Unsigned Shift Right

 

Negate

Bit-wise Invert

Logical NOT

 


Related Topics:

MATH - Calculate Expression