Bitwise operations

A

AMDRIT

Gang,

I always get confused when it comes to 1's and 0's. I would like to perform
a bitwise operation on a value based on checked boxes.

Am I doing this right?


assuming TurmsRemote.Weekdays has values of

Monday =2
Tuesday =4
Wednesday =8
Thursday = 16
Friday =32
Saturday = 64
Sunday = 128


Sub Monday_Click(Sender, E)
dim chkbox as checkbox = ctype(sender, checkbox)

If chkbox.Checked Then
If Not Me.WeeklyDays And TurmsRemote.Weekdays.Monday Then
Me.WeeklyDays = Me.WeeklyDays And TurmsRemote.Weekdays.Monday
End If
Else
If Me.WeeklyDays And TurmsRemote.Weekdays.Monday Then
Me.WeeklyDays = Me.WeeklyDays - TurmsRemote.Weekdays.Monday
End If
End If

End Sub
 
C

Cor Ligthert [MVP]

AMDRIT,

Why would you use it, it is in my opinion a little bit from the time that a
computer had 32K internal memory. (Therefore not so based on the Visual
Basic as you use).

It makes your program not better readable/maintanable while there is a nice
date class which has a method for weekdays for you.

http://msdn2.microsoft.com/en-us/library/t8dc1aee.aspx

Just my thought,

Cor
 
J

Jevon

You could actually simplify your code a bit - the inner If statements
probably cause more processing than just doing the logical calculation:

Sub SwitchBit(ByRef checked As Boolean, ByRef weekday As
TurmsRemote.Weekdays)
If checked = True Then
Me.WeeklyDays = Me.WeeklyDays And weekday
Else
Me.WeeklyDays = Me.WeeklyDays And Not weekday
End If
End Sub

I'm sure you can figure out how to call it! Cuts down on copy&paste repeated
code this way.

If you're doing a lot of manipulation like this, drawing out a few logic
tables on paper is often quite helpful.

Hope it helps,

Jevon
 
A

AMDRIT

I appreciate your thoughts. I was attempting to minimize the number of
columns on a table. Rather than having 7 bit columns, I would have 1 column
to represent the same data. I am creating a scheduler that may execute jobs
on a daily basis (i.e 12 AM every Mon, Tues, Thur, Friday)
 
A

AMDRIT

I understand better what it was that I was asking.

I wanted to have a value "x" represent the any or all of the values of a
list of values. To do so, I had wanted to perform bitwise operations to add
or remove values from "x". I have determined that I need to use the
operators ("Or" and "And Not") to add or remove values from "x"

Taking from Jevon's code reduction suggestion here is the resulting code:
(Jevon, I changed the byref's to byval's; was their a reason to use byref?)


Private Sub SwitchBit(ByVal checked As Boolean, ByVal weekday As
TurmsRemote.Weekdays)

If checked = True Then
Me.WeeklyDays = Me.WeeklyDays Or CType(weekday, Byte)
Else
Me.WeeklyDays = Me.WeeklyDays And Not CType(weekday, Byte)
End If

End Sub


This seems to solve all of the operations that I need. Unless anyone has
more to add, I will commit this to the ol' noggin for later use.


Thanks gang.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top