bitwise-rotation
Rotate values with a bitwise rotation.
In computer programming, a circular shift (or bitwise rotation) is a shift operator that shifts all bits of its operand. Unlike an arithmetic shift, a circular shift does not preserve a number's sign bit or distinguish a number's exponent from its significand (sometimes referred to as the mantissa). — Wikipedia
How to Use
; // Set the bit-width/length; max of `32`, min of `0`.const rotationObject = ; // Returns an object with rotate right (`ror`) and rotate left (`rol`) methods.const ror rol = rotationObject; // Rotate a value by an amount.; // 149; // 89
Visual Example
; const value = 1;value; // 00000001; // 00000010; // 00000100; // 00001000; // 00010000; // 00010000; // 00100000; // 01000000; // 10000000
API
bitwiseRotation(bitWidth)
(default
)
A function to specify a custom bitWidth that returns a rotationObject
.
bitWidth
: An integer between [0, 32] that specifies the limit for rotating values.
bitWidth
cannot be larger than 32 because operands of all bitwise operators are converted to signed 32-bit integers in two's complement format. — MDN
rotationObject
An object with the following 2 methods for rotating bytes:
-
ror(value, rotation)
: Rotate a byte to the right by the specified amount of rotation.-
value
: Decimal representation of a value to be rotated. -
rotation
: The amount to rotate thevalue
by.
-
-
rol(value, rotation)
: Rotate a byte to the left by the specified amount of rotation.-
value
: Decimal representation of a byte to be rotated. -
rotation
: The amount to rotate thevalue
by.
-
Both methods return a decimal representation of a value rotated.
rorInt8(value, rotation)
Convenience method for rotating an 8-bit value right.
rolInt8(value, rotation)
Convenience method for rotating an 8-bit value left.
rorInt16(value, rotation)
Convenience method for rotating a 16-bit value right.
rolInt16(value, rotation)
Convenience method for rotating a 16-bit value left.
rorInt32(value, rotation)
Convenience method for rotating a 32-bit value right.
rolInt32(value, rotation)
Convenience method for rotating a 32-bit value left.