Description |
The Shr keyword performs a bitwise shift right of an Integer. The number is shifted Bits to the right.
If Bits is greater than the size of the number type, the Bits value is Mod'ed by the number type size (minimum 32) before the shift.
|
|
Notes |
Warning Only use Shr when a bit operation is required - do not use instead of a multiplication or division. First because it is unclear as to what is happening. Secondly, bits may be lost in the operation.
Warning The compiler will reject hardcoded shift right values that exceed 32, unless the data type is Int64. The same is not true for Shl.
|
|
Related commands |
Hi |
|
Returns the hi-order byte of a (2 byte) Integer |
Lo |
|
Returns the low-order byte of a (2 byte) Integer |
Shl |
|
Shift an integer value left by a number of bits |
|
|
|
Example code : Shifting left and losing high bits before shifting right |
// Full Unit code. // ----------------------------------------------------------- // You must store this code in a unit called Unit1 with a form // called Form1 that has an OnCreate event called FormCreate. unit Unit1; interface uses Forms, Dialogs; type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} // Include form definitions procedure TForm1.FormCreate(Sender: TObject); var
before, after : Word;
begin // Set up out starting number before := $3C; // Hex 3C = 003C in the Word
// Shift left by 12 will lose the top 12 bits of the Word
after := before Shl 12;
ShowMessageFmt('Before : %x',[before]);
ShowMessageFmt('After shift left : %x',[after]);
// Shifting right by 12 will not recover the lost data
after := after Shr 12;
ShowMessageFmt('After shift right : %x',[after]);
end; end.
|
Hide full unit code |
Before : 3C
After shift left : C000
After shift right : C
|
|