-2

The expression (8 >> 1) evaluates to 4. Additionally, (8 << 1) evaluates to 16. I have no idea what the double arrow operator does, does anybody know?

Egg Dev
  • 3
  • 1

3 Answers3

0

Shifting operators. These operators accept integers as arguments. They shift the first argument to the left (<<) or right (>>) by the number of bits given by the second argument.

One example would be 0101 << 1 = 1010 etc

Image from wikipedia

enter image description here

Read it here.

user2736738
  • 30,591
  • 5
  • 42
  • 56
0

Double arrow operation is bit-shift.

Basically, it looks at your number in binary then "shifts" your entire number to one place left, or one place right.

For example:

8 >> 1 ->
             8 becomes:   00001000
shifting 1 place right:   00000100
which is 4.

Similarly,
8 << 1 ->
             8 becomes:   00001000
 shifting 1 place left:   00010000
which is 16.

There are some edge cases here and there such as logical shift versus arithmetic shift.

Usually, you can use them to do a very quick operation to multiply a number by 2 or divide them by 2, but that is language-dependent, and needs more detail to understand perfectly. In Python particularly, not so much as explained by @paxdiablo in the comments as follows:

bit shifting left in Python is not necessarily faster than multiplication, it has optimisations to avoid expanding its arbitrary precision integers in the latter case

More details on here: What are bitwise shift (bit-shift) operators and how do they work?

cSharp
  • 2,884
  • 1
  • 6
  • 23
  • 1
    Actually, bit shifting left in Python is not necessarily faster than multiplication, it has optimisations to avoid expanding its arbitrary precision integers in the latter case. I'll give you the benefit of the doubt that this is included in your edge cases but would prefer an answer tailored for Python itself. – paxdiablo May 01 '22 at 23:37
  • Thank you, this helped lot. I was initially unsure of the use of this operator. – Egg Dev May 02 '22 at 03:06
-1

These operators shift the bits in the number, right >> or left <<. In binary notation, shifting a number to the left yeilds 2 * that number. For example, 3: 0011, 6: 0110, 12: 1100

elbashmubarmeg
  • 330
  • 1
  • 9