Someone help me, please: "<<" and ">>" and what's its importance? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Someone help me, please: "<<" and ">>" and what's its importance?

explanation : 5>>2 =1 and 5<<2 =20

14th May 2017, 1:21 AM
Jean Fouby 🇨🇲
Jean Fouby 🇨🇲 - avatar
5 Answers
+ 6
These are bitwise operators and work, as their name suggests, with bits. Example: x << y : Returns x with the bits shifted to the left by y places (and new bits on the right-hand-side are zeros). This is the same as multiplying x by 2**y. x >> y : Returns x with the bits shifted to the right by y places. This is the same as dividing xby 2**y. Sources: 1)https://wiki.python.org/moin/BitwiseOperators 2)http://stackoverflow.com/questions/22832615/what-do-and-mean-in-python 3)http://stackoverflow.com/questions/3411749/python-operator
14th May 2017, 1:44 AM
Billy Tsouvalas
+ 7
Are bitwise operators
14th May 2017, 2:52 AM
NimWing Yuan
NimWing Yuan - avatar
+ 3
these are shift operator. 5>>2 means 5/2=2.5 2.5/2=1.25 and shift operator always give integer value so the result is 1. while 5<<2 5*2=10 10*2=20 hence the result is 20.
14th May 2017, 1:44 AM
MR Programmer
MR Programmer - avatar
+ 3
Bitwise Operators Use: Bit fields (flags) They're the most efficient way of representing something whose state is defined by several "yes or no" properties. ACLs are a good example; if you have let's say 4 discrete permissions (read, write, execute, change policy), it's better to store this in 1 byte rather than waste 4. These can be mapped to enumeration types in many languages for added convenience. Communication over ports/sockets Always involves checksums, parity, stop bits, flow control algorithms, and so on, which usually depend on the logic values of individual bytes as opposed to numeric values, since the medium may only be capable of transmitting one bit at a time. Compression, Encryption Both of these are heavily dependent on bitwise algorithms. Look at the deflate algorithm for an example - everything is in bits, not bytes. Finite State Machines I'm speaking primarily of the kind embedded in some piece of hardware, although they can be found in software too. These are combinatorial in nature - they might literally be getting "compiled" down to a bunch of logic gates, so they have to be expressed as AND, OR, NOT, etc. Graphics There's hardly enough space here to get into every area where these operators are used in graphics programming. XOR (or ^) is particularly interesting here because applying the same input a second time will undo the first. Older GUIs used to rely on this for selection highlighting and other overlays, in order to eliminate the need for costly redraws. They're still useful in slow graphics protocols (i.e. remote desktop). (All the credit for the above info goes to Aaeonaught from stack overflow. His answer to this question is excellent and I couldn't explain it better.) Sources: 1)http://stackoverflow.com/questions/1746613/bitwise-operation-and-usage 2)http://stackoverflow.com/questions/2096916/real-world-use-cases-of-bitwise-operators
14th May 2017, 2:14 AM
Billy Tsouvalas
+ 1
Thankss you for your help, Really, I don't see the importance
14th May 2017, 2:00 AM
Jean Fouby 🇨🇲
Jean Fouby 🇨🇲 - avatar