英文:
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 << 63
works. Just note that you will want to use >>>
instead of >>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论