python shift operator uses [closed] - python

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 months ago.
Improve this question
Im just learning about pythons bitwise operator << and >>. as far as I see, it takes the binary version of an integer and shifts it n places left or right. that would mean that saying x<<y is equivalent to x*(2**y)
so my question is why is there an operator for this? as far as I know python doesnt like to give you more than 1 way of doing things to avoid confusion. is there a reason this operator is particularly useful or typical scenerios where its used? I know this is a pretty open ended question but when searching for this I only come across what this operator does, not why we would use it. thankyou in advance

The key is in your remark "it takes the binary version of an integer and shifts it n places left or right".
Ask yourself this: how does your computer represent integers at all? What are integers? Any integer is a sequence of bits, (typically a multiple of 8 bits, i.e. a byte) and your computer is built around memory positions, registers, addresses, etc. that hold these integer values.
So, it makes sense for a CPU to have an operation to shift such a value left or right by one bit, for an extremely fast multiplication or division by 2, more so since powers of two are very commonly needed because everything in your computer is binary.
Other operations can be composed from simple addition, subtraction, shift by n, etc. - Python exposes this operation to give you access to this very basic and quick operation, although Python integers aren't always (or even all that often) the same efficient integers you operate on directly in many other languages.
But bit-shifting has many applications, and as a standard operation of your computer, it only makes sense that Python would give you access to a tool that programmers are very used to, and have applications for in many common algorithms.

Related

Equivalent of Python in C++ operator [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Is there a way to do the same in C++?
Python
int.__add__(1, 1)
I can make something in C++, but it is still ugly and not very optimised and short.
I just want this : 1+1, but without + .
I absolutely want to use the int "class".
It's for matrix multiplication, addition, and subtraction...
I just want to have better-looking and shorter code.
I can make something in C++, but it is still ugly and not very optimised and short.
Last time I checked, int.__add__(1, 1) was way longer than 1+1. Just about anything is longer than 1+1. As for optimisations, you are not in a position to talk about what is more optimised, having not measured anything.
It's for matrix multiplication, addition, and subtraction
The same integer + operator is useful in lots of contexts. Matrix operations are among them. There is no need to single them out.
I just want this : 1+1, but without +
What you want is of secondary importance. Programming is a team sport. You do what everyone else does. Not only because it is likely to be tried and tested and the best thing after the sliced bread, but also because if everyone is doing something in a particular way and you are doing it differently, others will find it hard to understand what you mean.
This is not to say you cannot break conventions and introduce innovations. People do it all the time. But they are not asking anyone how to! If you need to ask, you are not in a position to lead the crowd.
I just want to have better-looking and shorter code.
Then absolutely positively use +. A no-brainer.
You want to use a function (or functor) for addition instead of the + operator. C++ has std::plus for that. I don't use C++ but I think one or both of these should work:
int a = std::plus(1, 1);
int a = std::plus<int>(1, 1);

Can you generate new asm files based on old ones? And if so what is the most efficent way? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Let's say I have a lot of .asm files on a python program (It can also be binary strings, hex strings or whatever you would like). How can I use those files to generate new files that function roughly the same (It's for an assembly game).
The thing is I have a lot of assembly players that were really good at the game and I wondered if I can somehow use natural selection to breed better assembly bots.
This sounds a lot like superoptimization (wikipedia).
e.g. STOKE starts with a sequence of asm instructions and stochastically modifies it looking for shorter / faster sequences that do the same thing.
(Or STOKE can start from scratch looking for an asm sequence that gives the desired result for a set of test-cases.)
It's open source, so have a look at the algorithms they use to modify asm and test-run the code. Of course it's possible if you have data structures that represent operands and opcodes.
See also Applying Genetic Programming to Bytecode and
Assembly, an academic paper from 2014.
I haven't read it, but hopefully it addresses ways to recombine code from different mutations and maybe get something useful more often than you get garbage that steps on the registers from the other code. (That's the major trick with random changes to code, especially in assembly where there are lots of non-useful instruction sequences.)

Python when substituting "**" instead of "+" into Fibonacci [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
My code, stuck at 6561, unable to continue. I have tried printing B, I've tried substituting while True for a counter of greater value than the amount being printed. I have also tried different numbers for the original values, this could sometimes make it worse; eg. "a, b = 9, 10" will only print 9 and 10. Question, how do I unstick it?
the code
UPDATE: It appears that if you run this code in idle, then create a new python file, python stops responding.
It's not a bug with the code, it's a direct consequence of the size of the generated numbers.
8 ** 6561 is a 5926 digit number. The next step in the sequence is 6561 raised to the power of that 5926 number. That's where it gets stuck - the computation is simply too large for Python to handle in a reasonable amount of time.
Python supports arbitrary sized integers beyond the native size supported by the hardware, and the implementation requires memory to store the digits of the integer. The result is that your program will consume more and more memory until either the answer is produced, your system/process runs out of memory, or you run out of patience.
You will find that it works with multiplication and addition simply because the numbers involved are smaller, and so it will take a lot longer to reach the huge sizes produced by exponentiation.

Bitwise Programming in Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to write a program to solve an ACM problem, and it has to be very fast. A mathematically fast approach to the problem is to use bitwise computations. However, I'm using python and I'm having trouble performing these computations at bit level. The problems are:
Counting the number of 1's and 0's in the bit. For example, how do we calculate the the binary digit 100010 has two 1's and 4 0's. The only approach I can imagine is to convert it to a string and and count them. But this conversion cancels out all the speed gained by working at bit level in the first place.
How to represent a string input describing the binary digit such as '100101' as an actual binary digit in Python? Currently I have a function that converts the bit into an integer and I perform the bitwise operations on the ints.
Is there a bit data type? That is can I receive the input as a bit rather than a string or an int?
I did consider writing a class such as bitSet in C++, but I have a feeling this will not be very fast either. P.S. the binary digits I'm working with can be as large as 1000 bits and I might have to work with a 1000 such binary digits, so efficiency is imperative.
Here's a well-known algorithm for counting the one-bits:
def bitCount(x):
count = 0
while x != 0:
count += 1
x &= (x-1)
return count
The point is that x-1 has the same binary representation as x, except that the least least significant one-bit is cleared to 0 and all the trailing 0's are set to one. So, setting x = x & (x-1) simply clears the least significant one-bit.

Python subtleties for MATLAB users [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
What are some of the most basic things that a programmer who is used to coding in MATLAB needs to be cautious about, when beginning to code in Python? I am not asking for an entire list of differences between MATLAB and Python, just a few very basic things that can save a beginner a few hours of debugging.
An example would be how someone used to MATLAB's 1:N may make a mistake in python by using range(1,N)
Differences in concepts would be more helpful than absolute differences in specific commands.
The NumPy site has an excellent list, which I won't reproduce in full here. NumPy provides basically all the basic functionality of MATLAB for Python users, so it is highly recommended if you're coming to Python from MATLAB.
Link
Some highlights:
INDEXING: MATLABĀ® uses one based indexing, so the initial element of a sequence has index 1. Python uses zero based indexing, so the initial element of a sequence has index 0. Confusion and flamewars arise because each has advantages and disadvantages. One based indexing is consistent with common human language usage, where the "first" element of a sequence has index 1. Zero based indexing simplifies indexing. See also a text by prof.dr. Edsger W. Dijkstra.
LOGICOPS: & or | in Numpy is bitwise AND/OR, while in Matlab & and | are logical AND/OR. The difference should be clear to anyone with significant programming experience. The two can appear to work the same, but there are important differences. If you would have used Matlab's & or | operators, you should use the Numpy ufuncs logical_and/logical_or.

Categories