|
-
Jul 24th, 2001, 09:00 PM
#1
Thread Starter
New Member
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]
-
Jul 25th, 2001, 12:15 PM
#2
Junior Member
s = Format(n, "0000")
that should do it...
-
Jul 25th, 2001, 01:28 PM
#3
Frenzied Member
No that wont do it. If just makes sure the number returned is 4 digits long, it doesnt count binary.
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.
-
Jul 25th, 2001, 02:20 PM
#4
Fanatic Member
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]
-
Jul 25th, 2001, 02:25 PM
#5
Junior Member
oh yeah, sorry about that....
i read it as he wanted it to be four digits....
-
Jul 25th, 2001, 03:24 PM
#6
transcendental analytic
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.
-
Aug 9th, 2001, 09:52 PM
#7
Registered User
Here is a different approach:
VB Code:
Private Sub Command1_Click()
Static x
x = x + 1
Debug.Print Format$(bin(x), "0000")
End Sub
Function bin(ByVal x As Long) As String
Do
bin = (x And 1) & bin
x = x \ 2
Loop While x
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|