Note that there are some explanatory texts on larger screens.

plurals
  1. POUnderstanding Left and Right Bitwise shift on a 128-bit number
    primarykey
    data
    text
    <p>Artichoke101 <a href="https://stackoverflow.com/questions/5996384/bitwise-shift-operation-on-a-128-bit-number">asked this</a>:</p> <blockquote> <p>Lets say that I have an array of 4 32-bit integers which I use to store the 128-bit number</p> <p>How can I perform left and right shift on this 128-bit number?"</p> </blockquote> <p>My question is related to the answer Remus Rusanu gave:</p> <pre><code>void shiftl128 ( unsigned int&amp; a, unsigned int&amp; b, unsigned int&amp; c, unsigned int&amp; d, size_t k) { assert (k &lt;= 128); if (k &gt; 32) { a=b; b=c; c=d; d=0; shiftl128(a,b,c,d,k-32); } else { a = (a &lt;&lt; k) | (b &gt;&gt; (32-k)); b = (b &lt;&lt; k) | (c &gt;&gt; (32-k)); c = (c &lt;&lt; k) | (d &gt;&gt; (32-k)); d = (d &lt;&lt; k); } } void shiftr128 ( unsigned int&amp; a, unsigned int&amp; b, unsigned int&amp; c, unsigned int&amp; d, size_t k) { assert (k &lt;= 128); if (k &gt; 32) { d=c; c=b; b=a; a=0; shiftr128(a,b,c,d,k-32); } else { d = (c &lt;&lt; (32-k)) | (d &gt;&gt; k); \ c = (b &lt;&lt; (32-k)) | (c &gt;&gt; k); \ b = (a &lt;&lt; (32-k)) | (b &gt;&gt; k); \ a = (a &gt;&gt; k); } } </code></pre> <p>Lets just focus on one shift, the left shift say. Specifically, </p> <pre><code>a = (a &lt;&lt; k) | (b &gt;&gt; (32-k)); b = (b &lt;&lt; k) | (c &gt;&gt; (32-k)); c = (c &lt;&lt; k) | (d &gt;&gt; (32-k)); d = (d &lt;&lt; k); </code></pre> <p>How is this left shifting the 128-bit number? I understand what bit shifting is, &lt;&lt; shifts bits left, (8-bit number) like 00011000 left shifted 2 is 01100000. Same goes for the right shift, but to the right. Then the single "pipe" | is OR meaning any 1 in either 32-bit number will be in the result.</p> <p>How is <code>a = (a &lt;&lt; k) | (b &gt;&gt; (32-k))</code> shifting the first part (32) of the 128-bit number correctly?</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.
 

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