Name
Shr Keyword
Syntax
ValueshrBits
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
IntegerandCardinaltypes can change with new versions of Delphi. Use the fixed-size types if you need a specific number of bits, such as 32 forLongIntandLongWord.Do not use the
shroperator instead of dividing by a power of two. If you mean division, you should write division. Theshroperator 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 |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access