Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The bitwise AND is somewhat special. <code>==</code> checks for equality but the bitwise AND operator allows you to work with the individual bits of a number.</p> <p>Imagine your event was defined as a list of keys:</p> <pre><code>event = ['a', 'shift', 'ctrl'] </code></pre> <p>You can then check to see if a particular modifier is a part of the event pretty easily:</p> <pre><code>if 'shift' in event: # ... </code></pre> <p>The bitwise AND is sort of like an <code>in</code> statement. You can define your event as a binary number like this:</p> <pre><code>event = 00010010 </code></pre> <p>Now, when you perform the bitwise AND, you can easily check to see if a certain modifier has been applied to the event, as modifiers are also represented as binary numbers:</p> <pre><code> 00010001 # event (18) &amp; 00010000 # shift key (8) ---------- 00010000 # you get a non-zero answer, so the shift key is in the event ---------- 00010001 # event (18) &amp; 00001000 # "z" key (4) ---------- 00000000 # you get zero because the "z" key wasn't a part of the event ---------- </code></pre> <p>You can construct an event like this using the bitwise OR:</p> <pre><code> 00000001 # shift key (1) | 10100000 # "a" key (160) ---------- 10100001 # resulting event (161) ---------- </code></pre> <p>Wikipedia sums up <a href="http://en.wikipedia.org/wiki/Bitwise_operation" rel="nofollow">bitwise operations</a> pretty well:</p> <blockquote> <p>A bitwise operation operates on one or more bit patterns or binary numerals at the level of their individual bits. It is a fast, primitive action directly supported by the processor, and is used to manipulate values for comparisons and calculations. On simple low-cost processors, typically, bitwise operations are substantially faster than division, several times faster than multiplication, and sometimes significantly faster than addition. While modern processors usually perform addition and multiplication just as fast as bitwise operations due to their longer instruction pipelines and other architectural design choices, bitwise operations do commonly use less power/performance because of the reduced use of resources.</p> </blockquote> <p>Basically, bitwise operators allow you to work with information stored in the bits of an integer efficiently.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload