-1

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

  • 2
    You can use Java type `long`, which is 64 bits wide. That it is a signed integer type would be an issue in C or C++, but it should not present a problem in Java. For example, `1L << 63` is well-defined in Java. – John Bollinger May 17 '23 at 19:27
  • Side note: there is one unsigned primitive type in Java: `char` is a 16-bit unsigned integer. Not that I think that helps you. – John Bollinger May 17 '23 at 19:33
  • 2
    There are also classes like [`BitSet`](https://docs.oracle.com/javase/8/docs/api/java/util/BitSet.html) that can help when your goal is to have an easier time. If you want to keep memory space to a minimum it's probably better to go with sometimes slightly awkward bit operations. E.g. keep in mind that java automatically widens `0xFF` to a full 32bit int – zapl May 17 '23 at 19:37
  • 1
    Are you actually doing bit operations? Otherwise you should use objects that better represent the domain of the problem. You have more tools for that in Java than in C, like objects, enums. – aled May 17 '23 at 19:40
  • Like @aled suggests… Enums in Java are much more powerful and flexible than seen in most other languages. An enum in Java can have state (member fields), constructors, and methods. So each enum object per square can store its color, and store what piece rests on it, if you so choose. And `EnumSet` & `EnumMap` can very efficiently store a collection of enum objects. – Basil Bourque May 17 '23 at 19:46
  • I wouldn't tend to use enums here, if only because chessboards are best expressed as grids with numeric coordinates. There's specific advantage to be gained by sticking to numbers. – Louis Wasserman May 17 '23 at 19:47
  • That long is signed, is problem in Java for me, because I'm not able to handle with the last bit properly, or at least I personely don't know how to manage it. – Radoslav Khun May 17 '23 at 21:31
  • I am doing bit operations, that's the point why to use bitboards, there is a far faster moves generator and so on. – Radoslav Khun May 17 '23 at 21:33
  • The last bit works exactly like all the other bits, you just have to use `>>>` instead of `>>`. – Louis Wasserman May 17 '23 at 22:12
  • Off-topic: Although Java doesn't have an `unsigned` type, it does have some support. Check out the methods in the [Long API](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Long.html) . There is a brief [bitwise operators tutorial](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html) . – Old Dog Programmer May 18 '23 at 00:45

1 Answers1

3

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.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413