Shift an integer value left by a number of bits
  Number Shl bits
The Shl keyword performs a bitwise shift left of an Integer. The number is shifted Bits to the left.
If Bits is greater than the size of the number type, the Bits value is Mod'ed by the number type size before the shift.
For example;
  myByte : Byte;
  myByte := $2F;   // $2F = 47 decimal
  myByte := myByte Shl $24;

Gives the same result as Shl 4 = $F0.   
Warning Only use Shl 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.
Related commands
Hi Returns the hi-order byte of a (2 byte) Integer
Lo Returns the low-order byte of a (2 byte) Integer
Shr Shift an integer value right by a number of bits
 Author links

 Download this web site as a Windows program.

Example code : Shifting left and losing high bits before shifting back
  before, after : Word;
  // 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]);
Show full unit code
   Before : 3C
   After shift left  : C000
   After shift right : C
'); document.write('
'); document.write('
Delphi Programming Neil Moffatt 2002 - 2020. All rights reserved.  |  Home Page