473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Bitwise Operation limitation?

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?
Feb 9 '06 #1
10 6287
David R. wrote:
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.
Feb 9 '06 #2
> 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.
Feb 9 '06 #3
Thanks. Is there any back-end solution? Perhaps a custom version of AND
that does bitwise operation on numbers larger than 2^31?
"Dave Anderson" <GT**********@spammotel.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
David R. wrote:
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.

Feb 9 '06 #4
Anthony Jones wrote:
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.


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. Note again:

Response.Write(CBool(2^1023)) 'True

Only one culprit left: the AND Operator.
Try this:-

Response.Write CBool(2 and &h800000000)


This only works because &h800000000 = 134,217,728 -- a much smaller number
than 2^31 (2,147,483,648)
--
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.
Feb 11 '06 #5
>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.
Feb 11 '06 #6
Anthony Jones wrote:
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.
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, and additionally the reason why using hex notation does not help.
It's just how VB works.
VBScript is not VB.
Actually my post used the value &h80000000 (hexidecimal 8 followed by
7 0s) and your hexidecimal maths is a little rusty I think.


More like my cut-and-paste skills. I used this to get the value...

Response.Write(&h80000000)

....but I must have left off a digit. The point stands, however. You cannot
represent a number in VBScript with Hexadecimal that is bigger than "Long",
so (a) the AND Operator will still work with any hex number, and (b) this
still does not solve the OP's problem.

--
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.
Feb 11 '06 #7
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.
"Dave Anderson" <GT**********@spammotel.com> wrote in message
news:11*************@corp.supernews.com...
Anthony Jones wrote:
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.


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. Note again:

Response.Write(CBool(2^1023)) 'True

Only one culprit left: the AND Operator.
Try this:-

Response.Write CBool(2 and &h800000000)


This only works because &h800000000 = 134,217,728 -- a much smaller number
than 2^31 (2,147,483,648)
--
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.

Feb 12 '06 #8
>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.
Feb 12 '06 #9
Anthony Jones wrote:
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.
Feb 13 '06 #10
David R. wrote:
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.
Feb 13 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Randell D. | last post by:
Why would one use bitwise operators? I can program in various languages in some shape or form (C++, PHP, some scripting) and I've heard/seen bitwise operators before, but never understood why...
2
by: Joe Gonzalez | last post by:
Is it possible to perform bitwise operations in selects in DB2 8.1? In postgres I can say something like SELECT ... FROM <sometable> WHERE RecFlags && 0x08 <> 0 to get a bitwise and operation. I...
7
by: Jerry | last post by:
I want an algorithm that do arithmetic operations(divide,mutiply,add etc.)just using bitwise operators:<<,>>,&,|,^; For example,how "a/10" can be implemented. I just want a hint. Thanks.
2
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those...
8
by: Paul E Collins | last post by:
Suppose I have a few Keys objects: Keys k1 = Keys.V; // V Keys k2 = Keys.Control | Keys.V; // Ctrl+V Keys k3 = Keys.Shift | Keys.J; // Shift+J I need to determine which of these include the...
10
by: Emilio | last post by:
Do I use 'or' for bitwise operations where in c# I use | ?
3
by: Marc | last post by:
I'm trying to set the first three bits to zero on a byte. For some reason, the compiler is casting the number peculiarly when I use the bitwise complement operator. Here's what I think should...
45
by: Carramba | last post by:
Hi! I now that I can't do straight forward any bitwise operation on float (double etc..). But I wondering what is the easiest/best way to do this? I was thinking if I have float x=1.1111 so I can...
3
by: Sakhtkoosh | last post by:
I have written a program in C environment. I need to do operation on codes with more than 64 elements whose values are 0 and 1. At first, I simulated it with an array in 2 dimension(n*m) but after...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.