473,554 Members | 3,241 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A way to bitwise AND two int's

I'm writing a map generator and I'm looking for a way to keep track of the
various terrain. I had a thought of using bitboards but I can't find a way
to bitwise AND two int's together and get a bool.

How do I get the proper response from:
IF (0x01 & 0x05)

My only recourse so far is to use the full if syntax:
IF ((0x01 & 0x05) > 0)

Is that any faster or slower than the first way?
Is this really the only way to do this?

Thanks for the help,
Tom P.
Dec 15 '05 #1
21 1925

Henry Padilla wrote:
I'm writing a map generator and I'm looking for a way to keep track of the
various terrain. I had a thought of using bitboards but I can't find a way
to bitwise AND two int's together and get a bool.

How do I get the proper response from:
IF (0x01 & 0x05)
What's "the proper response" ? If you bitwise AND two int's, you get an
int. Which ints do you want to count as 'true' ?

My only recourse so far is to use the full if syntax:
IF ((0x01 & 0x05) > 0)
Oh, you want positive ints to count as true. Well, you seem to have
solved your problem!
Is that any faster or slower than the first way?
The first way doesn't compile, so is infinitely slow.
Is this really the only way to do this?


There are usually many ways to do things in C#. But if you want to see
if two ints have any set bits in common, this seems the obvious way.

--
Larry Lard
Replies to group please

Dec 15 '05 #2


"Henry Padilla" wrote:
I'm writing a map generator and I'm looking for a way to keep track of the
various terrain. I had a thought of using bitboards but I can't find a way
to bitwise AND two int's together and get a bool.

How do I get the proper response from:
IF (0x01 & 0x05)

My only recourse so far is to use the full if syntax:
IF ((0x01 & 0x05) > 0)

Is that any faster or slower than the first way?
Is this really the only way to do this?


C# enforces stricter typing than C/C++ do, the first won't compile because
bools and ints are diffrent types. Did you try casting the result?

if ((bool)(0x01 & 0x05))

Don't know if it'll be allowed or not. IF not you'll have to do some
variant of the 2nd.

PS despite the C syntax, c# casting is typesafe.
Dec 15 '05 #3

"Larry Lard" <la*******@hotm ail.com> wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.com...

Henry Padilla wrote:
I'm writing a map generator and I'm looking for a way to keep track of
the
various terrain. I had a thought of using bitboards but I can't find a
way
to bitwise AND two int's together and get a bool.

How do I get the proper response from:
IF (0x01 & 0x05)
What's "the proper response" ? If you bitwise AND two int's, you get an
int. Which ints do you want to count as 'true' ?


As defined by Kernighan and Ritchie (in 1970 or so) any non-zero is true. I
thought this was pretty well accepted in the programming community at large,
are you being snide and obtuse for a reason? Or do you not know how to use
a simple bitmask and are taking it out on me?

My only recourse so far is to use the full if syntax:
IF ((0x01 & 0x05) > 0)
Oh, you want positive ints to count as true. Well, you seem to have
solved your problem!


Yet yours seems to linger. If you read the second sentence in my post you
will see I am trying to get a bool. The part that says "bitwise AND two
int's together and get a bool" was a dead giveaway. This is quite a simple
opperation in C/C++And don't see why it needs to be complicated.
Is that any faster or slower than the first way?
The first way doesn't compile, so is infinitely slow.


This does not address the question of any ways faster than this. Nor why
you insist on these side quests to deride a simple question. Do you just
not know the answer?

Is this really the only way to do this?


There are usually many ways to do things in C#. But if you want to see
if two ints have any set bits in common, this seems the obvious way.


And you had to go through all that to tell me this? Man, it's a good thing
I didn't ask a difficult question, it would have taken months of side notes.

In as much as you have helped not at all, thank you. In the future, if this
is the best you can do please keep your misguided attempts at comedy to
yourself. You are, in point of fact, not good at it.

Tom P.
Dec 15 '05 #4

"Dan Neely" <Da******@discu ssions.microsof t.com> wrote in message
news:B8******** *************** ***********@mic rosoft.com...


"Henry Padilla" wrote:
I'm writing a map generator and I'm looking for a way to keep track of
the
various terrain. I had a thought of using bitboards but I can't find a
way
to bitwise AND two int's together and get a bool.

How do I get the proper response from:
IF (0x01 & 0x05)

My only recourse so far is to use the full if syntax:
IF ((0x01 & 0x05) > 0)

Is that any faster or slower than the first way?
Is this really the only way to do this?
C# enforces stricter typing than C/C++ do, the first won't compile because
bools and ints are diffrent types. Did you try casting the result?

if ((bool)(0x01 & 0x05))


Yes, it still won't do it. I finally found, after searching the depths of
the unhelpful file, that C# does not support "non-zero true".

The only way to do it is the 2nd way I described.

Don't know if it'll be allowed or not. IF not you'll have to do some
variant of the 2nd.

PS despite the C syntax, c# casting is typesafe.


Thanks again for the help.
Tom P.
Dec 15 '05 #5
Henry Padilla wrote:
Yes, it still won't do it. I finally found, after searching the depths of
the unhelpful file, that C# does not support "non-zero true".


One pleasant side effect of this rule is that most errors of this form
won't compile:

if (value = 15) { ... }

Jesse

Dec 15 '05 #6
Ignoring the "humour" (sorry Henry, but despite your protestations Larry's
comments did raise a chuckle), your "solution" is not quite correct to the
pedant:

you've written:

"if ((0x01 & 0x05) > 0) {}"

and complained that

"As defined by Kernighan and Ritchie (in 1970 or so) any non-zero is true."

That being the case, you should probably be testing with !=0

Marc
"Henry Padilla" <pa******@hotma il.com> wrote in message
news:ei******** *****@TK2MSFTNG P12.phx.gbl...

"Larry Lard" <la*******@hotm ail.com> wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.com...

Henry Padilla wrote:
I'm writing a map generator and I'm looking for a way to keep track of
the
various terrain. I had a thought of using bitboards but I can't find a
way
to bitwise AND two int's together and get a bool.

How do I get the proper response from:
IF (0x01 & 0x05)


What's "the proper response" ? If you bitwise AND two int's, you get an
int. Which ints do you want to count as 'true' ?


As defined by Kernighan and Ritchie (in 1970 or so) any non-zero is true.
I thought this was pretty well accepted in the programming community at
large, are you being snide and obtuse for a reason? Or do you not know
how to use a simple bitmask and are taking it out on me?

My only recourse so far is to use the full if syntax:
IF ((0x01 & 0x05) > 0)


Oh, you want positive ints to count as true. Well, you seem to have
solved your problem!


Yet yours seems to linger. If you read the second sentence in my post you
will see I am trying to get a bool. The part that says "bitwise AND two
int's together and get a bool" was a dead giveaway. This is quite a
simple opperation in C/C++And don't see why it needs to be complicated.
Is that any faster or slower than the first way?


The first way doesn't compile, so is infinitely slow.


This does not address the question of any ways faster than this. Nor why
you insist on these side quests to deride a simple question. Do you just
not know the answer?

Is this really the only way to do this?


There are usually many ways to do things in C#. But if you want to see
if two ints have any set bits in common, this seems the obvious way.


And you had to go through all that to tell me this? Man, it's a good
thing I didn't ask a difficult question, it would have taken months of
side notes.

In as much as you have helped not at all, thank you. In the future, if
this is the best you can do please keep your misguided attempts at comedy
to yourself. You are, in point of fact, not good at it.

Tom P.

Dec 15 '05 #7

Henry Padilla wrote:
"Larry Lard" <la*******@hotm ail.com> wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.com...

Henry Padilla wrote:
I'm writing a map generator and I'm looking for a way to keep track of
the
various terrain. I had a thought of using bitboards but I can't find a
way
to bitwise AND two int's together and get a bool.

How do I get the proper response from:
IF (0x01 & 0x05)
What's "the proper response" ? If you bitwise AND two int's, you get an
int. Which ints do you want to count as 'true' ?


As defined by Kernighan and Ritchie (in 1970 or so) any non-zero is true. I
thought this was pretty well accepted in the programming community at large,
are you being snide and obtuse for a reason?


I must apologise for the excessive snideness, it's been a trying day. I
am usually much less snide (some, but not so much). And I try to avoid
all snideness when, unlike here, actual education is in order. You
clearly know what you are doing, so I went without the didacticism in
favour of a more jocular approach.

I will restrict myself to one final observation: K&R were talking about
C. C# is not C, and one of the differences is that implicit narrowing
conversions (such as from int to bool) are not allowed (compiler error
CS0029, if you want to look it up). Now, that last sentence would
probably have sufficed as an initial response. But it wouldn't have
been half so much fun.
Tom P.


And what have you done with Henry!?

--
Larry Lard
Replies to group please

Dec 15 '05 #8

"Larry Lard" <la*******@hotm ail.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .

Henry Padilla wrote:
"Larry Lard" <la*******@hotm ail.com> wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.com...
>
> Henry Padilla wrote:
>> I'm writing a map generator and I'm looking for a way to keep track of
>> the
>> various terrain. I had a thought of using bitboards but I can't find
>> a
>> way
>> to bitwise AND two int's together and get a bool.
>>
>> How do I get the proper response from:
>> IF (0x01 & 0x05)
>
> What's "the proper response" ? If you bitwise AND two int's, you get an
> int. Which ints do you want to count as 'true' ?


As defined by Kernighan and Ritchie (in 1970 or so) any non-zero is true.
I
thought this was pretty well accepted in the programming community at
large,
are you being snide and obtuse for a reason?


I must apologise for the excessive snideness, it's been a trying day. I
am usually much less snide (some, but not so much). And I try to avoid
all snideness when, unlike here, actual education is in order. You
clearly know what you are doing, so I went without the didacticism in
favour of a more jocular approach.

I will restrict myself to one final observation: K&R were talking about
C. C# is not C, and one of the differences is that implicit narrowing
conversions (such as from int to bool) are not allowed (compiler error
CS0029, if you want to look it up). Now, that last sentence would
probably have sufficed as an initial response. But it wouldn't have
been half so much fun.
Tom P.


And what have you done with Henry!?


Henry Thomas Padilla

And thank you for the apology. With 4-8 inches of snow and a 34 mi drive
home I can definitely sympathize with the "hard day".

I hope it gets better.
Tom P.
Dec 15 '05 #9

"Marc Gravell" <mg******@rm.co m> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
Ignoring the "humour" (sorry Henry, but despite your protestations Larry's
comments did raise a chuckle), your "solution" is not quite correct to the
pedant:

you've written:

"if ((0x01 & 0x05) > 0) {}"

and complained that

"As defined by Kernighan and Ritchie (in 1970 or so) any non-zero is
true."

That being the case, you should probably be testing with !=0

Marc


Touché (with a little mark above the "e". I can never get those marks to
work right... Hey, the spell checker did it for me, cool.)

Anyway, you are absolutely correct and thank you for the observation.
Indeed if the high order bit is set this would result in a negative number
to the compiler but not result in a "true" condition. Since this situation
is in deference to bitmasking the high-order bit is just that, another bit.
Sign is insignificant.

That may well have driven me nuts for an hour or so. Thank you.

Tom P.

P.S. Knowing the comments were made in a "bad-day jocular" manner I can see
the humor. I'm sorry I was taken by surprise at first.
Dec 15 '05 #10

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

Similar topics

8
4586
by: J. Campbell | last post by:
When the bitwise NOT operator, is placed in front of an integer variable type (bool, char, short, int, long), the return value is a signed int, regardless of the variable type. An example can be seen in this code: #include<iostream> using namespace std; int main(){
34
16823
by: Christopher Benson-Manica | last post by:
I'm trying to compute the absolute value of an integer using only bitwise operators... int x; sscanf( "%d", &x ); printf( "%d\n", (x^((~((x>>31)&1))+1)) + ((x>>31)&1) ); That works, but it assumes 32 bit integers. Is there a portable/standard way to do this? Or are ANSI integers always 32 bits? If not, is using sizeof(int) * 8 - 1 as...
9
2480
by: Johnathan Doe | last post by:
Hi, I am having problems understanding bitwise idioms that I see frequently in source code. I understand that they work at the individual bit level and that 0 | 1 = 1, 1 & 1 = 1 and so on (understand what all the operators mean), but when I try bit banging code it inevitably fails to get the result I wanted. I am trying to put together...
9
7020
by: Christopher Weaver | last post by:
I know that the bitwise AND of 8 and 4 will return 0 or false and the bitwise AND of 8 and 9 will return 1 or true but I don't know how to write the synax for it in C#. I have a value that ranges from 0 to 15 and I need to compare it to 15 in order to find if it contains the values 1, 2, 4, or 8. To represent it more graphically, the value...
3
12485
by: shdwsclan | last post by:
I am native to various languages but bitwise operators just kill me. I see how much I take object oriented languages for granted. I like all the other c derivitives but ANSI C is making me loose my hair....especially ANSI C's bitwise operators..... For reference i come from the (Java/C#/C++) realm and was never forced to use these. Many people...
3
13136
by: Marc | last post by:
I'm trying to set the first three bits to zero on a byte. For some reason, the compiler is casting the number peculiarly when I use the bitwise complement operator. Here's what I think should work but doesn't: byteArray &= ~0xe0; // set the first three bits to 0 (11100000 == 0xe0) The error is "Constant value '-225' cannot be converted...
45
5246
by: Carramba | last post by:
Hi! I now that I can't do straight forward any bitwise operation on float (double etc..). But I wondering what is the easiest/best way to do this? I was thinking if I have float x=1.1111 so I can multiple it by 1000 to get 11111 and the preform bitwise like <<2 to get 88888 and then divide by 1000 to go back to float 8.8888. but these seem...
29
5921
by: Carl Banks | last post by:
Anyone with me here? (I know the deadline for P3 PEPs has passed; this is just talk.) Not many people are bit-fiddling these days. One of the main uses of bit fields is flags, but that's not often done in Python because of keyword arguments and dicts, which are lot more versatile. Another major use, talking to hardware, is not something...
11
2175
by: Jordan | last post by:
I am trying to rewrite some C source code for a poker hand evaluator in Python. Putting aside all of the comments such as just using the C code, or using SWIG, etc. I have been having problems with my Python code not responding the same way as the C version. C verison: unsigned find_fast(unsigned u) { unsigned a, b, r;
0
7516
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7787
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8029
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7551
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6131
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5428
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5147
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2012
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.