Results 1 to 7 of 7

Thread: Test Question

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344

    Red face

    A program will input any interger value between 0 and 15 then the algorithm is to convert that value to binary (base2). The number input and its equivalent binary value are to be printed out.

    Can anyone show an algorithm representing that?

    I know it is something like...lets say if they inputed 11 in the text field it has to be divded by 2, the answer will be 5.5.....then you have to put that 5 over 2 and get 2.2? and then bahhhhh im lost any help guys?
    -RaY
    VB .Net 2010 (Ultimate)

  2. #2
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715
    This function takes a byte and converts it to binary:
    Code:
    Function Getbits(Invoer As Byte) As String
        Dim Uitvoer As String
        
        If (Invoer And 128) = 128 Then
            Uitvoer = Uitvoer & 1
        Else
            Uitvoer = Uitvoer & 0
        End If
            
        If (Invoer And 64) = 64 Then
            Uitvoer = Uitvoer & 1
        Else
            Uitvoer = Uitvoer & 0
        End If
            
        If (Invoer And 32) = 32 Then
            Uitvoer = Uitvoer & 1
        Else
            Uitvoer = Uitvoer & 0
        End If
            
        If (Invoer And 16) = 16 Then
            Uitvoer = Uitvoer & 1
        Else
            Uitvoer = Uitvoer & 0
        End If
            
        If (Invoer And 8) = 8 Then
            Uitvoer = Uitvoer & 1
        Else
            Uitvoer = Uitvoer & 0
        End If
            
        If (Invoer And 4) = 4 Then
            Uitvoer = Uitvoer & 1
        Else
            Uitvoer = Uitvoer & 0
        End If
            
        If (Invoer And 2) = 2 Then
            Uitvoer = Uitvoer & 1
        Else
            Uitvoer = Uitvoer & 0
        End If
            
        If (Invoer And 1) = 1 Then
            Uitvoer = Uitvoer & 1
        Else
            Uitvoer = Uitvoer & 0
        End If
        
        Getbits = Uitvoer
        
    End Function
    You can use it like this:
    Code:
    Msgbox Getbits(CByte(Cint(Text1.Text)))
    Oetje
    [email protected]
    93606776
    Visual Basic 6, Windows 2000

    Never pet a burning dog

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344

    Sad enough

    He wants it to be the drawn out long way, anyone know how to do that?
    -RaY
    VB .Net 2010 (Ultimate)

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344

    and not # 0 - 15 not bytes

    The problem is he was showing it in q basic...and told us we can use any language we want to do it but cant use no short cuts have to show it step by step....I chose vb but need help i dont understand........if you dont know how to make the algorithm do you at least know the steps to convert the number? My had writing was so sloppy im lost.
    -RaY
    VB .Net 2010 (Ultimate)

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344

    I got something like this in notes

    More like psecode(SP)

    Input 11
    Divide 2
    Answer 5.5
    Int(5.5) = (cant read looks like Se maybe < 0)
    if (5.5 = 5) >0 then
    4th digit = 1
    else 4th digit = 0
    end if
    5
    - = 2.5 Int(2.5(Cant read looks like (2.5+2)
    2
    If 2.5 - 2 > other
    3rd digit = 1
    etc.
    -RaY
    VB .Net 2010 (Ultimate)

  6. #6
    Frenzied Member mlewis's Avatar
    Join Date
    Sep 2000
    Posts
    1,226
    Its been a while since I've done this but try:

    Code:
    Public Function GetBits(ByVal Number As Integer) As String
     Dim I As Integer
     Dim Buffer As String
    
     For I = 0 To 15
      If (Number Mod (2 ^ I)) = 0 Then
       Buffer = "0" & Buffer
      Else
       Buffer = "1" & Buffer
      End If
     Next I
    
     GetBits = Buffer
    End Function
    I don't know if thats exactly right but that should give you a good start. (I hope)
    M. Lewis
    Pi-Q Software
    How many mouse clicks does it take to cook breakfast?

    Blargh! I am dead!

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344

    Thanks

    I'll see what i can get out of it....do you know the steps by hand to convert a number into binary? This would help as well..... By the way Lewis....that download the first level link on your site doesnt seem to work?.......Thanks!
    -RaY
    VB .Net 2010 (Ultimate)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width