Number and bit

  • Thread starter Thread starter diego
  • Start date Start date
D

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
 
diego said:
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
 
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
 
diego said:
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.
 
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


| 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
|
 
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
 
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
 
diego said:
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
 
Back
Top