Interrogating a binary value in an integer

B

BobRoyAce

I have a third party user control that contains 7 checkedits, one for
each day of the week. It stores which days are checked as an integer
by adding up the values for each day checked, using the following
values:

SUN 1
MON 2
TUE 4
WED 8
THU 16
FRI 32
SAT 64

So, for example, if TUE, WED, & THU are the only days checked, it
stores a value of 28. If FRI, SAT, & SUN only, it stores a value of
97.

How can I interrogate the integer value to know if a particular day
was checked? In other words, if I knew that the integer value was 28,
what calculation/operation would I perform on that number to determine
that, yes, it does indicate that THU was checked? Or, if the value was
96, I could determine that no, THU was not checked?
 
T

Teemu

BobRoyAce said:
I have a third party user control that contains 7 checkedits, one for
each day of the week. It stores which days are checked as an integer
by adding up the values for each day checked, using the following
values:

SUN 1
MON 2
TUE 4
WED 8
THU 16
FRI 32
SAT 64

So, for example, if TUE, WED, & THU are the only days checked, it
stores a value of 28. If FRI, SAT, & SUN only, it stores a value of
97.

How can I interrogate the integer value to know if a particular day
was checked? In other words, if I knew that the integer value was 28,
what calculation/operation would I perform on that number to determine
that, yes, it does indicate that THU was checked? Or, if the value was
96, I could determine that no, THU was not checked?

You can use AND operator to check this. Here is a small example:

Dim tb As New System.Text.StringBuilder
Dim value As Integer = 97

Dim i As Integer = 0

While 2 ^ i < 128

tb.AppendLine(i.ToString + " " + CStr((value And 2 ^ i) = 2 ^
i))
i += 1

End While

TextBox1.Text = tb.ToString

-Teemu
 
T

Teemu

Teemu said:
You can use AND operator to check this. Here is a small example:

Dim tb As New System.Text.StringBuilder
Dim value As Integer = 97

Dim i As Integer = 0

While 2 ^ i < 128

tb.AppendLine(i.ToString + " " + CStr((value And 2 ^ i) = 2 ^
i))
i += 1

End While

TextBox1.Text = tb.ToString

Maybe this was a bad example. If you want to know does 97 contain THU, just
check if (97 And 16)=16.

-Teemu
 
J

J.B. Moreno

BobRoyAce said:
I have a third party user control that contains 7 checkedits, one for
each day of the week. It stores which days are checked as an integer
by adding up the values for each day checked, using the following
values:

SUN 1
MON 2
TUE 4
WED 8
THU 16
FRI 32
SAT 64

So, for example, if TUE, WED, & THU are the only days checked, it
stores a value of 28. If FRI, SAT, & SUN only, it stores a value of
97.

How can I interrogate the integer value to know if a particular day
was checked?

const sun as integer = 1
const thur as integer = 16
const sat as integer = 64

dim tday as integer

tday = 28

if tday AND thurs then

end if
 

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

Sorting day names chronologically 5
4 if's, with 4 different results 4
Next weekday ... 3
The Tueday prior to a holiday... 8
Attn: Luke - followup to the 4 IF's from Friday 10
Payroll 1
Dcounta 4
System.Collections 3

Top