Name
Shr Keyword
Syntax
Value
shrBits
Description
The shr
operator performs a right shift of an
integer Value
by
Bits
bit positions. Vacated bits are
filled in on the left with zero bits.
The Bits
operand is interpreted as an
unsigned integer modulo the size of the
Value
. That is, if
Value
is a LongInt
,
Bits
is interpreted modulo 32, and if
Value
has type Int64
,
Bits
is interpreted modulo 64. In other
words, only the least significant few bits of
Bits
are used to determine the shift
amount.
Tips and Tricks
Remember that the size of the
Integer
andCardinal
types can change with new versions of Delphi. Use the fixed-size types if you need a specific number of bits, such as 32 forLongInt
andLongWord
.Do not use the
shr
operator instead of dividing by a power of two. If you mean division, you should write division. Theshr
operator does not check for range errors and does not propagate the sign bit. Let Delphi’s compiler decide the best instruction to use.
Example
// Measure the size of an integer, which can vary between versions.
function BitsPerInteger: Integer;
var
Test: Integer;
begin
Test := -1;
Result := 0;
while Test <> 0 do
begin
Test := Test shr 1;
Inc(Result);
end;
end;
See Also
And Keyword, Not Keyword, Or Keyword, Shl Keyword, Xor Keyword |
Get Delphi in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.