in vb.net
here , takes a uinteger, converts to binary, and rotates it to left or right.
Calls
getbinary(value as uinteger) as string
converts the uinteger value to a binary string
Rotate(ByVal Binary As String, ByVal N As Integer, ByVal dLeft As Boolean,nAmount as integer) As String
takes a binary string, rotate N times to left(is true) or right(if false) and returns the binary string wanna convert back to uinteger, namount is the reversed chunk size
[edited]
this function converts back to uinteger
binary is the binary string and returns the uinteger value
the runcode ill be
getUInteger(Rotate(GetBinary(121), 1, False, 2))
01111001
11100101
returns this
Code
Module Module1
Public Function GetBinary(ByVal S As UInteger) As String ' letter by letter
Dim r As UInteger
Dim i As Integer
r = S
For i = 0 To S
If r < 2 ^ i Then
Exit For
End If
r -= 2 ^ i
Next
Dim k As Long
k = 2 ^ (i + 1)
'this part gets the bit size of the number. its to make sure that you wont put useless loops
While k <> 0
If S >= k Then
S = S - k
GetBinary = GetBinary + "1"
Else
GetBinary = GetBinary + "0"
End If
k = k - 2 ^ i
i = i - 1
End While
'reads from top to down, if the number fits on the bit size, remove the number from the total and add a 1, else add a 0
For f As Integer = Len(GetBinary) To i + 1
GetBinary += "0"
Next
End Function
'if there is many 000 missed on the right side(thats why i made the first loop) only fill them.
Public Function getUInteger(ByVal Binary As String) As UInteger
For i As Integer = 1 To Binary.Length
If Mid(Binary, i, 1) = "1" Then
getUInteger += 2 ^ (Binary.Length - i)
End If
Next
End Function
'converts back to uinteger
Public Function Rotate(ByVal Binary As String, ByVal N As Integer, ByVal dLeft As Boolean, ByVal amountOfN As Integer) As String
Dim i As Integer
Dim buffer As String = Binary
If dLeft Then
While i < N
Binary = Right(Binary, amountOfN) + Left(Binary, Binary.Length - amountOfN)
i += 1
End While
Else
While i < N
Binary = Right(Binary, Binary.Length - amountOfN) + Left(Binary, amountOfN)
i += 1
End While
End If
Return Binary
End Function
Sub Main()
Console.WriteLine(getUInteger(Rotate(GetBinary(121), 1, False, 2)))
Console.ReadKey()
End Sub
End ModuleCalls
getbinary(value as uinteger) as string
converts the uinteger value to a binary string
Rotate(ByVal Binary As String, ByVal N As Integer, ByVal dLeft As Boolean,nAmount as integer) As String
takes a binary string, rotate N times to left(is true) or right(if false) and returns the binary string wanna convert back to uinteger, namount is the reversed chunk size
[edited]
this function converts back to uinteger
Code
Public Function getUInteger(ByVal Binary As String) As UInteger
For i As Integer = 1 To Binary.Length
If Mid(Binary, i, 1) = "1" Then
getUInteger += 2 ^ (Binary.Length - i)
End If
Next
End Functionthe runcode ill be
getUInteger(Rotate(GetBinary(121), 1, False, 2))
01111001
11100101
returns this
