一种按位和两个int的方法 [英] A way to bitwise AND two int's

查看:95
本文介绍了一种按位和两个int的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个地图生成器,我正在寻找一种方法来跟踪各种地形的各种地形。我想到了使用位板,但我找不到一种方式

按位和两个int'一起得到一个bool。


我如何从以下方面得到正确的答复:

IF(0x01& 0x05)


到目前为止我唯一的办法是使用完整的if语法:

IF((0x01& 0x05)> 0)


是否比第一种方式更快或更慢?

这真的是唯一的方法吗?


感谢您的帮助,

Tom P.

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.

推荐答案



Henry Padilla写道:

Henry Padilla wrote:
我正在写一个地图生成器,我正在寻找一种方法来跟踪各种地形。我曾经想过要使用位板,但是我找不到按顺序和两个int'的方式并且得到一个bool。

我如何得到正确的响应:
IF(0x01& 0x05)


什么是'正确答案' ?如果按位和两个int',你得到一个

int。你想把哪些内容算作真实?

到目前为止,我唯一的办法是使用完整的if语法:
IF((0x01& 0x05)> 0 )


哦,你想要积极的整数算作真实。好吧,你似乎已经解决了你的问题了吗?

这比第一种方式更快还是更慢?


第一种方式无法编译,因此速度非常慢。

这真的是唯一的方法吗?
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?




在C#中通常有很多方法可以做。但是如果你想看看

如果两个整数有任何共同的设置位,这似乎是显而易见的方式。


-

Larry Lard

回复团体请



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





" Henry Padilla的"写道:


"Henry Padilla" wrote:
我正在写一个地图生成器,我正在寻找一种方法来跟踪各种地形。我曾经想过要使用位板,但是我找不到按顺序和两个int'的方式并且得到一个bool。

我如何得到正确的响应:
IF(0x01& 0x05)

到目前为止,我唯一的办法是使用完整的if语法:
IF((0x01& 0x05)> 0)<这是否比第一种方式更快或更慢?
这真的是唯一的方法吗?
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比C / C ++更严格的打字,第一个不会编译因为

bools和int是不同的类型。你尝试过投射结果吗?


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


不知道是不是是否被允许。如果不是你将不得不做一些

变种的第二个。


PS尽管有C语法,但c #casting是类型安全的。



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.




" Larry Lard" < LA ******* @ hotmail.com>在消息中写道

news:11 ********************* @ o13g2000cwo.googlegro ups.com ...

"Larry Lard" <la*******@hotmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...

Henry Padilla写道:

Henry Padilla wrote:
我正在写一个地图生成器,我正在寻找一种方法来跟踪
各种各样的地形。我想到了使用位板,但我找不到一个
的方式来按位和两个int'一起来得到一个bool。

我怎么得到适当的回应来自: IF(0x01& 0x05)
什么是正确的反应? ?如果你按位和两个int',你得到一个
int。你想把哪个整数算作''true''?
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'' ?




根据Kernighan和Ritchie的定义(1970年左右),任何非零都是正确的。我认为这在整个编程社区已经被广泛接受了,

你是不是因为某种原因而嗤之以鼻?或者你不知道如何使用

a简单的位掩码并将它取出来?



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?


我到目前为止唯一的办法是使用完整的if语法:
IF((0x01& 0x05)> 0)
哦,你想要积极的整数计为真。好吧,你似乎已经解决了你的问题!

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!




然而你的似乎仍然存在。如果你在我的帖子中读到第二句话你将会看到我想要一个布尔。这个部分说按位和两个

int在一起并得到一个bool是一个死的赠品。这是一个非常简单的用于C / C ++的
操作并且不知道为什么它需要复杂。



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?



在C#中通常有很多方法可以做。但是如果你想看看
如果两个整数有任何共同的设置位,这似乎是显而易见的方式。



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.




你必须经历所有这一切告诉我这个?男人,这是件好事

我没有问过一个困难的问题,这需要几个月的旁注。


尽可能多因为你根本没有帮助,谢谢你。在将来,如果这个

是你能做到的最好的,请把你误导的喜剧尝试留给你自己的b $ b。事实上,你并不擅长。


Tom P.



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.


这篇关于一种按位和两个int的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆