integer function right_most_zero(i, n_orbs)
! gives the position of the right most zero with atleast one 1
! right of it in an integer bit representation, up to a input
! dependent orbital n_orbs
integer(n_int), intent(in) :: i
integer, intent(in) :: n_orbs
integer, allocatable :: nZeros(:), nOnes(:)
integer(n_int) :: j
! first truncate the integer to be save..
! set all the ones left of n_orbs to 0
j = iand(i, int(maskr(n_orbs), n_int))
! be sure to avoid the edge case:
if (j == 0_n_int) then
right_most_zero = -1
return
end if
! i could misuse the decode_bit_det functionality
allocate(nZeros(popcnt(not(j))))
allocate(nOnes(popcnt(j)))
! but exclude the unnecessary 0 left of n_orbs..
call decode_bit_det(nZeros, [not(j)])
call decode_bit_det(nOnes, [j])
! in this way to encode it.. the right most 0 is then the minumum
! in nZeros which is bigger then the minumum in nOnes
right_most_zero = minval(nZeros, nZeros > minval(nOnes))
end function right_most_zero