From: Csaba Gabor on
I'd like to determine the number of bits (the position
of the highest on bit) in a negative number (say -1)
without using knowledge of its type.

Valid operations would be AND, OR, XOR, negation,
and one's compliment can be done as:

'Get a -1 of the same type as myvar
Dim minus1: minus1 = myvar - (myvar + 1)
Dim onesComp: onesComp = myvar XOR minus1

You could also use multiplication or division by 2
or other mathematical operators/functions, but
then sign extension comes into play.

The standard solution is to presume the answer
given the type of the variable, but that is not very
generic.

Csaba Gabor from Vienna

By the way, this is equivalent to the problem of
determining the number of bits (both on and off
bits) in a given variable.
From: mr_unreliable on
Csaba Gabor wrote:
> I'd like to determine the number of bits (the position
> of the highest on bit) in a negative number (say -1)
> without using knowledge of its type.
>

The "brute force" way would be to "shift left" one bit
at a time, until you hit your 1-bit.

Problem is, you can't shift left in script. There are
a number of shifting functions written in VB, but they
require calling the system api.

Most scripters abhor calling the system api, and so
your best bet is to wait around for a more knowledgable
person to come along with a "pure script" solution.

cheers, jw
From: Tom Lavedas on
On Dec 2, 12:46 pm, mr_unreliable <kindlyReplyToNewsgr...(a)notmail.com>
wrote:
> Csaba Gabor wrote:
> > I'd like to determine the number of bits (the position
> > of the highest on bit) in a negative number (say -1)
> > without using knowledge of its type.
>
> The "brute force" way would be to "shift left" one bit
> at a time, until you hit your 1-bit.
>
> Problem is, you can't shift left in script.  There are
> a number of shifting functions written in VB, but they
> require calling the system api.
>
> Most scripters abhor calling the system api, and so
> your best bet is to wait around for a more knowledgable
> person to come along with a "pure script" solution.
>
> cheers, jw

JScript has SHIFT operators, so the old trick of using JScript in VBS
will work ...

wsh.echo ShiftRight(clng(-1) and &h7fffffff&, 30)

Function ShiftRight(arg, n)
with createobject("htmlfile")
.write "<script>var a=" & arg & ">>" & n & ";</script>"
ShiftRight = .parentWindow.a
end with
end function

Function ShiftLeft(arg, n)
with createobject("htmlfile")
.write "<script>var a=" & arg & "<<" & n & ";</script>"
ShiftLeft = .parentWindow.a
end with
end function

I also propose ANDing the number (converted to a Long - 32 bit -
number) with a mask that zeros the minus sign bit. Then the other
bits can be tested. The shift that first returns a value of zero is
the most significant bit (I didn't bother to show the test loop).

BTW, your shift nomenclature seems backward to me. I guess you were
thinking of shifting the mask and not the number. However, starting
from the MSB end would still seem most efficient and would still shift
the mask to the right, not the left wouldn't it?
_____________________
Tom Lavedas
From: mr_unreliable on
Tom Lavedas wrote:
> BTW, your shift nomenclature seems backward to me. I guess you were
> thinking of shifting the mask and not the number. However, starting
> from the MSB end would still seem most efficient and would still shift
> the mask to the right, not the left wouldn't it?
>

I wasn't clear in expressing what I was thinking --
which was to put a MASK in the left-most bit, then
shifting the DATA left one bit at a time, masking
to see what the left-most bit was, and then counting
the number of zero's, until a 1-bit was reached.

But then, my verbal skills have always been lacking,
even worse than GWB!

Regardless, as jscript has a shift function, that
sounds like the way to go.

cheers, jw
From: Paul Randall on

"mr_unreliable" <kindlyReplyToNewsgroup(a)notmail.com> wrote in message
news:u1ybAb3cKHA.5156(a)TK2MSFTNGP04.phx.gbl...
> Csaba Gabor wrote:
>> I'd like to determine the number of bits (the position
>> of the highest on bit) in a negative number (say -1)
>> without using knowledge of its type.
>>
>
> The "brute force" way would be to "shift left" one bit
> at a time, until you hit your 1-bit.
>
> Problem is, you can't shift left in script. There are
> a number of shifting functions written in VB, but they
> require calling the system api.
>
> Most scripters abhor calling the system api, and so
> your best bet is to wait around for a more knowledgable
> person to come along with a "pure script" solution.

If the task must be done without using "knowledge of its type", then double
precision floats might be involved, which could be a lot of bits that don't
shift nicely.