Bitwise operators. Differences from javascript and PHP.

S

skorobogatov

I can't understand a little thing in bitwise operations.


In PHP and Javascript
4653896912>>13 = -212992

In Python and Ruby
4653896912>>13 = 568102

In Python and Ruby - it's ok. I understand.
But i need to get in Python same value as in PHP and Javascript.
How can i do this?
 
F

Fredrik Lundh

I can't understand a little thing in bitwise operations.

In PHP and Javascript
4653896912>>13 = -212992

In Python and Ruby
4653896912>>13 = 568102

In Python and Ruby - it's ok. I understand.
But i need to get in Python same value as in PHP and Javascript.
hint:
True

not sure what PHP is doing with that expression, really.

</F>
 
J

John Machin

I can't understand a little thing in bitwise operations.


In PHP and Javascript
4653896912>>13 = -212992

In Python and Ruby
4653896912>>13 = 568102

In Python and Ruby - it's ok. I understand.
But i need to get in Python same value as in PHP and Javascript.
How can i do this?

So that someone who knows next-to-nothing about those 2 languages can
help you, what is x >> n defined to do in PHP and Javascript?

If you can answer that question, what part of the Python implementation
of the PHP/Javascript definition are you having trouble with?

If you can't answer it ... is the problem caused by the fact that
4653896912 is, at 9 hex digits, a bit large for a 32-bit integer, those
2 languages don't have longer integers [my guess],
*and*, to quote something I found about PHP while googling,

http://theopensourcery.com/phplogic.htm

"In effect the bitwise operations [words "can be" omitted, I presume]
safely applied to integer variables - their effect on booleans,
float/double or string variables can be predicted but are not really
useful" ???

Cheers,
John
 
F

Fredrik Lundh

John said:
http://theopensourcery.com/phplogic.htm

"In effect the bitwise operations [words "can be" omitted, I presume]

"can only be", more likely.
safely applied to integer variables - their effect on booleans,
float/double or string variables can be predicted but are not really
useful" ???

sounds like we're in
>>> v = float(4653896912)
>>> from string import pack, unpack
>>> o = unpack("i", pack("d", v)[:4])[0]
>>> (-o>>(13-3))|-2<<(32-13-1)
-212992

territory.

</F>
 
J

John Machin

John said:
"In effect the bitwise operations [words "can be" omitted, I presume]"can only be", more likely.

Yes, probably.

from struct, more likely?
o = unpack("i", pack("d", v)[:4])[0]
(-o>>(13-3))|-2<<(32-13-1)
-212992

territory.

Sounds like you could be correct. However it's possible that you have
aided and abetted the OP to perpetrate a nonsense :)

Cheers,
John
 

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

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top