      integer*4 function get_bits(ia,iwrd,ibit,nbits)

C Function returns 32-bit (unsigned) integer containing  nbits  right-adjusted
C from array  ia.  Word and bit pointers  (iwrd  and  ibit,  respectively)
C define the start of the bit string to be transferred where  iwrd = 1,2,3,...
C and  ibit = 31,30,...,0  and the most significant bit is #31.

C This function gives the user the same functionality as  mvbits  except that
C it allows crossing word boundaries.

C iwrd  and  ibit  point to the next "unread" bit; they are updated by the
C routine to point at the next unread bit on return.


      integer*4       ia(1)       !input array
      integer*4       ibit        !bit pointer (31,30,...,0; msb to lsb)
      integer*4       iwrd        !word pointer (1,2,3,...)
      integer*4       nbits       !number of bits to extract


C Initialization

      get_bits = 0


C Do the transfer; different algorithm if operation has to be split

      if ((ibit - nbits) .ge. -1) then
         call mvbits(ia(iwrd),ibit-nbits+1,nbits,get_bits,0)
      else
         call mvbits(ia(iwrd),0,ibit+1,get_bits,nbits-ibit-1)
         call mvbits(ia(iwrd+1),33-nbits+ibit,nbits-ibit-1,get_bits,0)
      end if


C Update pointers

      ibit = mod(ibit-nbits+32,32)
      if (ibit .gt. (31-nbits)) iwrd = iwrd + 1

      return
      end
