Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Computers are binary devices. Because of this, numbers are represented by a sequence of 1s and 0s.</p> <p>Bitshifting is simply moving those sequences of 1s and 0s left or right.</p> <p>So all the <code>&gt;&gt;</code> operator does is shift the bits towards the right one bit.</p> <p>Consider the number 101:</p> <pre><code>// Assuming signed 8-bit integers 01100101 // How 101 is represented in binary 00110010 // After right shifting one bit, this represents 50 </code></pre> <p>The least significant bit in this case was truncated. Obviously the devil's in the details, but that's all there is really to it.</p> <p>The <code>&lt;&lt;</code> operator does the opposite operation:</p> <pre><code>// Assuming signed 8-bit integers 01100101 // How 101 is represented in binary 11001010 // After left shifting one bit, this represents -54 // Assuming unsigned 8-bit integers 01100101 // How 101 is represented in binary 11001010 // After left shifting one bit, this represents 202 </code></pre> <p>In this case, the most significant bit was truncated since I used only 8-bits. If the number had more bits, however:</p> <pre><code>// Assuming signed 16-bit integers 00000000 01100101 // How 101 is represented in binary 00000000 11001010 // After left shifting one bit, this represents 202 00000001 10010100 // After left shifting one bit again, this represents 404 </code></pre> <p>So you may get different numbers depending on how many bits and the data types associated with those bits you're dealing with.</p> <p><strong>Addendum:</strong> If you're wondering how binary works, think about how the decimal number system works. Consider the number 5287. It can be written like this:</p> <pre><code>5287 </code></pre> <p>But you can also write it out like this:</p> <pre><code>5287 = (5 * 1000) + (2 * 100) + (8 * 10) + (7 * 1) </code></pre> <p>Which you can then write out like this:</p> <pre><code>5287 = (5 * 10^3) + (2 * 10^2) + (8 * 10^1) + (7 * 10^0) </code></pre> <p>The above equation explains why the decimal number system is sometimes called the base-10 system. The decimal number system employs the use of 10 digits (0-9). Notice how the exponents correspond to digit position.</p> <p>The binary number system, or the base-2 system, is the exact same thing but with the number two as the base of the exponents, and employing only two digits: 0 and 1.</p> <pre><code>5287 = 00010100 10100111 (base 2) = (0 * 2^15) + (0 * 2^14) + (0 * 2^13) + (1 * 2^12) + (0 * 2^11) + (1 * 2^10) + (0 * 2^9) + (0 * 2^8) + (1 * 2^7) + (0 * 2^6) + (1 * 2^5) + (0 * 2^4) + (0 * 2^3) + (1 * 2^2) + (1 * 2^1) + (1 * 2^0) </code></pre>
    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