Difference between revisions of "Shl"

From Free Pascal wiki
Jump to navigationJump to search
(Categorization)
 
(13 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{shl}}
+
{{Shl}}
<br>
 
<br>
 
'''Sh'''ift '''l'''eft ('''shl''') performs a left bit-shift operation (opposite than [[Shr|shr]]).
 
  
E.g
+
 
  Command is: 00000100 shl 2
+
Back to [[Reserved words]].
 +
 
 +
== Overview ==
 +
 
 +
The reserved word '''Sh'''ift '''l'''eft ('''shl''') performs a left bit-shift operation, shifting the value by the amount of bits specified as an argument (opposite of [[Shr|shr]]).
 +
 
 +
Example:
 +
 
 +
  Command is: 00000100 shl 2 (shift left 2 bits)
 
    
 
    
  Action is:  00000100 <- 00
+
  Action is:  00000100 <- 00 (00 gets added to the right of the value; left 00 "disappears")
 
    
 
    
 
  Result is:  00010000
 
  Result is:  00010000
Line 13: Line 18:
 
== Clear a bit ==
 
== Clear a bit ==
  
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
function ClearBit( const aValue, aBitNumber : integer ) : integer;
 
function ClearBit( const aValue, aBitNumber : integer ) : integer;
 
begin
 
begin
  result := aValue and not( 1 shl aBitNumber );
+
// sanity check supplied value
 +
  if (aBitNumber <0) or  (aBitNumber >15) then
 +
      result :=0
 +
  else
 +
// value ok
 +
    result := aValue and not( 1 shl aBitNumber );
 
end;
 
end;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
If you call ClearBit(%1111,1) then get %1101 (%1111 = 15 and %1101 = 13). If you call ClearBit(13,2) then get 9 (9 = %1001) .
+
If you call ClearBit(%1111,1), then you get %1101 (The [[Binary_numeral_system|binary number]] %1111 is 15 and %1101 = 13).  
  
== See e.g ==
+
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.
  
 +
{{Logical operators}}
 
* [[Or# Set a bit|Set a bit]]
 
* [[Or# Set a bit|Set a bit]]
 
* [[Xor# Toggle a bit|Toggle a bit]]
 
* [[Xor# Toggle a bit|Toggle a bit]]
 +
* [[Bit manipulation]]
 +
* [[$Bitpacking]]
  
[[Category:Pascal]]
+
[[Category:Operators]]
 +
[[Category:Code]]

Latest revision as of 20:25, 29 August 2021

Deutsch (de) English (en) suomi (fi) français (fr) русский (ru)


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:

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

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.


navigation bar: Pascal logical operators
operators

and • or • not • xor
shl • shr
and_then (N/A)• or_else (N/A)

see also

{$boolEval} • Reference: § “boolean operators” • Reference: § “logical operators”