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)
Printable View
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)
Boy that's awful code.
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?
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:but I don't see the use of itCode:0001110 - 14
1110000 - 112
-------
1111110 - 126
and to answer post #3:VB Code:
Text1.Text = (Text1.Text - 38) Mod 10 + 40
LOL OMG, that's so obvious. Thx man.
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).Quote:
Originally Posted by INF3RN0666
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
But that doesn't work.Quote:
Originally Posted by INF3RN0666
30 -> 31
48 -> 40
49 -> 41
50 -> 42
Well, which is faster for execution. I'm going to be using this for character processing for hundreds of strings.
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:Quote:
Originally Posted by MartinLiss
VB Code:
Text1.Text = (Text1.Text - 38) Mod [B]11[/B] + 40
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.
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.Quote:
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.Quote:
Originally Posted by INF3RN0666
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