|
-
Oct 7th, 2009, 04:26 AM
#1
Thread Starter
Hyperactive Member
[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.
-
Oct 7th, 2009, 05:46 AM
#2
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:
Dim days As Integer = 2 Or 32 'Monday and Friday If (days And 1) = 1 Then MessageBox.Show("Sunday") Else MessageBox.Show("Not Sunday") End If If (days And 2) = 2 Then MessageBox.Show("Monday") Else MessageBox.Show("Not Monday") End If If (days And 4) = 4 Then MessageBox.Show("Tuesday") Else MessageBox.Show("Not Tuesday") End If If (days And 8) = 8 Then MessageBox.Show("Wednesday") Else MessageBox.Show("Not Wednesday") End If If (days And 16) = 16 Then MessageBox.Show("Thursday") Else MessageBox.Show("Not Thursday") End If If (days And 32) = 32 Then MessageBox.Show("Friday") Else MessageBox.Show("Not Friday") End If If (days And 64) = 64 Then MessageBox.Show("Saturday") Else MessageBox.Show("Not Saturday") End If
-
Oct 7th, 2009, 05:49 AM
#3
Addicted Member
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() ) );
-
Oct 7th, 2009, 06:40 AM
#4
Thread Starter
Hyperactive Member
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.
-
Oct 7th, 2009, 05:41 PM
#5
Re: Logic With days
 Originally Posted by Oliver1
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.
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
|