      subroutine faibin(nsa,nsb,noac,nalpha,nbeta,ibin)
      integer nsa(nalpha),nsb(nbeta)
      ibin=0
      do k=1,nalpha
         ibin=ibset(ibin,nsa(k)-1)
      enddo
      do k=1,nbeta
         ibin=ibset(ibin,nsb(k)+16-1)
      enddo
!      print'(a,20i3)','nsa ',nsa
!      print'(a,20i3)','nsb ',nsb
!      print*,'ibin',ibin
!      print*,'-------------------------------------'
      return
      end
       subroutine encodbin(nd,idist,irang,noac,ibin,jbin,icl)
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! calcul SEL : codage de la distance
! en entree : idist
! si noac<15, en entree : noac,irang,(jbin inutilise)
! modifies : nd,ibin
! si noac>14, modifies : jbin
!
! ibin : entier codant le string CAS
! icl  : classe (nca,ncb) du string CAS
! nd   : nb de determinants de la classe (icl,idist)
!
! principe : on a en entree un string CAS jbin, obtenu par excitation
!            d'une reference, et sa distance
! ce string a peut-etre deja ete rencontre (autre ref, autre excitation)
! si la nouvelle distance est plus petite que l'ancienne, on la prend
! (on corrige alors la distance dans ibin ou jbin, on corrige nd)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
      dimension nd(19,0:2),jbin(*)
      integer(1) jbin
      integer ibin
      logical yd0,yd1,yd2,yd3,y1,y2,yw
      yw=.false.
      if(noac>14) then
!        jamais teste !
         jjbin=jbin(irang)
         if(idist<jjbin) then
            nd(icl,idist)=nd(icl,idist)+1
            nd(icl,jjbin)=nd(icl,jjbin)-1
            jbin(irang)=idist
         endif
      else
         !print'(a,16l1,2x,16l1)','E ',(btest(ibin,k),k=1,31)
         kk=16+noac+1
         ll=noac+1
         y1=btest(ibin,ll-1)
         y2=btest(ibin,kk-1)
         yd3=.not.y1.and..not.y2
         yd1=.not.y1.and.y2
         yd2=y1.and..not.y2
         yd0=y1.and.y2
         if(yw)print*,'y1,y2,yd0,yd1,yd2,yd3',y1,y2,yd0,yd1,yd2,yd3
!         if(yd3) nd(icl,idist)=nd(icl,idist)+1
!         print*,'yd3',yd3,icl,idist,nd(icl,idist)
         if(idist==0.and.yd1) then
            ibin=ibset(ibin,kk-1)
            ibin=ibset(ibin,ll-1)
            nd(icl,0)=nd(icl,0)+1
            nd(icl,1)=nd(icl,1)-1
            if(yw)print*,'yd1-->0',icl,idist,nd(icl,idist)
         endif
         if(idist==0.and.yd2) then
            ibin=ibset(ibin,kk-1)
            ibin=ibset(ibin,ll-1)
            nd(icl,0)=nd(icl,0)+1
            nd(icl,2)=nd(icl,2)-1
            if(yw)print*,'yd2-->0',icl,idist,nd(icl,idist)
         endif
         if(idist==0.and.yd3) then
            ibin=ibset(ibin,kk-1)
            ibin=ibset(ibin,ll-1)
            nd(icl,0)=nd(icl,0)+1
            if(yw)print*,'yd3-->0',icl,idist,nd(icl,idist)
         endif
         if(idist==1.and.yd2) then
            ibin=ibset(ibin,kk-1)
            ibin=ibclr(ibin,ll-1)
            nd(icl,1)=nd(icl,1)+1
            nd(icl,2)=nd(icl,2)-1
            if(yw)print*,'yd2-->1',icl,idist,nd(icl,idist)
         endif
         if(idist==1.and.yd3) then
            ibin=ibset(ibin,kk-1)
            ibin=ibclr(ibin,ll-1)
            nd(icl,1)=nd(icl,1)+1
            if(yw)print*,'yd3-->1',icl,idist,nd(icl,idist)
         endif
         if(idist==2.and.yd3) then
            ibin=ibset(ibin,ll-1)
            nd(icl,2)=nd(icl,2)+1
            if(yw)print*,'yd3-->2',icl,idist,nd(icl,idist)
         endif
         if(yw)print'(a,16l1,2x,16l1)','S ',(btest(ibin,k-1),k=1,32)
      endif
      return
      end
