Bitwise Operation limitation?

D

David R.

I want to do bitwise operation on some large integers.

For example,

Response.Write CBool(2 AND 2^30) ' returns False
Response.Write CBool(2 AND 2^31) ' CRASHED!

Looks like the AND operator can only deal with integers up to 2147483647?
Is there a way to make the AND operator work with larger integers, such as
the double datatype?
 
D

Dave Anderson

David said:
I want to do bitwise operation on some large integers.

For example,

Response.Write CBool(2 AND 2^30) ' returns False
Response.Write CBool(2 AND 2^31) ' CRASHED!

Looks like the AND operator can only deal with integers up
to 2147483647? Is there a way to make the AND operator work
with larger integers, such as the double datatype?

One way:

<%@ Language=VBScript %><%

Response.Write(JSAnd(1234562,2^36-1))

%><script runat="server" language="jscript">
function JSAnd(a,b) { return a & b }
</script>



--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
 
A

Anthony Jones

Response.Write CBool(2 AND 2^31) ' CRASHED!
Looks like the AND operator can only deal with integers up to 2147483647?

This has nothing to do with the And operator.

The maximum length of integer that VBScript uses is 4-bytes long or 32 bits.
However this is a signed integer so the range is 2^31-1 down to -(2^31).

Try this:-

Response.Write CBool(2 and &h80000000)

If you are doing bitwise operations and you want to flip the top bit use:-

x = x Or &H80000000 ' On
x = x And &H7FFFFFFF ' Off

Anthony.
 
D

David R.

Thanks. Is there any back-end solution? Perhaps a custom version of AND
that does bitwise operation on numbers larger than 2^31?
 
A

Anthony Jones

It has everything to do with the AND operator. Note:
Response.Write(TypeName(2^1)) 'Double
That means the Exponentiation Operator has no trouble whatsoever with 2^31
(much less 2^1023). So that means the error was thrown by either CBool or
AND.

You make it sound like these language syntax constructs have some kind of
life of their own but their so basic (excuse the pun) that you can't really
seperate them into individual functions. It's just how VB works.

The fact is And, Or and Not in VB are fundamentally integer operators. VB
will attempt to coearce it's operands to an integer if they are not already
integers. As it will for any other operator or function which requires
integer inputs. There's nothing here that singles out And especially as
being a problem.

It is this coearsion which is overflowing. 2^31 cannot be represented by a
signed 4 byte integer.
This only works because &h800000000 = 134,217,728 -- a much smaller number
than 2^31 (2,147,483,648)

Actually my post used the value &h80000000 (hexidecimal 8 followed by 7 0s)
and your hexidecimal maths is a little rusty I think.

&h8 = 8 = 2^3
&h80 = 128 = 2^7
&h800 = 2048 = 2^11
&h8000& = 32768 = 2^15
..
..
&h8000000 = 134217728 = 2^27
&h80000000 = 2147483648 = 2^31 (although in VB it becomes negative)

Anthony.
 
D

David R.

Thanks for the confirmation, Dave.

Has anyone developed a custom AND that does bitwise operation on numbers
larger than 2^31? I'm asking so that I don't have to reinvent the wheel.
However, if no one has done it, I won't mind coming up with my own
algorithm.
 
A

Anthony Jones

You can indeed. The VBScript AND Operator requires operands of a particular
type, and will attempt to coerce values into that type. The other operators
in that block of code *CLEARLY* are not the cause of the error. There is no
question whatsoever that the AND Operator is the root cause of the OP's
problem,

Yes I'll concede the point that if it weren't for the precence of the AND
and that AND requires integer operands the error wouldn't happen. However
AND doesn't coerce it's operands VB(Script) does, but that's just splitting
semantics.
and additionally the reason why using hex notation does not help

Actually my original response does work:-

Response.Write CBool(2 and &h80000000)

but its not much use if you want to represent 2^32 upwards.
VBScript is not VB.

True but a significant portion of VBScript is based on the VB codebase and
in this particular case they behave the same.
(a) the AND Operator will still work with any hex number, and (b) this
still does not solve the OP's problem.

True and True.

Still I don't think the solution lies in switching to JScript.

Anthony.
 
D

Dave Anderson

Anthony said:
Still I don't think the solution lies in switching to JScript.

Certainly that has been the only solution offered thus far, so while it may
not be THE solution, it is A solution.



--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
 
D

Dave Anderson

David said:
Has anyone developed a custom AND that does bitwise operation on
numbers larger than 2^31? I'm asking so that I don't have to
reinvent the wheel. However, if no one has done it, I won't mind
coming up with my own algorithm.

If you know that one operand is always a Long or Int, you can do it this way
(the first parameter is Long, the second Double):

Function MyAnd(L,D)
Dim M, K, R
M = 2^(Int(Log(L)/Log(2)) + 1)
K = Int(D/M)
R = D - K*M
MyAnd = L AND R
End Function

You can try doing it recursively, but you'll end up having to write custom
Mod and/or integer division operators if you want to account for dual
operands of arbitrary size. The native ones have the same problem as AND --
they barf on Doubles.



--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top