473,413 Members | 1,700 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,413 software developers and data experts.

Number and bit

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

Mar 21 '06 #1
8 1169
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
Mar 21 '06 #2
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" <di******@yahoo.com> escribió en el mensaje
news:11**********************@z34g2000cwc.googlegr oups.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

Mar 21 '06 #3
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.

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
Mar 21 '06 #4
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
"diego" <di******@yahoo.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.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
|
Mar 21 '06 #5
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

Mar 21 '06 #6
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" <di******@yahoo.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.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

Mar 21 '06 #7
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
Mar 21 '06 #8
thanks guys for all your posts. you've been of immense help.

diego

Mar 22 '06 #9

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

Similar topics

3
by: Shay Hurley | last post by:
this is probably a stupid question so apologies in advance. I am trying to format a number to look like a phone number with "-"'s between the numbers etc e.g. 15554256987 should be formatted as...
8
by: EAS | last post by:
Hey, I'm new to python (and programming in general) so I'll prolly be around here a lot... Anyways, I've found out how to make a "guess my number game" where the player guesses a number between...
11
by: don | last post by:
Ok, this is a homework assignment, but can you help me out anyway...... I need a routine for figuring out if a number inputted by the user is a prime number or not...... all I'm asking for is Not...
27
by: Gaijinco | last post by:
Sooner or later everytime I found recreational programming challenges I stumble with how I test if a number is has decimal places differnt than 0? For example if I want to know if a number is a...
13
by: Ron | last post by:
Hi all I'm deciding whether to use the PK also as an account number, invoice number, transaction number, etc that the user will see for the respective files. I understand that sometimes a...
19
by: gk245 | last post by:
Trying to write a program that will figure out if a number is perfect or not. Here is my logic: 1) Read in the number 2) Split it up (number - 1) 3) Put all the split up numbers into an...
4
by: SweetLeftFoot | last post by:
Hello, i have designed some code that works out the first 250 prime numbers and prints them to the screen. However i need to implement 2 functions, one of which returns a 1 if the number is a prime...
4
by: fatimahtaher | last post by:
Hi, I am supposed to create a program that generates a random number and then asks the user to guess the number (1-100). The program tells the user if he guessed too high or too low. If he...
5
by: silversnake | last post by:
I'm trying to write a program that take a input number and prints if is a prime numbers but is not working for instance, it says that 4 is prime while 5 is not. can anyone see what the problem is ....
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.