You are here: Function Reference > Alphabetical Listing > B > BitRotate

BitRotate

Use this function to return the result of a bit shift-and-rotate operation performed on a numeric value.

Syntax

BitRotate(value1, shiftAmt)

The first parameter specifies the numeric value on which the bitwise shift-and-rotate operation is performed. The second parameter specifies the number of bit positions to shift. If either parameter is not an integer, it will be converted to an integer before the bitwise shift-and-rotate operation is performed.

This is a shift-and-rotate operation. This means that bits shifted off the end of a value are rotated back onto the value at the other end. In other words, the bits rotate in what might be thought of as a circular pattern — thus no bits are ever lost.

Note See the BitShift function for logical shift operations that do not shift-and-rotate.

A positive shiftAmt value causes the bit pattern in value1 to shift-and-rotate left the number of bits specified by shiftAmt. Bits that rotate off the left (high) end of the value return on the right (low) end.

A negative shiftAmt value causes the bit pattern in value1 to shift-and-rotate right the number of bits specified by shiftAmt. Bits that rotate off the right (low) end of the value return on the left (high) end. Note that integer values have 32 bits.

The following table shows the result of a bitwise shift-and-rotate operation:

 

Shift

Result value bits

6 (0110)

1

12 (1100)

6 (0110)

2

24 (0001 1000)

6 (0110)

3

48 (0011 0000)

6 (0110)

4

96 (0110 0000)

6 (0110)

-1

3 (0011)

6 (0110)

-2

-2147483647 (1000 0000 0000 0000 0000 0000 0000 0001)

6 (0110)

-3

-1073741824 (1100 0000 0000 0000 0000 0000 0000 0000)

6 (0110)

-4

1610612736 (0110 0000 0000 0000 0000 0000 0000 0000)

Example

Here is an example:

z = BitRotate(6,-8)

z = 100663296 (0000 0110 0000 0000 0000 0000 0000 0000)

See also