'
' Number.GetBits
'
' 24-30 Jan 2002
'
' See Number.SetBits for detailed explanations.
'===================================================================='

nBits = SELF.Get(4)    ' Number of bits to get
if (nBits<=0) then
  return NIL
end
x = SELF.Get(0)        ' Original number
iX = SELF.Get(1)       ' Start bit offset in x; must be 1 or greater
bmpOut = SELF.Get(2)   ' Output bitmap
iB = SELF.Get(3)       ' Start bit offset in bmpOut
'
' Remove the sign bit.
'

x = x.Abs
'
' Obtain the exponent.
'

if (x=0.0) then
  nExponent = 0
  x = 0.0
else
  nExponent = x.Log(2).Ceiling max -1023
  while (2^(nExponent-1)>x and (nExponent>-1023))
    nExponent = nExponent-1
  end
  while (2^nExponent<=x and (nExponent<1024))
    nExponent = nExponent+1
  end
  '
  ' Normalize, then shift left by iX + nBits.
  '

  x = ((2^(0-nExponent))*x)*(2^(iX+nBits))
  x = x.Floor
end
'
' Copy the message right-to-left into bmpOut.
'

for each j in iB+nBits-1..iB by -1
  if (x mod 2<>0) then
    bmpOut.Set(j)
  end
  x = (x/2).Floor
end
' end of script