Bitmaps/bitboards在Java中

huangapple go评论85阅读模式
英文:

Bitmaps/bitboards in Java

问题

我正在用Java编写一个国际象棋引擎,我想将所有棋子的位置表示为一个名为位图或位板的变量。在C++中,我可以简单地使用unsigned long或称为uint64_t。但是因为在Java中没有无符号变量(据我所知),有什么高效的方法可以实现这个方法呢?

我尝试了我能想到的一切。

英文:

I'm programming a chess engine in Java and I would like to represent all pieces positions in one variable called bitmap or bitboard. In C++ I would simply use unsigned long, or so-called uint64_t. But because there are no unsigned variables in Java (as far as I know), what is some efficient way, how to implement this method?

I tried everything I was able to think of

答案1

得分: 3

Signedness(有符号性)实际上对于使用数字作为位掩码并没有显著的影响,至少在Java中是这样。

你可以很好地使用long,即使它是有符号的。1L << 63有效。只要注意在向右移位时,你将想要使用>>>而不是>>

请注意,如果你想存储超过64位的数据,你可能希望使用java.util.BitSet,它专门设计为一个根据需要扩展的位图。

英文:

Signedness doesn't actually make a significant difference to using numbers as bit masks -- at least, not in Java.

You can use a long just fine, even though it is signed. 1L &lt;&lt; 63 works. Just note that you will want to use &gt;&gt;&gt; instead of &gt;&gt; to when shifting your mask right.

Note that if you want to store more than 64 bits, you may wish to use java.util.BitSet, which is designed specifically as a bitmap that scales as necessary.

huangapple
  • 本文由 发表于 2023年5月18日 03:24:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76275571.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定