|
-
Jul 12th, 2006, 07:04 PM
#1
Thread Starter
Addicted Member
Can someone explain this to me?
I saw something similar to this once, and I was wondering how it's evaluated to the final value. I can't seem to figure it out.
VB Code:
Text1.Text = (Text1.Text Mod 50) Or (Text1.Text + 48)
-
Jul 12th, 2006, 07:08 PM
#2
Re: Can someone explain this to me?
-
Jul 12th, 2006, 07:13 PM
#3
Thread Starter
Addicted Member
Re: Can someone explain this to me?
LOL I realize but it must have some use.
Say, the user enters a number between 40 and 50, and I want to add 2 to it. BUT, I have to keep the value between 40 and 50, and have it roll over. For example, 49+2 rolls over back to 40. How would I go about doing that. My old professor says it can be done with just boolean statements and math.
Is that what the above code can be used for?
-
Jul 12th, 2006, 07:15 PM
#4
Re: Can someone explain this to me?
X mod Y gives the remainder after X is divided by Y.
then the bits are ORed together. to use an example:
64 Mod 50 = 14
64 + 48 = 112
14 Or 112:
Code:
0001110 - 14
1110000 - 112
-------
1111110 - 126
but I don't see the use of it
-
Jul 12th, 2006, 07:20 PM
#5
Re: Can someone explain this to me?
and to answer post #3:
VB Code:
Text1.Text = (Text1.Text - 38) Mod 10 + 40
-
Jul 12th, 2006, 07:22 PM
#6
Thread Starter
Addicted Member
Re: Can someone explain this to me?
LOL OMG, that's so obvious. Thx man.
-
Jul 12th, 2006, 07:23 PM
#7
Re: Can someone explain this to me?
 Originally Posted by INF3RN0666
LOL I realize but it must have some use.
Say, the user enters a number between 40 and 50, and I want to add 2 to it. BUT, I have to keep the value between 40 and 50, and have it roll over. For example, 49+2 rolls over back to 40. How would I go about doing that. My old professor says it can be done with just Boolean statements and math.
Is that what the above code can be used for?
As an academic exercise I wouldn't be surprised if it couldn't be done with just Boolean statements but I would hate to have to maintain the program without lots of explanatory comments. I think the following would be better (but even it could use a comment or two).
VB Code:
If Text1.Text > 39 And Text1.Text < 51 Then
If Text1.Text + 2 > 50 Then
Text1.Text = "40"
Else
Text1.Text = Text1.Text + 2
End If
Else
Text1.Text = 40
End If
-
Jul 12th, 2006, 07:26 PM
#8
Re: Can someone explain this to me?
 Originally Posted by INF3RN0666
LOL OMG, that's so obvious. Thx man.
But that doesn't work.
30 -> 31
48 -> 40
49 -> 41
50 -> 42
-
Jul 12th, 2006, 07:26 PM
#9
Thread Starter
Addicted Member
Re: Can someone explain this to me?
Well, which is faster for execution. I'm going to be using this for character processing for hundreds of strings.
-
Jul 12th, 2006, 07:30 PM
#10
Re: Can someone explain this to me?
 Originally Posted by MartinLiss
But that doesn't work.
30 -> 31
48 -> 40
49 -> 41
50 -> 42
I was assuming that the value in Text1.Text would already be between 40 and 50. I also thought he wanted 49 + 2 to roll to 41, but to make it roll to 40 do:
VB Code:
Text1.Text = (Text1.Text - 38) Mod [B]11[/B] + 40
-
Jul 12th, 2006, 07:30 PM
#11
Thread Starter
Addicted Member
Re: Can someone explain this to me?
Ok I just realized that I might be subtracting or adding values. This is either going to be like a page of code, or there's a short way to do it.
-
Jul 12th, 2006, 07:32 PM
#12
Re: Can someone explain this to me?
 Originally Posted by INF3RN0666
Well, which is faster for execution. I'm going to be using this for character processing for hundreds of strings.
what martin and I have provided are examples, not solutions - the best method would entirely depend on what you were trying to do - and may not, in fact, be either of these.
-
Jul 12th, 2006, 07:37 PM
#13
Thread Starter
Addicted Member
Re: Can someone explain this to me?
Yeah sorry, I haven't exactly defined my position here.
What I was doing is trying to shift letters over up or down depending on a value provided. For example, if the user enters 2, it shifts A to C, or Z to B. If the user enters -2, it would shift A to Y, and Z to X. Catch my drift?
I came down to the following:
VB Code:
Select Case bytChar
Case 48 To 57
Case 65 To 90
Case 97 To 122
bytChar = bytechar + Value
If bytChar > 122 Then
bytChar = bytChar - 26
ElseIf bytChar < 97 Then
bytChar = bytChar + 26
End If
barOut(lngPos) = bytChar
lngPos = lngPos + 2
-
Jul 12th, 2006, 07:50 PM
#14
Re: Can someone explain this to me?
 Originally Posted by INF3RN0666
Yeah sorry, I haven't exactly defined my position here.
What I was doing is trying to shift letters over up or down depending on a value provided. For example, if the user enters 2, it shifts A to C, or Z to B. If the user enters -2, it would shift A to Y, and Z to X. Catch my drift?
I came down to the following:
VB Code:
Select Case bytChar
Case 48 To 57
Case 65 To 90
Case 97 To 122
bytChar = bytechar + Value
If bytChar > 122 Then
bytChar = bytChar - 26
ElseIf bytChar < 97 Then
bytChar = bytChar + 26
End If
barOut(lngPos) = bytChar
lngPos = lngPos + 2
In the future, please give us that kind of information up front.
-
Jul 12th, 2006, 08:07 PM
#15
Re: Can someone explain this to me?
you can use the same formula for shifting forwards or backwards (97 to 122 are a to z btw - not A to Z):
VB Code:
Private Sub Form_Load()
Debug.Print ShiftIt("abcdefghijklmnopqrstuvwxyz", 1)
Debug.Print ShiftIt("abcdefghijklmnopqrstuvwxyz", -1)
End Sub
Private Function ShiftIt(ByVal sString As String, ByVal lShift As Long) As String
Dim b() As Byte, N As Long
b = sString
For N = 0 To UBound(b) Step 2
Select Case b(N)
Case 97 To 122
b(N) = ((b(N) - 97) + (26 + lShift)) Mod 26 + 97
End Select
Next N
ShiftIt = b
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
|