Home All Groups Group Topic Archive Search About
Author
21 Mar 2006 8:40 AM
diego
hi all,

255 and 1 = 1
255 and 2 = 2

256 and 1 = 0

how can i get the coorect result for values greater than 256

i hope you understand what i'm getting at.

thanks in advance

diego

Author
21 Mar 2006 9:03 AM
Andrew Morton
diego wrote:
> hi all,
>
> 255 and 1 = 1
> 255 and 2 = 2
>
> 256 and 1 = 0
>
> how can i get the coorect result for values greater than 256
>
> i hope you understand what i'm getting at.

It would help if you told us what you want: what do you consider to be a
correct result?

Perhaps if you wrote down the numbers in binary you would see what's
happening.

Andrew
Author
21 Mar 2006 10:21 AM
Carlos J. Quintero [VB MVP]
256 in binary is 1 0000 0000 so it won´t work as you want, whatever you are
trying to do... maybe a Mod function?

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com



Show quoteHide quote
"diego" <diego***@yahoo.com> escribió en el mensaje
news:1142930439.335056.314940@z34g2000cwc.googlegroups.com...
> hi all,
>
> 255 and 1 = 1
> 255 and 2 = 2
>
> 256 and 1 = 0
>
> how can i get the coorect result for values greater than 256
>
> i hope you understand what i'm getting at.
>
> thanks in advance
>
> diego
>
Author
21 Mar 2006 12:20 PM
C-Services Holland b.v.
diego wrote:
Show quoteHide quote
> hi all,
>
> 255 and 1 = 1
> 255 and 2 = 2
>
> 256 and 1 = 0
>
> how can i get the coorect result for values greater than 256
>
> i hope you understand what i'm getting at.
>
> thanks in advance
>
> diego
>

All those results are correct with 256 the bit that signifies 1 is not
set. What is the problem?  Unless you're misunderstanding bitwise
comparison.


--
Rinze van Huizen
C-Services Holland b.v
Author
21 Mar 2006 1:08 PM
Jay B. Harlow [MVP - Outlook]
Diego,
In addition to the other comments:

| how can i get the coorect result for values greater than 256

Define "correct result", as the example you show is "correct" (see Carlos'
example for why).

Try the following:

        For value As Integer = 246 To 266
            Debug.WriteLine(value And 1, Convert.ToString(value, 2) & " and
1")
            Debug.WriteLine(value And 2, Convert.ToString(value, 2) & " and
2")
        Next

Remember the And operator does bitwise anding with ordinal (integer) values,
not logical anding. If you want logical Anding you need to convert the
operarands to Boolean values first...


--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Show quoteHide quote
"diego" <diego***@yahoo.com> wrote in message
news:1142930439.335056.314940@z34g2000cwc.googlegroups.com...
| hi all,
|
| 255 and 1 = 1
| 255 and 2 = 2
|
| 256 and 1 = 0
|
| how can i get the coorect result for values greater than 256
|
| i hope you understand what i'm getting at.
|
| thanks in advance
|
| diego
|
Author
21 Mar 2006 2:44 PM
diego
hi all,

thanks for responding.

here's what i want to do. i have a list of items (a1,a2,a3,... an)
where n would be <= 20. from this list i want to filter out which items
are returned using a value.

so if i have 4 items a1,a2,a3,a4 and a value of 6, only a2 and a3 would
be displayed. how can i dow this with long lists?

is this the correct approach or are there other much better approaches?
if so, can you please point me to the right direction.

thanks in advance

diego
Author
21 Mar 2006 4:59 PM
Enfor
Here is what I think you're looking for.  The last section loops through
each item and returns the items from the list where the bit in value is 1.
Sorry, I don't have dot net installed at work so I did it in VB6.


    Dim items(1 To 20) As Long
    Dim value As Long
    Dim txt$
    Dim bit As Integer


    'generate some random numbers for the items
    value = Rnd * (2 ^ 20)
    For bit = 1 To 20
        items(bit) = Rnd * 100
    Next bit



    txt = ""
    For bit = 1 To UBound(items)
        If value And 2 ^ (bit - 1) Then
            txt = txt & items(bit) & "  "
        End If
    Next bit
    MsgBox txt







Show quoteHide quote
"diego" <diego***@yahoo.com> wrote in message
news:1142952266.576292.258380@i39g2000cwa.googlegroups.com...
> hi all,
>
> thanks for responding.
>
> here's what i want to do. i have a list of items (a1,a2,a3,... an)
> where n would be <= 20. from this list i want to filter out which items
> are returned using a value.
>
> so if i have 4 items a1,a2,a3,a4 and a value of 6, only a2 and a3 would
> be displayed. how can i dow this with long lists?
>
> is this the correct approach or are there other much better approaches?
> if so, can you please point me to the right direction.
>
> thanks in advance
>
> diego
>
Author
21 Mar 2006 5:27 PM
Andrew Morton
diego wrote:
> here's what i want to do. i have a list of items (a1,a2,a3,... an)
> where n would be <= 20. from this list i want to filter out which
> items are returned using a value.
>
> so if i have 4 items a1,a2,a3,a4 and a value of 6, only a2 and a3
> would be displayed. how can i dow this with long lists?

It would have been more helpful to have given an example which is not
symmetrical like that.

(assuming you have a TextBox1 to display the results in)

TextBox1.Clear()
Dim s As Integer() = {123, 234, 345, 456, 567}
Dim filter As Integer = 23  ' 10111
Dim n As Integer = 1
For i As Integer = 0 To UBound(s)
    If n And filter Then TextBox1.AppendText(s(i).ToString & " ")
    n = 2 * n
Next

Displays: 123 234 345 567

Andrew
Author
22 Mar 2006 12:26 AM
diego
thanks guys for all your posts. you've been of immense help.

diego