Results 1 to 15 of 15

Thread: Can someone explain this to me?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2002
    Posts
    229

    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:
    1. Text1.Text = (Text1.Text Mod 50) Or (Text1.Text + 48)

  2. #2

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2002
    Posts
    229

    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?

  4. #4
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    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

  5. #5
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Can someone explain this to me?

    and to answer post #3:
    VB Code:
    1. Text1.Text = (Text1.Text - 38) Mod 10 + 40

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Mar 2002
    Posts
    229

    Re: Can someone explain this to me?

    LOL OMG, that's so obvious. Thx man.

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Can someone explain this to me?

    Quote 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:
    1. If Text1.Text > 39 And Text1.Text < 51 Then
    2.     If Text1.Text + 2 > 50 Then
    3.         Text1.Text = "40"
    4.     Else
    5.         Text1.Text = Text1.Text + 2
    6.     End If
    7. Else
    8.     Text1.Text = 40
    9. End If

  8. #8

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Mar 2002
    Posts
    229

    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.

  10. #10
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Can someone explain this to me?

    Quote 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:
    1. Text1.Text = (Text1.Text - 38) Mod [B]11[/B] + 40

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Mar 2002
    Posts
    229

    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.

  12. #12
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Can someone explain this to me?

    Quote 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.

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Mar 2002
    Posts
    229

    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:
    1. Select Case bytChar
    2.             Case 48 To 57
    3.            
    4.             Case 65 To 90
    5.            
    6.             Case 97 To 122
    7.                 bytChar = bytechar + Value
    8.                
    9.                 If bytChar > 122 Then
    10.                     bytChar = bytChar - 26
    11.                 ElseIf bytChar < 97 Then
    12.                     bytChar = bytChar + 26
    13.                 End If
    14.                
    15.                 barOut(lngPos) = bytChar
    16.                 lngPos = lngPos + 2

  14. #14
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Can someone explain this to me?

    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:
    1. Select Case bytChar
    2.             Case 48 To 57
    3.            
    4.             Case 65 To 90
    5.            
    6.             Case 97 To 122
    7.                 bytChar = bytechar + Value
    8.                
    9.                 If bytChar > 122 Then
    10.                     bytChar = bytChar - 26
    11.                 ElseIf bytChar < 97 Then
    12.                     bytChar = bytChar + 26
    13.                 End If
    14.                
    15.                 barOut(lngPos) = bytChar
    16.                 lngPos = lngPos + 2
    In the future, please give us that kind of information up front.

  15. #15
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    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:
    1. Private Sub Form_Load()
    2.     Debug.Print ShiftIt("abcdefghijklmnopqrstuvwxyz", 1)
    3.     Debug.Print ShiftIt("abcdefghijklmnopqrstuvwxyz", -1)
    4. End Sub
    5.  
    6. Private Function ShiftIt(ByVal sString As String, ByVal lShift As Long) As String
    7.     Dim b() As Byte, N As Long
    8.     b = sString
    9.     For N = 0 To UBound(b) Step 2
    10.         Select Case b(N)
    11.             Case 97 To 122
    12.                 b(N) = ((b(N) - 97) + (26 + lShift)) Mod 26 + 97
    13.         End Select
    14.     Next N
    15.     ShiftIt = b
    16. 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