Results 1 to 5 of 5

Thread: [RESOLVED] Logic With days

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    262

    Resolved [RESOLVED] Logic With days

    Hi All,

    We have a system were a user chooses a combination of days from a calander. We then insert a values in the databse based on the following

    Sunday - 1
    Monday - 2
    Tuesday - 4
    Wednesday - 8
    Thursday - 16
    Friday - 32
    Saturday - 64

    So if they picked Monday and Friday we would insert 34, or if they picked Monday,Tuesday and Thursday we would insert 22.

    Now my question is how do I reverse this easily in code, to find out what days they have picked. So I get fed 22 from the database how do I figure out what combination of days make up that value.

    Sorry it sounds a bit like a homework project bit it really isn't.
    The only thing I can think of is creating the mother of all case statements but I know there must be a better way.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Logic With days

    You don't have to but I would suggest defining an enumeration to represent the days.

    http://www.vbforums.com/showthread.php?t=502644

    The logic is still pretty much the same even if you don't use an enumeration, i.e. bitwise operations, e.g.
    vb.net Code:
    1. Dim days As Integer = 2 Or 32 'Monday and Friday
    2.  
    3. If (days And 1) = 1 Then
    4.     MessageBox.Show("Sunday")
    5. Else
    6.     MessageBox.Show("Not Sunday")
    7. End If
    8.  
    9. If (days And 2) = 2 Then
    10.     MessageBox.Show("Monday")
    11. Else
    12.     MessageBox.Show("Not Monday")
    13. End If
    14.  
    15. If (days And 4) = 4 Then
    16.     MessageBox.Show("Tuesday")
    17. Else
    18.     MessageBox.Show("Not Tuesday")
    19. End If
    20.  
    21. If (days And 8) = 8 Then
    22.     MessageBox.Show("Wednesday")
    23. Else
    24.     MessageBox.Show("Not Wednesday")
    25. End If
    26.  
    27. If (days And 16) = 16 Then
    28.     MessageBox.Show("Thursday")
    29. Else
    30.     MessageBox.Show("Not Thursday")
    31. End If
    32.  
    33. If (days And 32) = 32 Then
    34.     MessageBox.Show("Friday")
    35. Else
    36.     MessageBox.Show("Not Friday")
    37. End If
    38.  
    39. If (days And 64) = 64 Then
    40.     MessageBox.Show("Saturday")
    41. Else
    42.     MessageBox.Show("Not Saturday")
    43. End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Addicted Member tsungik's Avatar
    Join Date
    Aug 2004
    Location
    Philippines
    Posts
    194

    Re: Logic With days

    Code:
        Dim tmp as Integer = 64 ' Change this to the value from the Database 
        Dim strDays as String = String.Empty
        If tmp >= 64 Then
            strDays = "Sat"
            tmp = tmp Mod 64
        End If
        If tmp >= 32 Then
            strDays = "Fri" + strDays
            tmp = tmp Mod 32
        End If
        If tmp >= 16 Then
            strDays = "Thu" + strDays
            tmp = tmp Mod 16
        End If
        If tmp >= 8 Then
            strDays = "Wed" + strDays
            tmp = tmp Mod 8
        End If
        If tmp >= 4 Then
            strDays = "Tue" + strDays
            tmp = tmp Mod 4
        End If
        If tmp >= 2 Then
            strDays = "Mon" + strDays
            tmp = tmp Mod 2
        End If
        If tmp >= 1 Then
            strDays = "Sun" + strDays
            tmp = tmp Mod 1
        End If
        MessageBox.Show(strDays)
    There is better code out there, but this will sure help you figure out to solve your problem.
    Begin with the end in mind.

    My Profile

    while( !( succeed = try() ) );

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    262

    Re: Logic With days

    Thanks both.
    Have never used "and" and "or" Bitwise Operators for anything other than booleans and that was a long time ago.
    Anyway I lied a little by posting this in the vb.net forum, as ideally I want to do this all at the tsql level (though worst come to worst I would have done it in code), quick google search gave me.

    http://sqlfool.com/2009/02/bitwise-operations/

    Which shows me how to do the same thing in sql.
    So thanks for your help.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Logic With days

    Quote Originally Posted by Oliver1 View Post
    Have never used "and" and "or" Bitwise Operators for anything other than booleans and that was a long time ago
    If you used them with Booleans then they were logical operators, not bitwise operators. Bitwise operations are basically logical operations at the bit level, where 1 is true and 0 is false. When you perform a bitwise operation on two values you basically perform a logical operation on each pair of corresponding bits.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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