title: Packing 4 integers into one single [10,10,10,2] integer
body: <p>I am porting some <a href="https://github.com/g-truc/ogl-samples/blob/master/tests/gl-330-buffer-type.cpp" rel="nofollow">C++ samples</a> to <a href="https://github.com/elect86/jogl-samples/blob/master/jogl-samples/src/tests/gl_330/Gl_330_buffer_type.java" rel="nofollow">Java</a></p>
  
  <p>I am now stuck trying to pack four integers into a single one in the following layout [10, 10, 10, 2] in bits, that is the first int will occupy the first 10 bits, similar for the second and third one, while the last one just the last two bits.</p>
  
  <p>In the C++ sample, this is the code to pack them:</p>
  
  <pre><code>GLM_FUNC_QUALIFIER uint32 packSnorm3x10_1x2(vec4 const &amp; v)
      {
          detail::i10i10i10i2 Result;
          Result.data.x = int(round(clamp(v.x,-1.0f, 1.0f) * 511.f));
          Result.data.y = int(round(clamp(v.y,-1.0f, 1.0f) * 511.f));
          Result.data.z = int(round(clamp(v.z,-1.0f, 1.0f) * 511.f));
          Result.data.w = int(round(clamp(v.w,-1.0f, 1.0f) *   1.f));
          return Result.pack;
      }
  </code></pre>
  
  <p>Result.data is the following <code>struct</code>:</p>
  
  <pre><code>union i10i10i10i2
      {
          struct
          {
              int x : 10;
              int y : 10;
              int z : 10;
              int w : 2;
          } data;
          uint32 pack;
      };
  </code></pre>
  
  <p>With an input equal to <code>[-1f,-1f,0f,1f]</code> we get a <code>Result.data</code> equal to <code>[-511,-511,0,1]</code> and this return value 1074267649, that, in binary is:</p>
  
  <pre><code>        0                       -511
     |          |             |          |
   0100 0000 0000 1000 0000 0110 0000 0001
   ||             |          |
   1                  -511
  </code></pre>
  
  <p>What I did so far is:</p>
  
  <pre><code>public static int packSnorm3x10_1x2(float[] v) {
      int[] tmp = new int[4];
      tmp[0] = (int) (Math.max(-1, Math.min(1, v[0])) * 511.f);
      tmp[1] = (int) (Math.max(-1, Math.min(1, v[1])) * 511.f);
      tmp[2] = (int) (Math.max(-1, Math.min(1, v[2])) * 511.f);
      tmp[3] = (int) (Math.max(-1, Math.min(1, v[3])) * 1.f);
      int[] left = new int[4];
      left[0] = (tmp[0] &lt;&lt; 22);
      left[1] = (tmp[1] &lt;&lt; 22);
      left[2] = (tmp[2] &lt;&lt; 22);
      left[3] = (tmp[3] &lt;&lt; 30);
      int[] right = new int[4];
      right[0] = (left[0] &gt;&gt; 22);
      right[1] = (left[1] &gt;&gt; 12);
      right[2] = (left[2] &gt;&gt; 2);
      right[3] = (left[3] &gt;&gt; 0);
      return right[0] | right[1] | right[2] | right[3];
  }
  </code></pre>
  
  <p><code>tmp</code> is <code>[-511,-511,0,1]</code>, <code>left</code> is <code>[-2143289344,-2143289344,0,1073741824]</code>, which in binary is:</p>
  
  <pre><code>[1000 0000 0100 0000 0000 0000 0000 0000,
   1000 0000 0100 0000 0000 0000 0000 0000,
   0000 0000 0000 0000 0000 0000 0000 0000,
   0100 0000 0000 0000 0000 0000 0000 0000]
  </code></pre>
  
  <p>And it makes sense so far. Now that I cleaned the value on the left, I want to drag them on the right at their right place. But when I do so, I get the gap on the left filled with 1s, I guess because of the signed int in java(?).</p>
  
  <p>Then <code>right</code> is <code>[-511,-523264,0,1073741824]</code> or:</p>
  
  <pre><code>[1111 1111 1111 1111 1111 1110 0000 0001,
   1111 1111 1111 1000 0000 0100 0000 0000,
   0000 0000 0000 0000 0000 0000 0000 0000,
   0100 0000 0000 0000 0000 0000 0000 0000]
  </code></pre>
  
  <p>So, why is this happening and how can I fix this? Maybe with ANDing only the bits I am interested in?</p>