This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Problem in calculating checksum : casting int to signed int32
This should be a relatively easy answer, I just don't really know how to search for it...I got a few semi-relevant things, but nothing that fits what I'm trying to do.
>>> 1171855803 << 7
149997542784L # I want -326312576
In other words, treat the number as an integer and don't allow it to convert to a long.
How would I do this?
I tried the solution in this question:
>>> x = 0xFFFFFFFF & (1171855803 << 7)
>>> if x > 0x7FFFFFFF: print -(~(x - 1) & 0xFFFFFFFF)
else: print x
-326312576L # yay!
It works!
May not be the best answer, but this works...
import numpy as np
np.int32(1171855803) << 7
You could try
import ctypes
a=ctypes.c_int32(1171855803)
a.value<<=7
print a
which gives: c_int(-326312576)
It seems to allow platform specific bit-manipulations. I am not sure about efficiency.
Related
This question already has answers here:
Converting bitstring to 32-bit signed integer yields wrong result
(3 answers)
Closed 2 years ago.
Is there a way to force python to treat a number based upon its sign?
E.g. 0xFFFFFFFF = -1 instead of 4294967295?
You can use ctypes.c_int32 for a signed 32 bit integer:
import ctypes
wrapped = ctypes.c_int32(0xFFFFFFFF)
print(wrapped) # c_int(-1)
print(wrapped.value) # -1
This question already has answers here:
Numpy is calculating wrong [duplicate]
(2 answers)
Closed 3 years ago.
I am attempting to solve problem 8 of projecteuler. I am having difficulty understanding exactly why my code is not outputting the correct solution. I am aware that the solution to this problem is above the 32 bit maximum, but I do not know how to allow python to work with numbers above that within my code.
For reference, the original question states : "Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?"
from numpy import prod
f = 7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450
z = list(int(i) for i in str(f))
a1 =[]
start = 0
end = start + 13
while end <= len(z):
a1.append(prod(z[start:end]))
start+=1
end+=1
a = a1.index(max(a1))
print(a1[a]) #prints the product solution
print('---')
dimlen=end-start
newstart = a
newend=a+dimlen
print(z[newstart:newend]) #prints the integers that build the solution
I keep getting the number 2091059712, (the solution is 23514624000)
I think it might be numpy.prod. It might be preserving the input type and wrapping the value. Try using:
def prod(it):
p = 1
for m in it:
p *= m
return p
This question already has answers here:
Speed of calculating powers (in python)
(6 answers)
Closed 7 years ago.
Read somewhere in my adventure in optimizing my mandelbrot program that if you are for example squaring something. Using the mandelbrot equation as the example that
z * z is faster than z**2. Is this true, and if it is why is this so? Is it due to the fact that z**2 still computes trivial cases such as 0?
I am one of the people who have suggested the optimization and yes, it is true. In the example below, squaring with * is over 3 times as fast as with **2.
from timeit import repeat
print(repeat('z*z', 'z=.54321 + .56789j'))
print(repeat('z**2', 'z=.54321 + .56789j'))
# prints
[0.07780417092306088, 0.05484807785182001, 0.05577646621573226]
[0.2081317598277747, 0.19060335713839965, 0.1913974058615736]
Since both operations give the same answer
>>> z*z
(-0.02742194800000003+0.6169670537999999j)
>>> z**2
(-0.02742194800000003+0.6169670537999999j)
the second must eventually do the first (plus whatever else it does.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Python - '>>' operator
What does the >> operator means in Python?
ie:
x = x + str(n%2)
n >> 1
Thank you
n >> 1 shifts n right 1 bit. This is the same as dividing by 2.
More generally, n >> m shifts n right m bits, giving a division by 2^m.
See also:
Shifting operations in Python's documentation
Logical shift on Wikipedia
Just for completeness, note that there's a completely different usage which is changing the stream that print uses by default:
print >> sys.stderr, message
For more information, please have a look at this related question.
That's the right shift operator.
It's the bitshift operator:
x >> n
x shifted right by n bits.
This question already has answers here:
What is the result of % in Python?
(20 answers)
Closed 9 years ago.
I'm working on the exercises in Learning Python the Hard Way and I am wondering what the % function does. I am getting the wrong answer when i count my eggs and hoping understanding this will help me understand what I'm doing wrong.
I can't really tell why your code is broken because you haven't shown anybody what your code is. Please post samples and links next time.
Python % is used in two places, one is mathematical (the modulo operator), and the other has to do with formatting text. I'm going to assume "counting eggs" means the math way.
The modulo operator in X % Y means "Divide X by Y and give me the remainder." So:
10 % 2 == 0
10 % 3 == 1
10 % 11 == 10
That is the the modulo operator