1

So im making a chess engine in java, which involved a lot of bit operations, and I have been looking at some C code for some inspiration. In the code, he uses the print statement:

        for (int rank = 0; rank < 8; rank++){
        for (int file = 0; file < 8; file++) {

            int square = rank * 8 + file;
            printf("%d", (bitboard & (1ULL << sqaure)) ?1 :0 );

        }
    } 

The whole point of this method is that it loops through a long with 64 bits in it, and prints out an 8x8 square to represent a chessboard, with 1s for taken squares, and 0s for empty squares. Now I am familiar with everything in this code block, except 1ULL. What does it represent? Is there a way I can use this in java, or do I not even need to worry about it?

  • 1
    Java does not have `unsigned long long` as a type. – Elliott Frisch Feb 27 '21 at 01:42
  • 2
    That said, a java [long](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html) is 64-bits, and java does have bit operations. So you can certainly port that code to java (as the code is only looking at the bits in the long, doesn't matter that it is signed). What issue are you having with doing that? – geocodezip Feb 27 '21 at 01:52
  • The question that you *should* have asked is "what does 1ULL mean in C". Once you know what it means in C, the translation to Java is straightforward ... – Stephen C Feb 27 '21 at 02:11
  • Should be closed as a dup of https://stackoverflow.com/questions/17795722/ – Stephen C Feb 27 '21 at 02:13
  • Unrelated, but I fail to see (much) value in representing a chess board as a collection of empty/occupied spaces. Since you'll already have a *meaningful* representation of the board anyway why not just use that? – Dave Newton Feb 27 '21 at 05:55
  • 1
    @DaveNewton it's used for chess algorithms, for example [the `o^(o-2r)` trick](https://www.chessprogramming.org/Subtracting_a_Rook_from_a_Blocking_Piece). – harold Feb 27 '21 at 09:29
  • @harold *googles* Ah, ok. – Dave Newton Feb 27 '21 at 12:32

1 Answers1

2

1ULL is an unsigned long long - probably 64-bit. It means: The number '1', but as an unsigned long long. The unsigned part doesn't actually appear to matter, so, just.. 1L << square would do the job. (java also has this postfix letter thing, but only F, D, and L, for float, double, and long).

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72