473,379 Members | 1,252 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Help, BitMask function :(

Sigh... working on it for whole day and got no idea...

Basically I want to have a bitmask filtering function, I will throw in
3 parameters:

bitMaskValue - current bitmask value
filterValue - the value I want to see if its exist in "bitMaskValue"
BiggestValue - the maximum individual value can be exist

And I want it just to return true if the value exist, or false if not

So for example, I have 7 as "bitMaskValue", "filterValue" as 2 and
"BiggestValue" as 4... then it will return True... as 1 + 2 + 4 = 7

If I have 5 as "bitMaskValue", "filterValue" as 2 and "BiggestValue"
as 4... then it will return False... as 1 + 4 = 5... no "2"

I have come up the following be it just don't work :(

Private Function BitMaskFilter(ByVal bitMaskValue As Int32, ByVal
filterValue As Int32, ByVal BiggestValue As Int32) As Boolean
' Filter "filterValue" from "bitMaskValue" and return true if
value
' BiggestValue = biggest individual BitMask value
Dim filed As Boolean = False
While bitMaskValue > 1 And BiggestValue > 0
If bitMaskValue >= BiggestValue And BiggestValue <>
filterValue Then
bitMaskValue = bitMaskValue - BiggestValue
End If
If BiggestValue = filterValue Then
filed = True
End If
BiggestValue = BiggestValue / 2
End While
If filed Then
Return bitMaskValue = filterValue
Else
Return bitMaskValue = 0
End If

End Function

If I have the maximum possible value (in this case, 7) then it's ok...
if I have a bit missing in between the function will fail... I have
tried so many approach but I just can't think through it... any help
would be appericiated.

Thanks in advance.
Nov 21 '05 #1
5 2221

Some times it is better to build from the ground up. Here is some rough
spuedo-code with the obvious initial checks excluded.

Function SequentialBitMaskFilter(bitMask, biggestBit, filterMask)

<Obvious checks not included>

If filterMask = bitMask OR filterMask = 1 Then
Return True
End If

maskFound = False
nextBit = 1
bitBuilder = 1

Do While nextBit < biggestBit AND
bitBuilder < bitMask AND NOT maskFound
nextBit *= 2
bitBuilder += nextBit
If filterMask = nextBit OR
filterMask = bitBuilder Then
maskFound = True
ElseIf filterMask < nextBit Then
Exit Loop
End If
Loop

Return maskFound

End Function

***Important: Notice that the above only test for a sequential sum
filterMask. In other words it will catch filterMask values like;
1+2+4 or generally 1+2+4+...+2^m
However it will not catch filterMask values like;
1+8 (2 and 4 ommited) or generally
1+...+2^n +2^(n+x) +2^(n+x+1) +...+2^m
n<m, x>0, (n+x)<m
even though these filter mask do exist in a bitMask of 1+2+4+8 and
1+2+...+2^m. For this you need to take into account the combinations of n
bit positions choosing 1 bit to n bits for n in [1, m]. You can use the
above with a few modification in the loop to easily test for these cases.
You will also need to code a simple combinations function. I have not done
this here since your question does not seem to need to check non-sequential
sum filterMask. I just thought that you be aware of the limitations of the
solution.

--Robby

"Andy" <po********@yahoo.com.hk> wrote in message
news:9d**************************@posting.google.c om...
Sigh... working on it for whole day and got no idea...

Basically I want to have a bitmask filtering function, I will throw in
3 parameters:

bitMaskValue - current bitmask value
filterValue - the value I want to see if its exist in "bitMaskValue"
BiggestValue - the maximum individual value can be exist

And I want it just to return true if the value exist, or false if not

So for example, I have 7 as "bitMaskValue", "filterValue" as 2 and
"BiggestValue" as 4... then it will return True... as 1 + 2 + 4 = 7

If I have 5 as "bitMaskValue", "filterValue" as 2 and "BiggestValue"
as 4... then it will return False... as 1 + 4 = 5... no "2"

I have come up the following be it just don't work :(

Private Function BitMaskFilter(ByVal bitMaskValue As Int32, ByVal
filterValue As Int32, ByVal BiggestValue As Int32) As Boolean
' Filter "filterValue" from "bitMaskValue" and return true if
value
' BiggestValue = biggest individual BitMask value
Dim filed As Boolean = False
While bitMaskValue > 1 And BiggestValue > 0
If bitMaskValue >= BiggestValue And BiggestValue <>
filterValue Then
bitMaskValue = bitMaskValue - BiggestValue
End If
If BiggestValue = filterValue Then
filed = True
End If
BiggestValue = BiggestValue / 2
End While
If filed Then
Return bitMaskValue = filterValue
Else
Return bitMaskValue = 0
End If

End Function

If I have the maximum possible value (in this case, 7) then it's ok...
if I have a bit missing in between the function will fail... I have
tried so many approach but I just can't think through it... any help
would be appericiated.

Thanks in advance.

Nov 21 '05 #2

"Andy" <po********@yahoo.com.hk> wrote in message
news:9d**************************@posting.google.c om...
Sigh... working on it for whole day and got no idea...

Basically I want to have a bitmask filtering function, I will throw in
3 parameters:

bitMaskValue - current bitmask value
filterValue - the value I want to see if its exist in "bitMaskValue"
BiggestValue - the maximum individual value can be exist

And I want it just to return true if the value exist, or false if not

So for example, I have 7 as "bitMaskValue", "filterValue" as 2 and
"BiggestValue" as 4... then it will return True... as 1 + 2 + 4 = 7

If I have 5 as "bitMaskValue", "filterValue" as 2 and "BiggestValue"
as 4... then it will return False... as 1 + 4 = 5... no "2"

I have come up the following be it just don't work :(

Private Function BitMaskFilter(ByVal bitMaskValue As Int32, ByVal
filterValue As Int32, ByVal BiggestValue As Int32) As Boolean
' Filter "filterValue" from "bitMaskValue" and return true if
value
' BiggestValue = biggest individual BitMask value
Dim filed As Boolean = False
While bitMaskValue > 1 And BiggestValue > 0
If bitMaskValue >= BiggestValue And BiggestValue <>
filterValue Then
bitMaskValue = bitMaskValue - BiggestValue
End If
If BiggestValue = filterValue Then
filed = True
End If
BiggestValue = BiggestValue / 2
End While
If filed Then
Return bitMaskValue = filterValue
Else
Return bitMaskValue = 0
End If

End Function

If I have the maximum possible value (in this case, 7) then it's ok...
if I have a bit missing in between the function will fail... I have
tried so many approach but I just can't think through it... any help
would be appericiated.

Thanks in advance.


I may be missing the point somewhat but could you not use something like
this: -

Private Function BitMaskFilter(ByVal bitMaskValue As Int32, ByVal
filterValue As Int32, ByVal BiggestValue As Int32) As Boolean

'***Checks to ensure sensible input omitted***

Dim result As Integer = bitMaskValue And filterValue
Return (result > 0 AndAlso result <= BiggestValue)

End Function

Regards,

Nick Hall
Nov 21 '05 #3

Actually, after a goods nights sleep this method is not the best. I came up
with a new one but Nick beat me to it.

--Robby

"Robby" <ed****@not.my.email.com> wrote in message
news:OR**************@TK2MSFTNGP10.phx.gbl...

Some times it is better to build from the ground up. Here is some rough
spuedo-code with the obvious initial checks excluded.

Function SequentialBitMaskFilter(bitMask, biggestBit, filterMask)

<Obvious checks not included>

If filterMask = bitMask OR filterMask = 1 Then
Return True
End If

maskFound = False
nextBit = 1
bitBuilder = 1

Do While nextBit < biggestBit AND
bitBuilder < bitMask AND NOT maskFound
nextBit *= 2
bitBuilder += nextBit
If filterMask = nextBit OR
filterMask = bitBuilder Then
maskFound = True
ElseIf filterMask < nextBit Then
Exit Loop
End If
Loop

Return maskFound

End Function

***Important: Notice that the above only test for a sequential sum
filterMask. In other words it will catch filterMask values like;
1+2+4 or generally 1+2+4+...+2^m
However it will not catch filterMask values like;
1+8 (2 and 4 ommited) or generally
1+...+2^n +2^(n+x) +2^(n+x+1) +...+2^m
n<m, x>0, (n+x)<m
even though these filter mask do exist in a bitMask of 1+2+4+8 and
1+2+...+2^m. For this you need to take into account the combinations of
n bit positions choosing 1 bit to n bits for n in [1, m]. You can use the
above with a few modification in the loop to easily test for these cases.
You will also need to code a simple combinations function. I have not
done this here since your question does not seem to need to check
non-sequential sum filterMask. I just thought that you be aware of the
limitations of the solution.

--Robby

"Andy" <po********@yahoo.com.hk> wrote in message
news:9d**************************@posting.google.c om...
Sigh... working on it for whole day and got no idea...

Basically I want to have a bitmask filtering function, I will throw in
3 parameters:

bitMaskValue - current bitmask value
filterValue - the value I want to see if its exist in "bitMaskValue"
BiggestValue - the maximum individual value can be exist

And I want it just to return true if the value exist, or false if not

So for example, I have 7 as "bitMaskValue", "filterValue" as 2 and
"BiggestValue" as 4... then it will return True... as 1 + 2 + 4 = 7

If I have 5 as "bitMaskValue", "filterValue" as 2 and "BiggestValue"
as 4... then it will return False... as 1 + 4 = 5... no "2"

I have come up the following be it just don't work :(

Private Function BitMaskFilter(ByVal bitMaskValue As Int32, ByVal
filterValue As Int32, ByVal BiggestValue As Int32) As Boolean
' Filter "filterValue" from "bitMaskValue" and return true if
value
' BiggestValue = biggest individual BitMask value
Dim filed As Boolean = False
While bitMaskValue > 1 And BiggestValue > 0
If bitMaskValue >= BiggestValue And BiggestValue <>
filterValue Then
bitMaskValue = bitMaskValue - BiggestValue
End If
If BiggestValue = filterValue Then
filed = True
End If
BiggestValue = BiggestValue / 2
End While
If filed Then
Return bitMaskValue = filterValue
Else
Return bitMaskValue = 0
End If

End Function

If I have the maximum possible value (in this case, 7) then it's ok...
if I have a bit missing in between the function will fail... I have
tried so many approach but I just can't think through it... any help
would be appericiated.

Thanks in advance.


Nov 21 '05 #4
Robby,

It's acutally the non-sequential sum filterMask checking is bugging
me. If it's just the sequence one it will just take me 2 mins because
it's easy.

Would you mind to fill me in on the non-sequential sum checking part
with what you have given me, please?

Thank you.
Andy

"Robby" <ed****@not.my.email.com> wrote in message news:<OR**************@TK2MSFTNGP10.phx.gbl>...
Some times it is better to build from the ground up. Here is some rough
spuedo-code with the obvious initial checks excluded.

Function SequentialBitMaskFilter(bitMask, biggestBit, filterMask)

<Obvious checks not included>

If filterMask = bitMask OR filterMask = 1 Then
Return True
End If

maskFound = False
nextBit = 1
bitBuilder = 1

Do While nextBit < biggestBit AND
bitBuilder < bitMask AND NOT maskFound
nextBit *= 2
bitBuilder += nextBit
If filterMask = nextBit OR
filterMask = bitBuilder Then
maskFound = True
ElseIf filterMask < nextBit Then
Exit Loop
End If
Loop

Return maskFound

End Function

***Important: Notice that the above only test for a sequential sum
filterMask. In other words it will catch filterMask values like;
1+2+4 or generally 1+2+4+...+2^m
However it will not catch filterMask values like;
1+8 (2 and 4 ommited) or generally
1+...+2^n +2^(n+x) +2^(n+x+1) +...+2^m
n<m, x>0, (n+x)<m
even though these filter mask do exist in a bitMask of 1+2+4+8 and
1+2+...+2^m. For this you need to take into account the combinations of n
bit positions choosing 1 bit to n bits for n in [1, m]. You can use the
above with a few modification in the loop to easily test for these cases.
You will also need to code a simple combinations function. I have not done
this here since your question does not seem to need to check non-sequential
sum filterMask. I just thought that you be aware of the limitations of the
solution.

--Robby

"Andy" <po********@yahoo.com.hk> wrote in message
news:9d**************************@posting.google.c om...
Sigh... working on it for whole day and got no idea...

Basically I want to have a bitmask filtering function, I will throw in
3 parameters:

bitMaskValue - current bitmask value
filterValue - the value I want to see if its exist in "bitMaskValue"
BiggestValue - the maximum individual value can be exist

And I want it just to return true if the value exist, or false if not

So for example, I have 7 as "bitMaskValue", "filterValue" as 2 and
"BiggestValue" as 4... then it will return True... as 1 + 2 + 4 = 7

If I have 5 as "bitMaskValue", "filterValue" as 2 and "BiggestValue"
as 4... then it will return False... as 1 + 4 = 5... no "2"

I have come up the following be it just don't work :(

Private Function BitMaskFilter(ByVal bitMaskValue As Int32, ByVal
filterValue As Int32, ByVal BiggestValue As Int32) As Boolean
' Filter "filterValue" from "bitMaskValue" and return true if
value
' BiggestValue = biggest individual BitMask value
Dim filed As Boolean = False
While bitMaskValue > 1 And BiggestValue > 0
If bitMaskValue >= BiggestValue And BiggestValue <>
filterValue Then
bitMaskValue = bitMaskValue - BiggestValue
End If
If BiggestValue = filterValue Then
filed = True
End If
BiggestValue = BiggestValue / 2
End While
If filed Then
Return bitMaskValue = filterValue
Else
Return bitMaskValue = 0
End If

End Function

If I have the maximum possible value (in this case, 7) then it's ok...
if I have a bit missing in between the function will fail... I have
tried so many approach but I just can't think through it... any help
would be appericiated.

Thanks in advance.

Nov 21 '05 #5
Andy

You don't have to worry about the non-seqential if you use a bit operator
like Nick suggested.

What is weird about your post is the 'biggest bit' requirement. I'm not
certain that you need it. There is only one way to represent a number in
binary so 7 will always be {11100000} and 5 will always be {10100000}.
Therefore in these cases that you used as your examples specifiying the
biggest bit as 4 {00100000} is completely unessesary. This can be extended
to any number since it only has one binary representation there is no need
to try and validate that the biggiest bit will conform. It must conform or
the number will be a different number.

Why do you need to specify the biggest bit? Will your value of the biggest
bit ever differ from the most significant bit that defines your bit mask as
it perticular number?

--Robby

"Andy" <po********@yahoo.com.hk> wrote in message
news:9d**************************@posting.google.c om...
Robby,

It's acutally the non-sequential sum filterMask checking is bugging
me. If it's just the sequence one it will just take me 2 mins because
it's easy.

Would you mind to fill me in on the non-sequential sum checking part
with what you have given me, please?

Thank you.
Andy

"Robby" <ed****@not.my.email.com> wrote in message
news:<OR**************@TK2MSFTNGP10.phx.gbl>...
Some times it is better to build from the ground up. Here is some rough
spuedo-code with the obvious initial checks excluded.

Function SequentialBitMaskFilter(bitMask, biggestBit, filterMask)

<Obvious checks not included>

If filterMask = bitMask OR filterMask = 1 Then
Return True
End If

maskFound = False
nextBit = 1
bitBuilder = 1

Do While nextBit < biggestBit AND
bitBuilder < bitMask AND NOT maskFound
nextBit *= 2
bitBuilder += nextBit
If filterMask = nextBit OR
filterMask = bitBuilder Then
maskFound = True
ElseIf filterMask < nextBit Then
Exit Loop
End If
Loop

Return maskFound

End Function

***Important: Notice that the above only test for a sequential sum
filterMask. In other words it will catch filterMask values like;
1+2+4 or generally 1+2+4+...+2^m
However it will not catch filterMask values like;
1+8 (2 and 4 ommited) or generally
1+...+2^n +2^(n+x) +2^(n+x+1) +...+2^m
n<m, x>0, (n+x)<m
even though these filter mask do exist in a bitMask of 1+2+4+8 and
1+2+...+2^m. For this you need to take into account the combinations of
n
bit positions choosing 1 bit to n bits for n in [1, m]. You can use the
above with a few modification in the loop to easily test for these cases.
You will also need to code a simple combinations function. I have not
done
this here since your question does not seem to need to check
non-sequential
sum filterMask. I just thought that you be aware of the limitations of
the
solution.

--Robby

"Andy" <po********@yahoo.com.hk> wrote in message
news:9d**************************@posting.google.c om...
> Sigh... working on it for whole day and got no idea...
>
> Basically I want to have a bitmask filtering function, I will throw in
> 3 parameters:
>
> bitMaskValue - current bitmask value
> filterValue - the value I want to see if its exist in "bitMaskValue"
> BiggestValue - the maximum individual value can be exist
>
> And I want it just to return true if the value exist, or false if not
>
> So for example, I have 7 as "bitMaskValue", "filterValue" as 2 and
> "BiggestValue" as 4... then it will return True... as 1 + 2 + 4 = 7
>
> If I have 5 as "bitMaskValue", "filterValue" as 2 and "BiggestValue"
> as 4... then it will return False... as 1 + 4 = 5... no "2"
>
> I have come up the following be it just don't work :(
>
> Private Function BitMaskFilter(ByVal bitMaskValue As Int32, ByVal
> filterValue As Int32, ByVal BiggestValue As Int32) As Boolean
> ' Filter "filterValue" from "bitMaskValue" and return true if
> value
> ' BiggestValue = biggest individual BitMask value
> Dim filed As Boolean = False
> While bitMaskValue > 1 And BiggestValue > 0
> If bitMaskValue >= BiggestValue And BiggestValue <>
> filterValue Then
> bitMaskValue = bitMaskValue - BiggestValue
> End If
> If BiggestValue = filterValue Then
> filed = True
> End If
> BiggestValue = BiggestValue / 2
> End While
> If filed Then
> Return bitMaskValue = filterValue
> Else
> Return bitMaskValue = 0
> End If
>
> End Function
>
> If I have the maximum possible value (in this case, 7) then it's ok...
> if I have a bit missing in between the function will fail... I have
> tried so many approach but I just can't think through it... any help
> would be appericiated.
>
> Thanks in advance.

Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Case Nelson | last post by:
Hi there I've just been playing around with some python code and I've got a fun little optimization problem I could use some help with. Basically, the program needs to take in a random list of no...
1
by: Andrey Brozhko | last post by:
Hi I need to represent custom type system (ints, bytes, chars, enums, bitmasks, arrays and some other types) in xml. It is easy to see how to represent enums in xml (using xs:enumeration), the...
3
by: Tommy | last post by:
I have been working on getting nested repeaters to work for several days. When I add a repeater inside my item template i cannot see it in my code and can't set the datasource for it, etc, thus...
3
by: ASP .NET Newbie | last post by:
Hey everyone, I have an application that I am developing (well, that's obvious!). The application minimizes to the system tray when it is minimized. I have a function called Show_Click() that...
3
by: Mark Allison | last post by:
Hi, Total newbie here, I'm trying to examine a bitmask which is passed into my app as a string. I convert it to int, and try and process it. I got the following code: //dbStatus current is a...
0
by: Xah Lee | last post by:
In this article, i explain how the use of bit masks is a hack in many imperative languages. Often, a function will need to take many True/False parameters. For example, suppose i have a function...
3
by: Patrick | last post by:
Hi there, I have a simple problem to solve but somehow I am too stupid to figure out where is the error. Basically, I wanna shift each element of the array by an amount of n, whereas the n...
3
by: TD | last post by:
Is there a way to DataBind controls to a specific Bit in Bitmask? Here is some sample code of what I am trying to do ... I am trying to bind the Visible and Enabled properties of a Button to...
10
by: lithiumcat | last post by:
Hi, This question seems to come up quite often, however I haven't managed to find an answer relevant to my case. I often use binary flags, and I have alaways used a "bitmask" technique, that...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.