Results 1 to 7 of 7

Thread: HELP ME PLS!!!!!!!!!!!!!!!!!!!!!Student with problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2001
    Posts
    4

    HELP ME PLS!!!!!!!!!!!!!!!!!!!!!Student with problem

    how to write a 4 bit counter using visual basic. like when i press up, it will change from 0000 to 0001 then up again from 0001 to 0010 so on and so forth. thanks for helping me out its for my school assignment.i am stuck with this. thanks once again. pls email the sample code to [email protected]

  2. #2
    Junior Member
    Join Date
    Jun 2001
    Posts
    22
    s = Format(n, "0000")

    that should do it...

  3. #3
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    No that wont do it. If just makes sure the number returned is 4 digits long, it doesnt count binary.

    VB Code:
    1. s = format(n, "0000")
    just adds zeroes. Counting binary could be pretty tricky in VB. TRy using C or C++ if you know it...
    You just proved that sig advertisements work.

  4. #4
    Fanatic Member THEROB's Avatar
    Join Date
    Oct 2000
    Location
    I'm cold and there are wolves after me
    Posts
    575
    Here is some code to change decimal to binary:

    Code:
    Public Function CONV(Dec As Long) As String
    
    Dim i As Long, x As Long, bin As String
    Const maxpower = 32 ' Maximum number of binary digits supported.
    bin = "" 'Build the desired binary number in this string, bin.
    x = Dec
    
    If x > 2 ^ maxpower Then
    MsgBox "Number must be no larger than " & Str$(2 ^ maxpower)
    CONV = ""
    Exit Function
    End If
    
    ' Here is the heart of the conversion from decimal to binary:
    
    ' Negative numbers have "1" in the 32nd left-most digit:
    If x < 0 Then bin = bin + "1" Else bin = bin + "0"
    
    For i = maxpower To 0 Step -1
    If x And (2 ^ i) Then ' Use the logical "AND" operator.
    bin = bin + "1"
    Else
    bin = bin + "0"
    End If
    Next
    CONV = bin ' The bin string contains the binary number.
    
    End Function
    So you can just store the number as decimal and just convert it to binary.

    Rob
    My secretary hopes that I will pay her, her landlord hopes that she will produce some rent, the Electricity Board hopes that he will settle their bill, and so on. I find it a wonderfully optimistic way of life. [Dirk Gently]

  5. #5
    Junior Member
    Join Date
    Jun 2001
    Posts
    22
    oh yeah, sorry about that....

    i read it as he wanted it to be four digits....

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    not so cumbersome as it looks

    Code:
    Private Sub Command1_Click()
        Static x&, a(7) As Byte
        x = x + 1
        a(0) = 48 - CBool(x And 8)
        a(2) = 48 - CBool(x And 4)
        a(4) = 48 - CBool(x And 2)
        a(6) = 48 - CBool(x And 1)
        Caption = CStr(a)
    End Sub
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Here is a different approach:

    VB Code:
    1. Private Sub Command1_Click()
    2.     Static x
    3.    
    4.     x = x + 1
    5.     Debug.Print Format$(bin(x), "0000")
    6.    
    7. End Sub
    8.  
    9. Function bin(ByVal x As Long) As String
    10.     Do
    11.         bin = (x And 1) & bin
    12.         x = x \ 2
    13.     Loop While x
    14. End Function

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