0

I'm performing 55 << 30 in C# and Python 3. C# returns -1073741824, which is what I want, but Python returns 59055800320. Can someone explain why exactly this happens, and what I can do to fix it?

3 Answers 3

1

python ints are arbitrarily large ... you can force it sort of

import numpy

numpy.int32(55) << 30
Sign up to request clarification or add additional context in comments.

Comments

1

Python integers are unbounded, so 59055800320 is the correct answer, but to simulate C#'s 32-bit limitation on integers and 2s complement negative numbers, you can do the math:

>>> hex(55)
'0x37'
>>> hex(55 << 30)  # 36-bit result, too large for 32-bit integers
'0xdc0000000'
>>> hex(55 << 30 & 0xffffffff) # mask off lower 32 bits.
'0xc0000000'
>>> ((55 << 30) & 0xffffffff)  # but the sign (bit 31) is a one, so it should be negative
3221225472

Bit 31 in a 32-bit signed integer has value -231, but unsigned it has value 231, meaning the value is 232 too high, so:

>>> ((55 << 30) & 0xffffffff) - (1<<32)
-1073741824

Here's a function:

def signed_32bit_shift_left(n,s):
    tmp = (n << s) & 0xffffffff
    return tmp - (1<<32) if tmp & 0x80000000 else tmp

Comments

0

As the others have said, Python ints are much larger than you might expect. To observe this (Python 3.7.4):

>>> import sys
>>> type(55)
<class 'int'>
>>> sys.getsizeof(int())
24

sys.getsizeof() will return the size, in bytes, of an object in memory.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.