java 左移 返回值,为什么在Python和Java中按位左移返回不同的结果?

I'm trying to port some functionality from a Java app to Python.

In Java,

System.out.println(155 << 24);

Returns: -1694498816

In Python:

print(155 << 24)

Returns 2600468480

Many other bitwise operations have worked in the same way in both languages. Why is there a different result in these two operations?

EDIT: I'm trying to create a function in python to replicate how the left shift operator works in Java. Something along the lines of:

def lshift(val, n):

return (int(val) << n) - 0x100000000

However this doesn't seem right as (I think) it turns all numbers negatives?

EDIT2: Several hours later, I've decided it is probably not the best idea to use Python for this job and will take part of the Java application and use it as a micro service for the existing Python app.

解决方案

Here are 3 different ways to convert a Python integer to its equivalent Java signed int. Note that these functions will not work correctly if the argument is wider than 32 bits, so you may wish to use bit masking on the argument before calling them.

The first way is to use the struct module to interpret the number as a 32 bit unsigned integer, pack it into bytes (using the local endian convention), and then unpack those bytes, interpreting them as a 32 bit signed integer. The other two methods use simple arithmetic with no function calls, so they are faster, but I guess they are a little harder to read.

This code was written on a 32 bit machine running Python 2.6.6, but it should run correctly on any architecture and version of Python (unless it's extremely ancient :) ).

from __future__ import print_function

from struct import pack, unpack

def ulong_to_long_pack(u):

''' using pack & unpack '''

ubytes = pack('L', u)

return unpack('l', ubytes)[0]

def ulong_to_long_sub(u):

''' using subtraction '''

return u - (1<<32) if u >= (1<<31) else u

def ulong_to_long2_xor(u):

''' using exclusive OR '''

return u ^ ~((1<<32)-1) if u & (1<<31) else u

funcs = (ulong_to_long_pack, ulong_to_long_sub, ulong_to_long2_xor)

# test

for ulong_to_long in funcs:

print(ulong_to_long.__doc__)

u = 2600468480

print(u, ulong_to_long(u))

big = 1<<31

for u in range(big - 3, big + 3):

print(u, ulong_to_long(u))

print()

output

using pack & unpack

2600468480 -1694498816

2147483645 2147483645

2147483646 2147483646

2147483647 2147483647

2147483648 -2147483648

2147483649 -2147483647

2147483650 -2147483646

using subtraction

2600468480 -1694498816

2147483645 2147483645

2147483646 2147483646

2147483647 2147483647

2147483648 -2147483648

2147483649 -2147483647

2147483650 -2147483646

using exclusive OR

2600468480 -1694498816

2147483645 2147483645

2147483646 2147483646

2147483647 2147483647

2147483648 -2147483648

2147483649 -2147483647

2147483650 -2147483646

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值