ROL: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
No edit summary
(Initial version)
Line 1: Line 1:
{{DISPLAYTITLE:_ROL}}
{{DISPLAYTITLE:_ROL}}
The [[_SHL]] function is used to shift the bits of a numerical value to the left.
The [[_ROL]] function is used to rotate the bits of a numerical value to the left. A rotation (or circular shift) is an operation similar to shift ([[_SHL]] and [[_SHR]]) except that the bits that fall off at one end are put back to the other end.  




{{PageSyntax}}
{{PageSyntax}}
:{{Parameter|result}} = [[_SHL]]({{Parameter|numericalVariable}}, {{Parameter|numericalValue}})
:{{Parameter|result}} = [[_ROL]]({{Parameter|numericalVariable}}, {{Parameter|numericalValue}})




{{Parameters}}
{{Parameters}}
* {{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: [[_BYTE]], [[INTEGER]], [[LONG]], or [[_INTEGER64]].
* Integer values can be signed or [[_UNSIGNED]].
* Integer values can be signed or [[_UNSIGNED]].
* {{Parameter|numericalValue}} is the number of places to shift the bits.
* {{Parameter|numericalValue}} is the number of places to rotate the bits.
* While 0 is a valid value it will have no affect on the variable being shifted.
* While 0 is a valid value it will have no affect on the variable being rotated.




{{PageDescription}}
{{PageDescription}}
* Allows for multiplication of a value by 2 faster than normal multiplication (see example 2 below).
* In left rotation, the bits that fall off at left end are put back at right end.
* Bits that reach the end of a variable's bit count are dropped (when using a variable of the same type - otherwise they will carry over).
* 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.




==Availability==
==Availability==
* '''Version 1.3 and up'''.
* '''Version 3.1.0 and up'''.




{{PageExamples}}
{{PageExamples}}
''Example 1:''
{{CodeStart}}
{{CodeStart}}A~%% = 1 'set right most bit of an{{Cl|_UNSIGNED}} {{Cl|_BYTE}}
{{Cl|OPTION _EXPLICIT}}
{{Cl|PRINT}} A~%%
 
{{Cl|PRINT}} {{Cl|_SHL}}(A~%%,7)
{{Cl|DIM}} a {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|_BYTE}}
B~%% = {{Cl|_SHL}}(A~%%,8) 'shift the bit off the left 'edge'
{{Cl|DIM}} b {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|INTEGER}}
{{Cl|PRINT}} B~%%
{{Cl|DIM}} c {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|LONG}}
{{CodeEnd}}
{{Cl|DIM}} d {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|_INTEGER64}}
{{OutputStart}}
 
1
a = &B11110000
128
b = &B1111111100000000
0
c = &B11111111111111110000000000000000
{{OutputEnd}}
d = &B1111111111111111111111111111111100000000000000000000000000000000
 
{{Cl|DO}}
    a = {{Cl|_ROL}}(a, 1)
    b = {{Cl|_ROL}}(b, 1)
    c = {{Cl|_ROL}}(c, 1)
    d = {{Cl|_ROL}}(d, 1)


    {{Cl|LOCATE}} 1, 1: {{Cl|PRINT}} {{Cl|RIGHT$}}({{Cl|STRING$}}(8, "0") + {{Cl|_BIN$}}(a), 8);
    {{Cl|LOCATE}} 2, 1: {{Cl|PRINT}} {{Cl|RIGHT$}}({{Cl|STRING$}}(16, "0") + {{Cl|_BIN$}}(b), 16);
    {{Cl|LOCATE}} 3, 1: {{Cl|PRINT}} {{Cl|RIGHT$}}({{Cl|STRING$}}(32, "0") + {{Cl|_BIN$}}(c), 32);
    {{Cl|LOCATE}} 4, 1: {{Cl|PRINT}} {{Cl|RIGHT$}}({{Cl|STRING$}}(64, "0") + {{Cl|_BIN$}}(d), 64);


''Example 2:''
    {{Cl|_LIMIT}} 15
{{CodeStart}}
{{Cl|LOOP}} {{Cl|WHILE}} {{Cl|_KEYHIT}} <> 27
A~%% = 1
{{Cl|FOR}} I%% = 0 {{Cl|TO}} 8
    {{Cl|PRINT}} {{Cl|_SHL}}(A~%%, I%%)
{{Cl|FOR...NEXT|NEXT}} I%%
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}
 
  1
  2
  4
  8
  16
  32
  64
128
256
{{OutputEnd}}
* Note: When directly [[PRINT]]ing to screen, the result is calculated internally using a larger variable type so the left most bit is carried to the next value.
** To avoid this store the result in a variable of the same type before printing.


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




{{PageNavigation}}
{{PageNavigation}}

Revision as of 20:47, 3 September 2022

The _ROL function is used to rotate the bits of a numerical value to the left. A rotation (or circular shift) is an operation similar to shift (_SHL and _SHR) except that the bits that fall off at one end are put back to the other end.


Syntax

result = _ROL(numericalVariable, numericalValue)


Template:Parameters

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


Description

  • In left rotation, the bits that fall off at left end are put back at right end.
  • The type of variable used to store the results should match the type of the variable being shifted.


Availability

  • Version 3.1.0 and up.


Examples

OPTION _EXPLICIT

DIM a AS _UNSIGNED _BYTE
DIM b AS _UNSIGNED INTEGER
DIM c AS _UNSIGNED LONG
DIM d AS _UNSIGNED _INTEGER64

a = &B11110000
b = &B1111111100000000
c = &B11111111111111110000000000000000
d = &B1111111111111111111111111111111100000000000000000000000000000000

DO
    a = _ROL(a, 1)
    b = _ROL(b, 1)
    c = _ROL(c, 1)
    d = _ROL(d, 1)

    LOCATE 1, 1: PRINT RIGHT$(STRING$(8, "0") + _BIN$(a), 8);
    LOCATE 2, 1: PRINT RIGHT$(STRING$(16, "0") + _BIN$(b), 16);
    LOCATE 3, 1: PRINT RIGHT$(STRING$(32, "0") + _BIN$(c), 32);
    LOCATE 4, 1: PRINT RIGHT$(STRING$(64, "0") + _BIN$(d), 64);

    _LIMIT 15
LOOP WHILE _KEYHIT <> 27


See also



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