Difference between revisions of "Shr"

From Free Pascal wiki
Jump to navigationJump to search
Line 3: Line 3:
 
<br>
 
<br>
 
<br>
 
<br>
'''Sh'''ift '''r'''ight (shr) performs a right bit-shift operation (opposite than [[Shl|shl]]).
+
'''Sh'''ift '''r'''ight (shr) performs a logical right bit-shift operation (opposite than [[Shl|shl]]).
  
 +
== Shr with signed types ==
 +
Note: unlike the >> operator in the C language, the shr operator is a logical (not arithmetic) bit shift, even if the left operand is a signed integer. An implicit typecast and extension to a larger unsigned type may be performed before the shift operation. Check what the following program actually prints.
 +
 +
<syntaxhighlight>
 +
program ShrTest;
 +
begin
 +
  WriteLn(ShortInt(-3) shr 1);
 +
end.
 +
</syntaxhighlight>
  
 
== Is a bit set ==
 
== Is a bit set ==

Revision as of 12:39, 4 June 2014

Overview

Template:shr

Shift right (shr) performs a logical right bit-shift operation (opposite than shl).

Shr with signed types

Note: unlike the >> operator in the C language, the shr operator is a logical (not arithmetic) bit shift, even if the left operand is a signed integer. An implicit typecast and extension to a larger unsigned type may be performed before the shift operation. Check what the following program actually prints.

program ShrTest;
begin
  WriteLn(ShortInt(-3) shr 1);
end.

Is a bit set

function isBitSet(AValue, ABitNumber:integer):boolean;
begin
   result:=odd(AValue shr ABitNumber);
end;

See also