title: C++ Compressing size of integer down to 2 bits?
body: <p>I am doing a little game physics networking project right now, and I am trying to optimize the packets I am sending using this guide:</p>
  
  <p><a href="https://gafferongames.com/post/snapshot_compression/" rel="nofollow noreferrer">https://gafferongames.com/post/snapshot_compression/</a></p>
  
  <p>In the "Optimize Quaternions" section it says:</p>
  
  <blockquote>
    <p>Don’t always drop the same component due to numerical precision issues. Instead, find the component with the largest absolute value and ENCODE its index using two bits [0,3] (0=x, 1=y, 2=z, 3=w), then send the index of the largest component and the smallest three components over the network</p>
  </blockquote>
  
  <p>Now my question is, how do I encode an integer down to 2 bits... or have I misunderstood the task?</p>
  
  <p>I know very little about compressing data, but reducing a 4 byte integer (32 bits) down to ONLY 2 bits seems a bit insane to me. Is that even possible, or have I completely misunderstood everything?</p>
  
  <p>EDIT:
  Here is some code of what I have so far:</p>
  
  <pre><code>void HavNetConnection::sendBodyPacket(HavNetBodyPacket bp)
  {
    RakNet::BitStream bsOut;
    bsOut.Write((RakNet::MessageID)ID_BODY_PACKET);
  
    float maxAbs = std::abs(bp.rotation(0));
    int maxIndex = 0;
    for (int i = 1; i &lt; 4; i++)
    {
      float rotAbs = std::abs(bp.rotation(i));
      if (rotAbs &gt; maxAbs) {
        maxAbs = rotAbs;
        maxIndex = i;
      }
    }
  
    bsOut.Write(bp.position(0));
    bsOut.Write(bp.position(1));
    bsOut.Write(bp.position(2));
    bsOut.Write(bp.linearVelocity(0));
    bsOut.Write(bp.linearVelocity(1));
    bsOut.Write(bp.linearVelocity(2));
    bsOut.Write(bp.rotation(0));
    bsOut.Write(bp.rotation(1));
    bsOut.Write(bp.rotation(2));
    bsOut.Write(bp.rotation(3));
    bsOut.Write(bp.bodyId.toRawInt(bp.bodyId));
    bsOut.Write(bp.stepCount);
  
    // Send body packets over UDP (UNRELIABLE), priority could be low.
    m_peer-&gt;Send(&amp;bsOut, MEDIUM_PRIORITY, UNRELIABLE, 
        0, RakNet::UNASSIGNED_SYSTEM_ADDRESS, true);
  }
  </code></pre>