The bitops module, for what I have seen, does not include a bit iterator, which would be convenient (chess programmers widely use bitboards, which are nothing else than an unsigned long with 64 logical properties linked to the 64 squares of a chessboard).
I use this:
import bitops
type bb=uint64
type square=int32
iterator bitIter(b:bb) : square {.inline.} =
var x:bb=b
while x!=0 :
var fb=firstsetBit(x)-1
yield square(fb)
x.clearMask(b1 shl fb)
So, if anyone has produced a better solution, I would be happy to read it. And if my code happens to be correct, then I suggest to adapt/generalize it and to make it available within the bitops module.
I use this method: https://lemire.me/blog/2018/02/21/iterating-over-set-bits-quickly/
here: https://github.com/brentp/slivar/blob/master/src/slivarpkg/fastsets.nim#L20-L24
in implementing a faster (iterator) version of set subtraction.
Well, regarding java... efficiency is lower than C or Nim... I would say 20% for CPU intensive apps, not more. The memory consumption is also excessive in Java. But conceptualization is convenient. Here is an example of java bit iteration (cryptic but working):
protected static final void bitIter(long _uu,Consumer<Long> action)
{if (_uu==0L) return;
long _xx=_uu & -_uu;
while (_xx!=0L) {action.accept(_xx);_uu-=_xx;_xx=_uu & -_uu;}}
protected static final int log2(long _X) {return 63 - Long.numberOfLeadingZeros(_X);}
bitIter(node._P, _RCS -> {int rcs=log2(_RCS);});
I would say 20%
That is surprising for me. I would have assumed that Java is not a good choice for a chess engine using (rotated) bitboard representation. As Java uses most of the time reference semantic with indirection, not value semantic. And as Java was not really designed initially for low level bit operations, metaprogramming or using macros like C++ does. (I saw some bitboard C++ code twenty years ago, I think was from Prof. Hyatt but can not really remember his name. Was called rotated-bitboards.)