Count bits in VBScript
According to information in VBScript Data Types MSDN article, a 32-bit integer is long variant subtype. 

However, the value &amp;h8000 (decimal  -32768) seems to be a 16-bit integer minimal value; it's hexadecimal &amp;hFFFF8000 if converted to long subtype (check Wscript.Echo Hex(&amp;h8000).

This means that your function miscounts 1 additional bit for all long values above 32767 i.e. above 16-bit integer maximal value &amp;h7FFF.

To check the sign bit of a long value correctly, use &amp;h80000000 as follows:

Function countBits(value)
  Dim mask : mask = 1
  Dim i
  countBits = 0                    ' no need to use any auxiliary variable
  For i = 0 to 30
    If ( value And mask ) &gt; 0 Then
      countBits = countBits + 1
    End If
    mask = mask * 2                ' got lost due to your own edit Dec 11 '16 at 14:22
  Next
  If ( value And &amp;h80000000 ) Then ' check the sign bit
    countBits = countBits + 1
  End If
End Function


Sample tests:

Wscript.Echo countBits(2^16)         '  1
Wscript.Echo countBits(2^30)         '  1
Wscript.Echo countBits(1)            '  1
Wscript.Echo countBits(-1)           ' 32
Wscript.Echo countBits(-2)           ' 31
Wscript.Echo countBits(-2147483648)  '  1