Sign in Join
Wiki copytemporary

Revision 2 of 2, by AndreasFilsinger on 8-29-2021. The page as it is now.

Shl

Back to Reserved words.

Overview

The reserved word Shift left (shl) performs a left bit-shift operation, shifting the value by the amount of bits specified as an argument (opposite of shr).

Example:

text
Command is: 00000100 shl 2 (shift left 2 bits)
 
Action is:  00000100 <- 00 (00 gets added to the right of the value; left 00 "disappears")
 
Result is:  00010000

Clear a bit

pascal
function ClearBit( const aValue, aBitNumber : integer ) : integer;
begin
// sanity check supplied value
  if (aBitNumber <0) or  (aBitNumber >15) then
      result :=0
  else
// value ok
     result := aValue and not( 1 shl aBitNumber );
end;

If you call ClearBit(%1111,1), then you get %1101 (The binary number %1111 is 15 and %1101 = 13).

If you call ClearBit(13,2), then you get 9 (9 = %1001).

In this case, bits are numbered right to left from 0 to 15, bit 0 being the ones bit and bit 15 being the sign bit.


From the Free Pascal wiki, page "Shl", as it stood on 2021-08-29 after a change by AndreasFilsinger, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.