Difference between revisions of "Shr"

From Lazarus wiki
Jump to navigationJump to search
(Undo revision 146631 by Mozi (talk). This is a program, neither a function nor procedure. I'm sorry.)
Tag: Undo
 
(12 intermediate revisions by 9 users not shown)
Line 1: Line 1:
'''Sh'''ift '''r'''ight (shr) performs a right bit-shift operation (opposite than [[Shl|shl]]).
+
{{Shr}}
  
  
 +
Back to [[Reserved words]].
 +
 +
 +
== Overview ==
 +
 +
The reserved word '''Sh'''ift '''r'''ight (shr) performs a logical right bit-shift operation (opposite than [[Shl|shl]]).
 +
 +
Example:
 +
 +
Command is: 00000100 shr 2 (shift right 2 bits)
 +
 
 +
Action is:  00000100 -> 00 (00 gets added to the left of the value; right 00 "disappears")
 +
 
 +
Result is:  00000001
 +
 +
== 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|typecast]] and extension to a larger unsigned type may be performed before the shift operation. Check what the following program actually prints.
 +
 +
<syntaxhighlight lang=pascal>
 +
program ShrTest;
 +
begin
 +
  WriteLn(ShortInt(-3) shr 1);
 +
end.
 +
</syntaxhighlight>
  
 
== Is a bit set ==
 
== Is a bit set ==
  
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
function isBitSet(AValue, ABitNumber:integer):boolean;
 
function isBitSet(AValue, ABitNumber:integer):boolean;
 
begin
 
begin
Line 12: Line 36:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== Read more ===
+
{{Logical operators}}
* [[Odd]]
 
* [[Boolean]]
 
 
* [[Const]]
 
* [[Const]]
 
* [[Function]]
 
* [[Function]]
 
* [[Integer]]
 
* [[Integer]]
 
[[Category:Pascal]]
 

Latest revision as of 04:30, 1 September 2021

Deutsch (de) English (en) français (fr) русский (ru)


Back to Reserved words.


Overview

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

Example:

Command is: 00000100 shr 2 (shift right 2 bits)
 
Action is:  00000100 -> 00 (00 gets added to the left of the value; right 00 "disappears")
 
Result is:  00000001

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;


navigation bar: Pascal logical operators
operators

and • or • not • xor
shl • shr
and_then (N/A)• or_else (N/A)

see also

{$boolEval} • Reference: § “boolean operators” • Reference: § “logical operators”