PC Review


Reply
Thread Tools Rate Thread

Bitwise comparison question

 
 
Ian Gilmore
Guest
Posts: n/a
 
      21st Apr 2007
I have some data which presents active days like this NNYYYYN or any
combination of them. I am told I can store the value of all combinations
in a single integer. I have read that this can be acheived by using
bitwise comparison. Unfortunately, I have never used this so don't
really understand it without seeing a simple explanation. I know you
guys are the best so would appreciate an explanation and ideally some
sample code.

Many Thanks
 
Reply With Quote
 
 
 
 
Helmut Weber
Guest
Posts: n/a
 
      21st Apr 2007
Hi Ian,

>active days like this NNYYYYN


I understand that YYYY may represent a year from 0000 to 9999.
But what would be N?

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
Reply With Quote
 
=?Utf-8?B?R2FyeScncyBTdHVkZW50?=
Guest
Posts: n/a
 
      21st Apr 2007
I think he means that for each day the value is yes or no. If represented by
0/1 the bitwise comparison makes sense.
--
Gary''s Student - gsnu200717


"Helmut Weber" wrote:

> Hi Ian,
>
> >active days like this NNYYYYN

>
> I understand that YYYY may represent a year from 0000 to 9999.
> But what would be N?
>
> --
> Greetings from Bavaria, Germany
>
> Helmut Weber, MVP WordVBA
>
> Win XP, Office 2003
> "red.sys" & Chr$(64) & "t-online.de"
>

 
Reply With Quote
 
RB Smissaert
Guest
Posts: n/a
 
      21st Apr 2007
Have a look here:

http://www.dicks-blog.com/archives/2004/07/23/

RBS

"Ian Gilmore" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I have some data which presents active days like this NNYYYYN or any
>combination of them. I am told I can store the value of all combinations in
>a single integer. I have read that this can be acheived by using bitwise
>comparison. Unfortunately, I have never used this so don't really
>understand it without seeing a simple explanation. I know you guys are the
>best so would appreciate an explanation and ideally some sample code.
>
> Many Thanks


 
Reply With Quote
 
Peter T
Guest
Posts: n/a
 
      21st Apr 2007
Function bHasBit(ByVal num As Long, ByVal bit As Long) As Boolean
bHasBit = ((num And 2 ^ bit) = 2 ^ bit)
End Function

Sub test()
Dim i As Long
Dim strIn As String
Dim strOut As String
Dim sBin As String, sVal
Dim WeekBits As Long ' store somewhere else, eg Global

strIn = "NNYYYYN"
For i = 1 To Len(strIn)
WeekBits = WeekBits Or 2 ^ -((Mid(strIn, i, 1) = "Y") * i)

sBin = sBin & -((Mid(strIn, i, 1) = "Y"))
sVal = sVal & 2 ^ -((Mid(strIn, i, 1) = "Y") * i) & " "
Next

For i = 1 To 7
strOut = strOut & IIf(bHasBit(WeekBits, i), "Y", "N")
Next

MsgBox strIn & vbCr & sBin & vbCr & sVal & vbCr & strOut
End Sub

Hopefully looking at sBin and sVal above will go some way to giving an
explanation.
Note that bits are numbered from zero. So this demo takes advantage of not
bothering about filling the first bit, which it does with any false's, 2^0 =
1. The use of 'Or' instead or '+' prevents the false-1 added more than once.

See 'And' & 'Or' in help and more about bitwise comparison.

FWIW, you could store a month's worth of N/Y, 31days. BUT, the example would
need to modified to offset by -1, ie zero to 30. Also, you couldn't add any
false 2^0 as in this lazy demo (guess purists wouldn't like the use of the
minus to convert True(-1) to +1).

Regards,
Peter T



"Ian Gilmore" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I have some data which presents active days like this NNYYYYN or any
> combination of them. I am told I can store the value of all combinations
> in a single integer. I have read that this can be acheived by using
> bitwise comparison. Unfortunately, I have never used this so don't
> really understand it without seeing a simple explanation. I know you
> guys are the best so would appreciate an explanation and ideally some
> sample code.
>
> Many Thanks



 
Reply With Quote
 
Ian Gilmore
Guest
Posts: n/a
 
      22nd Apr 2007
Peter T wrote:
> Function bHasBit(ByVal num As Long, ByVal bit As Long) As Boolean
> bHasBit = ((num And 2 ^ bit) = 2 ^ bit)
> End Function
>
> Sub test()
> Dim i As Long
> Dim strIn As String
> Dim strOut As String
> Dim sBin As String, sVal
> Dim WeekBits As Long ' store somewhere else, eg Global
>
> strIn = "NNYYYYN"
> For i = 1 To Len(strIn)
> WeekBits = WeekBits Or 2 ^ -((Mid(strIn, i, 1) = "Y") * i)
>
> sBin = sBin & -((Mid(strIn, i, 1) = "Y"))
> sVal = sVal & 2 ^ -((Mid(strIn, i, 1) = "Y") * i) & " "
> Next
>
> For i = 1 To 7
> strOut = strOut & IIf(bHasBit(WeekBits, i), "Y", "N")
> Next
>
> MsgBox strIn & vbCr & sBin & vbCr & sVal & vbCr & strOut
> End Sub
>
> Hopefully looking at sBin and sVal above will go some way to giving an
> explanation.
> Note that bits are numbered from zero. So this demo takes advantage of not
> bothering about filling the first bit, which it does with any false's, 2^0 =
> 1. The use of 'Or' instead or '+' prevents the false-1 added more than once.
>
> See 'And' & 'Or' in help and more about bitwise comparison.
>
> FWIW, you could store a month's worth of N/Y, 31days. BUT, the example would
> need to modified to offset by -1, ie zero to 30. Also, you couldn't add any
> false 2^0 as in this lazy demo (guess purists wouldn't like the use of the
> minus to convert True(-1) to +1).
>
> Regards,
> Peter T
>
>
>
> "Ian Gilmore" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> I have some data which presents active days like this NNYYYYN or any
>> combination of them. I am told I can store the value of all combinations
>> in a single integer. I have read that this can be acheived by using
>> bitwise comparison. Unfortunately, I have never used this so don't
>> really understand it without seeing a simple explanation. I know you
>> guys are the best so would appreciate an explanation and ideally some
>> sample code.
>>
>> Many Thanks

>
>

Thany you very much Peter. A really good explanation.
 
Reply With Quote
 
Dana DeLouis
Guest
Posts: n/a
 
      22nd Apr 2007
>>> ... I am told I can store the value of all combinations
>>> in a single integer.


Just another idea...

Sub Demo()
Dim s As String
Dim n As Long

s = "NNYYYYN"
s = Replace(s, "N", "0")
s = Replace(s, "Y", "1")
n = WorksheetFunction.Bin2Dec(s)
End Sub

--
HTH :>)
Dana DeLouis
Windows XP & Office 2007


"Ian Gilmore" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Peter T wrote:
>> Function bHasBit(ByVal num As Long, ByVal bit As Long) As Boolean
>> bHasBit = ((num And 2 ^ bit) = 2 ^ bit)
>> End Function
>>
>> Sub test()
>> Dim i As Long
>> Dim strIn As String
>> Dim strOut As String
>> Dim sBin As String, sVal
>> Dim WeekBits As Long ' store somewhere else, eg Global
>>
>> strIn = "NNYYYYN"
>> For i = 1 To Len(strIn)
>> WeekBits = WeekBits Or 2 ^ -((Mid(strIn, i, 1) = "Y") * i)
>>
>> sBin = sBin & -((Mid(strIn, i, 1) = "Y"))
>> sVal = sVal & 2 ^ -((Mid(strIn, i, 1) = "Y") * i) & " "
>> Next
>>
>> For i = 1 To 7
>> strOut = strOut & IIf(bHasBit(WeekBits, i), "Y", "N")
>> Next
>>
>> MsgBox strIn & vbCr & sBin & vbCr & sVal & vbCr & strOut
>> End Sub
>>
>> Hopefully looking at sBin and sVal above will go some way to giving an
>> explanation.
>> Note that bits are numbered from zero. So this demo takes advantage of
>> not
>> bothering about filling the first bit, which it does with any false's,
>> 2^0 =
>> 1. The use of 'Or' instead or '+' prevents the false-1 added more than
>> once.
>>
>> See 'And' & 'Or' in help and more about bitwise comparison.
>>
>> FWIW, you could store a month's worth of N/Y, 31days. BUT, the example
>> would
>> need to modified to offset by -1, ie zero to 30. Also, you couldn't add
>> any
>> false 2^0 as in this lazy demo (guess purists wouldn't like the use of
>> the
>> minus to convert True(-1) to +1).
>>
>> Regards,
>> Peter T
>>
>>
>>
>> "Ian Gilmore" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> I have some data which presents active days like this NNYYYYN or any
>>> combination of them. I am told I can store the value of all combinations
>>> in a single integer. I have read that this can be acheived by using
>>> bitwise comparison. Unfortunately, I have never used this so don't
>>> really understand it without seeing a simple explanation. I know you
>>> guys are the best so would appreciate an explanation and ideally some
>>> sample code.
>>>
>>> Many Thanks

>>
>>

> Thany you very much Peter. A really good explanation.



 
Reply With Quote
 
Peter T
Guest
Posts: n/a
 
      22nd Apr 2007
Hi Dana,

I like the use of Replace, looping a string of 0/1's would be faster than
N/Y's and use of 'If' etc.

A few things to consider with Bin2Dex though:

- Requires Analysis toolpack and probably slower in VBA than using a simple
loop

- Converts a Binary string with the least significant bit last, in contrast
to least significant bit first.

This might be fine providing any other functions that use the added bit
number work with same order. So it means in effect re-numbering the days
from 1-7 to 6-0. Also, in any related functions 2^0=1 should be reserved for
the day numbered 0, and not for gratuitously adding any False as in my
previous demo.

FWIW, a bit numbered 'n' in a number named 'num' can in effect be set to
True like this
num = num Or 2^n

and set to False
num = num And Not 2^n

or toggled
num = num Xor 2^n

Regards,
Peter T


That would be fine providing other cod
"Dana DeLouis" <(E-Mail Removed)> wrote in message
news:#(E-Mail Removed)...
> >>> ... I am told I can store the value of all combinations
> >>> in a single integer.

>
> Just another idea...
>
> Sub Demo()
> Dim s As String
> Dim n As Long
>
> s = "NNYYYYN"
> s = Replace(s, "N", "0")
> s = Replace(s, "Y", "1")
> n = WorksheetFunction.Bin2Dec(s)
> End Sub
>
> --
> HTH :>)
> Dana DeLouis
> Windows XP & Office 2007
>
>
> "Ian Gilmore" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Peter T wrote:
> >> Function bHasBit(ByVal num As Long, ByVal bit As Long) As Boolean
> >> bHasBit = ((num And 2 ^ bit) = 2 ^ bit)
> >> End Function
> >>
> >> Sub test()
> >> Dim i As Long
> >> Dim strIn As String
> >> Dim strOut As String
> >> Dim sBin As String, sVal
> >> Dim WeekBits As Long ' store somewhere else, eg Global
> >>
> >> strIn = "NNYYYYN"
> >> For i = 1 To Len(strIn)
> >> WeekBits = WeekBits Or 2 ^ -((Mid(strIn, i, 1) = "Y") * i)
> >>
> >> sBin = sBin & -((Mid(strIn, i, 1) = "Y"))
> >> sVal = sVal & 2 ^ -((Mid(strIn, i, 1) = "Y") * i) & " "
> >> Next
> >>
> >> For i = 1 To 7
> >> strOut = strOut & IIf(bHasBit(WeekBits, i), "Y", "N")
> >> Next
> >>
> >> MsgBox strIn & vbCr & sBin & vbCr & sVal & vbCr & strOut
> >> End Sub
> >>
> >> Hopefully looking at sBin and sVal above will go some way to giving an
> >> explanation.
> >> Note that bits are numbered from zero. So this demo takes advantage of
> >> not
> >> bothering about filling the first bit, which it does with any false's,
> >> 2^0 =
> >> 1. The use of 'Or' instead or '+' prevents the false-1 added more than
> >> once.
> >>
> >> See 'And' & 'Or' in help and more about bitwise comparison.
> >>
> >> FWIW, you could store a month's worth of N/Y, 31days. BUT, the example
> >> would
> >> need to modified to offset by -1, ie zero to 30. Also, you couldn't add
> >> any
> >> false 2^0 as in this lazy demo (guess purists wouldn't like the use of
> >> the
> >> minus to convert True(-1) to +1).
> >>
> >> Regards,
> >> Peter T
> >>
> >>
> >>
> >> "Ian Gilmore" <(E-Mail Removed)> wrote in message
> >> news:(E-Mail Removed)...
> >>> I have some data which presents active days like this NNYYYYN or any
> >>> combination of them. I am told I can store the value of all

combinations
> >>> in a single integer. I have read that this can be acheived by using
> >>> bitwise comparison. Unfortunately, I have never used this so don't
> >>> really understand it without seeing a simple explanation. I know you
> >>> guys are the best so would appreciate an explanation and ideally some
> >>> sample code.
> >>>
> >>> Many Thanks
> >>
> >>

> > Thany you very much Peter. A really good explanation.

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bitwise comparison in RowFilters Mark Rae Microsoft ASP .NET 2 7th Oct 2006 06:13 PM
Bitwise comparison in RowFilter expressions Mark Rae Microsoft ADO .NET 2 7th Oct 2006 03:07 PM
Bitwise comparison and enumerations / switch statements Anders Borum Microsoft C# .NET 3 3rd Nov 2005 03:57 AM
Bitwise operation question... gilad@arbingersysBADSPAMBOT.com Microsoft C# .NET 6 6th Oct 2004 07:54 PM
Question of The bitwise or operator ±èÀçȲ Microsoft C# .NET 1 31st Aug 2003 12:41 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:18 AM.