<p>I know that the following is true</p>

<pre><code>int i = 17; //binary 10001
int j = i &lt;&lt; 1; //decimal 34, binary 100010
</code></pre>

<p>But, if you shift too far, the bits fall off the end.  Where this happens is a matter of the size of integer you are working with.</p>

<p>Is there a way to perform a shift so that the bits rotate around to the other side?  I'm looking for a single operation, not a for loop.</p>

[{'answer_id': 35172, 'author': 'Chris Marasti-Georg', 'author_id': 96, 'author_profile': 'https://Stackoverflow.com/users/96', 'pm_score': 7, 'selected': True, 'text': '<p>If you know the size of type, you could do something like:</p>\n\n<pre><code>uint i = 17;\nuint j = i &lt;&lt; 1 | i &gt;&gt; 31;\n</code></pre>\n\n<p>... which would perform a circular shift of a 32 bit value.</p>\n\n<p>As a generalization to circular shift left n bits, on a b bit variable:</p>\n\n<pre><code>/*some unsigned numeric type*/ input = 17;\nvar result = input  &lt;&lt; n | input  &gt;&gt; (b - n);\n</code></pre>\n\n<p><hr/>\n@The comment, it appears that C# does treat the high bit of signed values differently.  I found some info on this <a href="http://www.blackwasp.co.uk/CSharpShiftOperators.aspx" rel="noreferrer">here</a>.  I also changed the example to use a uint.</p>\n'}, {'answer_id': 185743, 'author': 'jaircazarin-old-account', 'author_id': 20915, 'author_profile': 'https://Stackoverflow.com/users/20915', 'pm_score': 4, 'selected': False, 'text': "<p>One year ago I've to implement MD4 for my undergraduate thesis. Here it is my implementation of circular bit shift using a UInt32.</p>\n\n<pre><code>private UInt32 RotateLeft(UInt32 x, Byte n)\n{\n      return UInt32((x &lt;&lt; n) | (x &gt;&gt; (32 - n)));\n}\n</code></pre>\n"}, {'answer_id': 1042452, 'author': 'yeyeyerman', 'author_id': 110466, 'author_profile': 'https://Stackoverflow.com/users/110466', 'pm_score': 2, 'selected': False, 'text': '<p>Just as reference on how to do it, these two functions work perfectly for rotating the bits of 1/2word:</p>\n\n<pre><code>static public uint ShiftRight(uint z_value, int z_shift)\n{\n    return ((z_value &gt;&gt; z_shift) | (z_value &lt;&lt; (16 - z_shift))) &amp; 0x0000FFFF;\n}\n\nstatic public uint ShiftLeft(uint z_value, int z_shift)\n{\n    return ((z_value &lt;&lt; z_shift) | (z_value &gt;&gt; (16 - z_shift))) &amp; 0x0000FFFF;\n}\n</code></pre>\n\n<p>It would be easy to extend it for any given size.</p>\n'}, {'answer_id': 60161031, 'author': 'phuclv', 'author_id': 995714, 'author_profile': 'https://Stackoverflow.com/users/995714', 'pm_score': 3, 'selected': False, 'text': '<p>Sincce .NET Core 3.0 and up there\'s <a href="https://learn.microsoft.com/en-us/dotnet/api/system.numerics.bitoperations.rotateleft?view=netcore-3.1" rel="noreferrer"><code>BitOperations.RotateLeft()</code></a> and <a href="https://learn.microsoft.com/en-us/dotnet/api/system.numerics.bitoperations.rotateright?view=netcore-3.1" rel="noreferrer"><code>BitOperations.RotateRight()</code></a> so you can just use something like</p>\n\n<pre><code>BitOperations.RotateRight(12, 3);\nBitOperations.RotateLeft(34L, 5);\n</code></pre>\n\n<p>In previous versions you can use <a href="https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualstudio.utilities.bitrotator.rotateleft?view=visualstudiosdk-2015" rel="noreferrer"><code>BitRotator.RotateLeft()</code></a> and <a href="https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualstudio.utilities.bitrotator.rotateright?view=visualstudiosdk-2015" rel="noreferrer"><code>BitRotator.RotateRight()</code></a> in Microsoft.VisualStudio.Utilities</p>\n'}, {'answer_id': 66592889, 'author': 'Soleil', 'author_id': 1447389, 'author_profile': 'https://Stackoverflow.com/users/1447389', 'pm_score': 1, 'selected': False, 'text': '<p>The extension methods for rotating bits of a <code>uint</code> (32 bits):</p>\n<pre><code>public static uint ROR(this uint x, int nbitsShift)\n    =&gt; (x &gt;&gt; nbitsShift) | (x &lt;&lt; (32 - nbitsShift));\n\npublic static uint ROL(this uint x, int nbitsShift)\n    =&gt; (x &lt;&lt; nbitsShift) | (x &gt;&gt; (32 - nbitsShift));\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/35167', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2470/']