Difference between revisions of "Shl"

From Free Pascal wiki
Jump to navigationJump to search
(Use wiki standard heading level: start with level 2. Added links to see also)
(language)
Line 3: Line 3:
 
<br>
 
<br>
 
== Overview ==
 
== Overview ==
'''Sh'''ift '''l'''eft ('''shl''') performs a left bit-shift operation (opposite than [[Shr|shr]]).
+
'''Sh'''ift '''l'''eft ('''shl''') performs a left bit-shift operation (opposite of [[Shr|shr]]).
  
 
E.g
 
E.g
  Command is:  00000100 shl 2
+
  Command is:  00000100 shl 2
 
    
 
    
  Action is:  00000100 <- 00
+
  Action is:  00000100 <- 00 (00 gets added to the right of the value)
 
    
 
    
 
  Result is:  00010000
 
  Result is:  00010000
Line 17: Line 17:
 
function ClearBit( const aValue, aBitNumber : integer ) : integer;
 
function ClearBit( const aValue, aBitNumber : integer ) : integer;
 
begin
 
begin
  result := aValue and not( 1 shl aBitNumber );
+
  result := aValue and not( 1 shl aBitNumber );
 
end;
 
end;
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 10:32, 15 October 2014

Template:shl

Overview

Shift left (shl) performs a left bit-shift operation (opposite of shr).

E.g

Command is:  00000100 shl 2
 
Action is:  00000100 <- 00 (00 gets added to the right of the value)
 
Result is:  00010000

Clear a bit

function ClearBit( const aValue, aBitNumber : integer ) : integer;
begin
  result := aValue and not( 1 shl aBitNumber );
end;

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

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

See also