## Bitwise rotate left function

I am trying to implement a rotate left function that rotates an integer x left by n bits

- Ex: rotateLeft(0x87654321,4) = 0x76543218
- Legal ops: ~ & ^ | + << >>

so far I have this:

int rotateLeft(int x, int n) { return ((x << n) | (x >> (32 - n))); }

which I have realized to not work for signed integers..does anyone have any ideas as how to fix this?

so now I tried:

int rotateLeft(int x, int n) { return ((x << n) | ((x >> (32 + (~n + 1))) & 0x0f)); }

and receive the error:

ERROR: Test rotateLeft(-2147483648[0x80000000],1[0x1]) failed... ...Gives 15[0xf]. Should be 1[0x1]

Current best practice for compiler-friendly rotates is this community-wiki Q&A. The code from wikipedia doesn't produce very good asm with clang, or gcc older than 5.1.

There's a very good, detailed explanation of bit rotation a.k.a. circular shift on Wikipedia.

Quoting from there:

unsigned int _rotl(const unsigned int value, int shift) { if ((shift &= sizeof(value)*8 - 1) == 0) return value; return (value << shift) | (value >> (sizeof(value)*8 - shift)); } unsigned int _rotr(const unsigned int value, int shift) { if ((shift &= sizeof(value)*8 - 1) == 0) return value; return (value >> shift) | (value << (sizeof(value)*8 - shift));

In your case, since you don't have access to the multiplication operator, you can replace `*8`

with `<< 3`

.

**EDIT** You can also remove the `if`

statements given your statement that you cannot use `if`

. That is an optimization, but you still get the correct value without it.

Note that, if you really intend to rotate bits on a `signed`

integer, the interpretation of the rotated result will be platform dependent. Specifically, it will depend on whether the platform uses Two's Complement or One's Complement. I can't think of an application where it is meaningful to rotate the bits of a signed integer.

**Rotate bits of a number,** Current best practice for compiler-friendly rotates is this community-wiki Q&A. The code from wikipedia doesn't produce very good asm with� In reality, what actually happens is that the decimal number is converted to a binary number internally by the processor and then manipulation takes place on a bit level. In bit rotation, the bits are shifted to the direction specified. Bit rotation is of two types: Left Bitwise Rotation: In this scenario, the bits are shifted to the left.

int rotateLeft(int x, int n) { return (x << n) | (x >> (32 - n)) & ~((-1 >> n) << n); }

UPDATE:(thanks a lot @George)

int rotateLeft(int x, int n) { return (x << n) | (x >> (32 - n)) & ~(-1 << n); }

not use '-' version.

int rotateLeft(int x, int n) { return (x << n) | (x >> (0x1F & (32 + ~n + 1))) & ~(0xFFFFFFFF << n); } //test program int main(void){ printf("%x\n",rotateLeft(0x87654321,4)); printf("%x\n",rotateLeft(0x87654321,8)); printf("%x\n",rotateLeft(0x80000000,1)); printf("%x\n",rotateLeft(0x78123456,4)); printf("%x\n",rotateLeft(0xFFFFFFFF,4)); return 0; } /* result : GCC 4.4.3 and Microsoft(R) 32-bit C 16.00.40219.01 76543218 65432187 1 81234567 ffffffff */

**Bitwise rotate left function,** In bit rotation, the bits are shifted to the direction specified. Bit rotation is of two types: Left Bitwise Rotation: In this scenario, the bits are shifted to the left. Right Bitwise Rotation:In this scenario, the bits are shifted to the right. In this operation, sometimes called rotate no carry, the bits are "rotated" as if the left and right ends of the register were joined. The value that is shifted into the right during a left-shift is whatever value was shifted out on the left, and vice versa for a right-shift operation.

The best way is:

int rotateLeft(int x, int n) { _asm { mov eax, dword ptr [x] mov ecx, dword ptr [n] rol eax, cl } }

If you need to rotate an `int`

variable *right in your code*, then **the fastest way is:**

#define rotl( val, shift ) _asm mov eax, dword ptr[val] _asm mov ecx, dword ptr [shift] _asm rol eax, cl _asm mov dword ptr [val], eax

`val`

is the value you rotate, `shift`

is the length of the rotation.

**Left and Right Bit Rotation using Bitwise Operators,** Another form of shift is the circular shift, bitwise rotation or bit rotation. Rotate[edit] . Left circular shift or rotate. Left rotate through carry Right rotate through carry Rotate through carry is similar to the rotate no carry operation, but the two ends of the register are separated by the carry flag.

Can you define 'rotate left' for a `signed int`

?

I would simply cast `x`

to an `unsigned int`

and perform the rotation the way you have it right now.

On another note: does your code need to work on different architectures (not just 32-bit)? You may want to avoid hardcoding the `int`

bitsize.

**Circular shift,** example. c = bitrol( a , k ) returns the value of the fixed-point fi object, a , rotated left by k bits. bitrol rotates bits from the most significant bit (MSB) side into the� Logic to left rotate bits in a number. Left rotation of bits in C is supported using bitwise left shift operator <<. But left shift operator drops Most Significant Bit (MSB) on each shift. Which is what we don't want. Instead of dropping MSB on each rotation, Least Significant Bit (LSB) should get replaced as dropped MSB. Step by step descriptive logic to left rotate bits of a number.

In ISO C (no idea if this is your implementation language or not, but it sure looks like it), shift-right on a signed integer that’s negative is implementation-defined and should thus be avoided.

If you’re doing bitops anyway, you *really* should cast to unsigned integers first.

**Bitwise operation,** For example consider the problem of separating out the RGB values Similarly, if you want a left rotate of n bits of a 32-bit value then use: Last Updated: 28-01-2019. Bit Rotation: A rotation (or circular shift) is an operation similar to shift except that the bits that fall off at one end are put back to the other end. In left rotation, the bits that fall off at left end are put back at right end. In right rotation, the bits that fall off at right end are put back at left end.

**Bitwise rotate left - MATLAB bitrol,** Left and right bit shifting. Bit shifting means moving the position of the bits in the left direction or in the right direction. For example, if we write in binary form� For the three-bit rotate left (rol) shown above, the three left-most bits are shifted off the left side of the data word, but immediately appear as the three right-most bits, still in the same sequence, left -to-right. All the other bits in the word are simply shifted left three places, just as in a shift left (sll) instruction.

**Fundamental C - Shifts And Rotates,** Contribute to dfrankland/bitwise-rotation development by creating an account on GitHub. bitwiseRotation(8); // Returns an object with rotate right (`ror`) and rotate left A function to specify a custom bitWidth that returns a rotationObject . BTW, prefer rotate-right for variable-count rotates, to avoid making the compiler do 32-n to implement a left rotate on architectures like ARM and MIPS that only provide a rotate-right. (This optimizes away with compile-time-constant counts.)

**Bit shift and bit rotation algorithms with Scilab implementation – x ,** The << operator shifts its left-hand operand left by the number of bits defined by its right-hand operand. The left-shift operation discards the high-order bits that are outside the range of the result type and sets the low-order empty bit positions to zero, as the following example shows:

##### Comments

- Think about why your expression doesn't work for signed integers and what you could do to the part to the right of the or-operator to make it work. Also, note that
`-`

is not part of your legal operator list, so you need to fix that as well. - ok so I think i figured out how to get rid of the - by (32 + (~n + 1)) but I am having trouble figuring out what I can do after the | to make this work
- can you create a mask to & the extraneous 1 bits away?
- yes, but i have no c knowledge prior to this so I am not familiar with bitwise operators and i do not know how to use a mask
- @shaynie Work through this tutorial to understand bitwise operators: cprogramming.com/tutorial/bitwise_operators.html Then this problem will be much easier.
- i understand what bit rotation is and how it works but I have to program it with just the bitwise operations listed above
- Is there anything in the listed implementation that you do not have access to? Note that you can replace
`- 1`

with`+ -1`

and`*8`

with`<<3`

. - I am not aloud to use sizeOf or to call any other functions and for reference, we are to assume our 32 bit integers use two's complement
- In that case you can just do the math and replace
`sizeof(value)*8-1`

with`4*8-1`

=`31`

and`sizeof(value)*8`

with`32`

. - As a side note; I would have replaced the
`8`

with`CHAR_BIT`

from`limits.h`

. - This question is tagged as homework. Let's avoid giving explicit answers. There's much more benefit to the OP if he/she learns how to answer this question him/herself.
- Well, I will not That's right, that she(he) 's does not know how making mask?
- I'm not sure what you're asking. She doesn't know what a mask is or how it works, so she needs to learn that first. In any case, in your answer
`(-1 >> n)`

has no effect -- do you see why? It's better to simply use ~0. - She can't use
`-`

op, so you should use`~0`

. Your answer also needs an extra pair of parentheses.