title: Circular bit shift in Visual Basic
body: <p>I have coded a solution that I believe will work to make circular bit shifts in Visual Basic. However I'm new to the language, and I'm not 100% sure this is efficient or functional. Is there a better way of doing it?</p>
  
  <p>In case you're curious, I'm trying to implement the <a href="https://en.wikipedia.org/wiki/ARIA_(cipher)" rel="nofollow">ARIA cipher</a>, and I need this function to do so.</p>
  
  <pre><code>Private Function CircularRotationLeft(ByVal bytes As Byte(), ByVal times As Integer) As Byte()
      Dim carry As Boolean = False
      If times &lt; 0 Then
          Return Nothing
      End If
      While times &gt; bytes.Length * 8
          times -= bytes.Length * 8
      End While
      If times = 0 Then
          Return bytes
      End If
      Array.Reverse(bytes)
      For index As Integer = 1 To times
          For Each bits As Byte In bytes
              If bits &gt; 127 Then
                  bits -= 128
                  bits *= 2
                  If carry Then
                      bits += 1
                  End If
                  carry = True
              Else
                  bits *= 2
                  If carry Then
                      bits += 1
                  End If
                  carry = False
              End If
          Next
          If carry Then
              bytes(0) += 1
          End If
      Next
      Array.Reverse(bytes)
      Return bytes
  End Function
  
  Private Function CircularRotationRight(ByVal bytes As Byte(), ByVal times As Integer) As Byte()
      Dim carry As Boolean = False
      If times &lt; 0 Then
          Return Nothing
      End If
      While times &gt; bytes.Length * 8
          times -= bytes.Length * 8
      End While
      If times = 0 Then
          Return bytes
      End If
      Array.Reverse(bytes)
      For index As Integer = 1 To times
          For Each bits As Byte In bytes
              If bits Mod 2 = 0 Then
                  bits /= 2
                  If carry Then
                      bits += 128
                  End If
                  carry = False
              Else
                  bits /= 2
                  If carry Then
                      bits += 128
                  End If
                  carry = True
              End If
          Next
          If carry Then
              bytes(0) += 128
          End If
      Next
      Array.Reverse(bytes)
      Return bytes
  End Function
  </code></pre>