-2

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

  • 2
    As you found, you use it to shift bits, which works for more than just decimal numbers... For why? I'd assume it is for parity for other languages such as C. Also, `2**` **and** floating point multiplication is generally not the same performance as `<<` – OneCricketeer Aug 19 '22 at 23:31
  • why wouldn't be allowed? If you want to shift bits like this why not? – fgoudra Aug 19 '22 at 23:33
  • @fgoudra not that it wouldnt be allowed, but the makers of python typically dont give more than 1 way of doing 1 thing, to keep code straight foward and readable – Christian Trujillo Aug 19 '22 at 23:36
  • 1
    @ChristianTrujillo `myarray[-1]` :), but obviously it does not work on the other side of the index range. Just to be consistent. (The question itself looks way too broad/opinion based... Maybe if you scope it down to historical reasons like "why Python has the same operators as most other languages" it could be more focused... but likely still opinion based) – Alexei Levenkov Aug 19 '22 at 23:41
  • 2
    So the question "what is the purpose of shifting bits" is not specific to Python. You can also search for existing questions on other languages (like [this one for Java](https://stackoverflow.com/q/7454619/2745495) and all the related links in the comment under that question) and the reasoning could apply as well on Python. – Gino Mempin Aug 19 '22 at 23:42
  • 2
    [Have you ever had to use bit shifting in real projects?](https://stackoverflow.com/questions/520625/have-you-ever-had-to-use-bit-shifting-in-real-projects), [Are there any good reasons to use bit shifting except for quick math?](https://stackoverflow.com/q/3692992/2745495), [Practical applications of bit shifting](https://stackoverflow.com/q/9455941/2745495), [Real world use cases of bitwise operators](https://stackoverflow.com/q/2096916/2745495) – Gino Mempin Aug 19 '22 at 23:44

1 Answers1

1

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.

Grismar
  • 27,561
  • 4
  • 31
  • 54
  • awesome answer, thankyou. in my head this was purely for math applications, I failed to stop and think about computer science applications lol. Im fairly new to programming and still come across entirely new things in python all the time. this and operation overloading (what lead me to this operator) happened to be my learn of the day – Christian Trujillo Aug 19 '22 at 23:41
  • It's not a strange question - and you were correct in identifying that this is likely an operator you won't be using a lot. But there are specific operations and algorithms where it definitely beats more 'expensive' operations still and you're also right that it's more of a useful computers science operator than a math operator. As you learn more languages, you'll see it shows up almost everywhere. Also look at these operators: `& | ^ ~`, which are the other bitwise operators in Python. – Grismar Aug 19 '22 at 23:50
  • I use `&` and `|` allot, but Ive never noticed a difference between those and `and/or` accept that they bitwise operators sometimes work where `and/or` are "too ambiguous" – Christian Trujillo Aug 20 '22 at 00:25