By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,035 Members | 1,995 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,035 IT Pros & Developers. It's quick & easy.

Bitwise operators. Differences from javascript and PHP.

P: n/a
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?

Nov 17 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
sk**********@gmail.com wrote:
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:
>>4653896912 sys.maxint
True

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

</F>

Nov 17 '06 #2

P: n/a

skoroboga...@gmail.com wrote:
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

Nov 17 '06 #3

P: n/a
John Machin wrote:
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>

Nov 17 '06 #4

P: n/a


On Nov 17, 9:38 pm, Fredrik Lundh <fred...@pythonware.comwrote:
John Machin wrote:
http://theopensourcery.com/phplogic.htm
"In effect the bitwise operations [words "can be" omitted, I presume]"can only be", more likely.
Yes, probably.
>
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
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

Nov 17 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.