Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Coding Contest - Rotating Bits

in vb.net
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 Module
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
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 Function
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
my version only works with unassigned 16bit integer values. what it does:

-convert the integer to binary
-gets the last N amount of bits from right and puts to left or
gets the last N amount of bits from left and puts to right Ni times
-converts back to uinteger
omg i know that 00000001=1 and 10000000=128 -_-
32bit 16 8 , etc. im saying what the function i wrote uses a different method cuz i dont find any other way of moving the last bits of a uint value in vbnet without converting to binary first. i know is possible to with some other operation but since i already have a binary function(that i wrote) i use it to save time

so lets say that i mean is
"Translate" the integer to Binary ^^
(Jun 3, 2009 05:47 PM)g4143 Wrote:  maybe after a few more years with low/mid-level languages I'll be comfortable enough to make the step to a higher level language..G4143


lol. This is the most backwards statement ever. However, I think more people should think like you. Learn how the computer works with low level programming such as C and ASM, and then move up to the higher level languages once you know how the functions are actually working. This probably makes your code more efficient and powerful. I wish I took the same programming route as you.
(Jun 3, 2009 09:54 PM)g4143 Wrote:  One thing a lot of people do is assume that since you can program you understand how computers works. Programming is just a step, learning operating systems is another and hardware another, networking - internet - graphics - etc after enough steps your a competent hacker....G4143

very true, very true. Programming is indeed a very large step, but overall the bigger picture is the most important. You're a pretty wise guy g4143


Possibly Related Threads...
Thread / Author Replies Views Last Post
by thegeistJun 19, 2020 05:33 AM 3 153 Jun 19, 2020 05:33 AM
Last Post: thegeist
by hdev0Sep 1, 2019 02:57 PM 0 94 Sep 1, 2019 02:57 PM
Last Post: hdev0
[Help] by Mirage_88Jan 3, 2016 12:04 PM 2 103 Jan 3, 2016 12:04 PM
Last Post: FSBN