Difference between revisions of "Shl"

From Free Pascal wiki
Jump to navigationJump to search
(New page: '''Sh'''ift '''l'''eft (shl) performs a left bit-shift operation.)
 
Line 1: Line 1:
'''Sh'''ift '''l'''eft (shl) performs a left bit-shift operation.
+
'''Sh'''ift '''l'''eft ('''shl''') performs a left bit-shift operation (opposite than [[Shr|shr]]).
 +
 
 +
E.g
 +
Command is:  00000100  shl 2
 +
 
 +
Action is:  00000100 <- 00
 +
 
 +
Result is:  00010000
 +
 
 +
== Toggle a bit ==
 +
 
 +
<delphi>
 +
function ToggleBit(const AValue,ABitNumber:integer):integer;
 +
begin
 +
  result := AValue xor 1 shl ABitNumber;
 +
end;
 +
</delphi>
 +
 
 +
If you call ToggleBit(11,0) then get 10. If you call ToggleBit(10,2) then get 14.
 +
 
 +
=== Read more ===
 +
* [[Const]]
 +
* [[Integer]]
 +
* [[Xor]]

Revision as of 06:56, 24 October 2007

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

E.g

Command is:  00000100  shl 2
 
Action is:  00000100 <- 00
 
Result is:  00010000

Toggle a bit

<delphi> function ToggleBit(const AValue,ABitNumber:integer):integer; begin

  result := AValue xor 1 shl ABitNumber;

end; </delphi>

If you call ToggleBit(11,0) then get 10. If you call ToggleBit(10,2) then get 14.

Read more