SHR: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(Created page with "{{DISPLAYTITLE:_SHR}} The _SHR function is used to shift the bits of a numerical value to the right. {{PageSyntax}} :{{Parameter|result}} = _SHR({{Parameter|numericalVariable}}, {{Parameter|numericalValue}}) {{Parameters}} * {{Parameter|numericalVariable}} is the variable to shift the bits of and can be of the following types: INTEGER, LONG, _INTEGER64, or _BYTE. * Integer values can be signed or _UNSIGNED. * {{Parameter|numericalValue}} t...")
 
No edit summary
 
(5 intermediate revisions by 2 users not shown)
Line 7: Line 7:




{{Parameters}}
{{PageParameters}}
* {{Parameter|numericalVariable}} is the variable to shift the bits of and can be of the following types: [[INTEGER]], [[LONG]], [[_INTEGER64]], or [[_BYTE]].
* {{Parameter|numericalVariable}} is the variable to shift the bits of and can be of the following types: [[INTEGER]], [[LONG]], [[_INTEGER64]], or [[_BYTE]].
* Integer values can be signed or [[_UNSIGNED]].
* Integer values can be signed or [[_UNSIGNED]].
Line 18: Line 18:
* Bits that reach the end of a variables bit count are dropped.
* Bits that reach the end of a variables bit count are dropped.
* The type of variable used to store the results should match the type of the variable being shifted.
* The type of variable used to store the results should match the type of the variable being shifted.
* NOTE: When dealing with SIGNED variables, shifting the bits right will leave the sign bit set. This is due to how C++ deals with bit shifting under the hood.  
* NOTE: When dealing with SIGNED variables, shifting the bits right will leave the sign bit set. This is due to how C++ deals with bit shifting under the hood.


==Availability==
 
{{PageAvailability}}
* '''Version 1.3 and up'''.
* '''Version 1.3 and up'''.


Line 43: Line 44:
{{Cl|FOR}} I%% = 0 {{Cl|TO}} 8
{{Cl|FOR}} I%% = 0 {{Cl|TO}} 8
     {{Cl|PRINT}} {{Cl|_SHR}}(A~%%, I%%)
     {{Cl|PRINT}} {{Cl|_SHR}}(A~%%, I%%)
{{Cl|NEXT I%%}}
{{Cl|FOR...NEXT|NEXT}} I%%
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}
{{OutputStart}}
Line 56: Line 57:
   0
   0
{{OutputEnd}}
{{OutputEnd}}


{{PageSeeAlso}}
{{PageSeeAlso}}
* [[_SHL]], [[INTEGER]], [[LONG]]
* [[_SHL]], [[_ROL]], [[_ROR]]
* [[_BYTE]], [[_INTEGER64]]
* [[_BYTE]], [[INTEGER]]
* [[LONG]], [[_INTEGER64]]




{{PageNavigation}}
{{PageNavigation}}

Latest revision as of 01:07, 29 January 2023

The _SHR function is used to shift the bits of a numerical value to the right.


Syntax

result = _SHR(numericalVariable, numericalValue)


Parameters

  • numericalVariable is the variable to shift the bits of and can be of the following types: INTEGER, LONG, _INTEGER64, or _BYTE.
  • Integer values can be signed or _UNSIGNED.
  • numericalValue the number of places to shift the bits.
  • While 0 is a valid value it will have no affect on the variable being shifted.


Description

  • Allows for division of a value by 2 faster than normal division (see example 2 below).
  • Bits that reach the end of a variables bit count are dropped.
  • The type of variable used to store the results should match the type of the variable being shifted.
  • NOTE: When dealing with SIGNED variables, shifting the bits right will leave the sign bit set. This is due to how C++ deals with bit shifting under the hood.


Availability

  • Version 1.3 and up.


Examples

Example 1:

A~%% = 128 'set left most bit of an_UNSIGNED _BYTE
PRINT A~%%
PRINT _SHR(A~%%,7)
PRINT _SHR(A~%%,8) 'shift the bit off the right 'edge'
 128
 1
 0


Example 2:

A~%% = 128
FOR I%% = 0 TO 8
    PRINT _SHR(A~%%, I%%)
NEXT I%%
 128
  64
  32
  16
  8
  4
  2
  1
  0


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link