Decoding 32-bit MIPS instructions For a homework assignment I've been given the task of parsing out information from an 32-bit MIPS instruction. (For more information on the instruction formats, see here). The instructor has provided us with a header file enumerating all of the functions required, and it's my job to implement them.

Currently, this is what the implementation for one of my functions looks like:

void instruction_partition(unsigned instruction, unsigned *op, unsigned *r1, unsigned *r2, unsigned *r3, unsigned *funct, unsigned *offset, unsigned *jsec)
{
    assert(op &amp;&amp; r1 &amp;&amp; r2 &amp;&amp; r3 &amp;&amp; funct &amp;&amp; offset &amp;&amp; jsec);

    *op = (instruction &gt;&gt; 26) &amp; 0x3F;
    *r1 = (instruction &gt;&gt; 21) &amp; 0x1F;
    *r2 = (instruction &gt;&gt; 16) &amp; 0x1F;
    *r3 = (instruction &gt;&gt; 11) &amp; 0x1F;
    *offset = (instruction &gt;&gt; 6) &amp; 0x1F;
    *funct = instruction &amp; 0x3F;
    *jsec = instruction &amp; 0x3FFFFFF;
}


In a scenario such as this, I'd almost be tempted to hard-code the shift values and bitmasks, despite it being ingrained within me to avoid doing so at all costs.

I tried a few other configurations, but neither resonated with me. Any thoughts?

void instruction_partition(unsigned instruction, unsigned *op, unsigned *r1, unsigned *r2, unsigned *r3, unsigned *funct, unsigned *offset, unsigned *jsec)
{
    assert(op &amp;&amp; r1 &amp;&amp; r2 &amp;&amp; r3 &amp;&amp; funct &amp;&amp; offset &amp;&amp; jsec);

    struct { unsigned offset; unsigned bitMask; } 
        opMaskFormat = { 26, 0x3F }, 
        r1MaskFormat = { 21, 0x1F },
        r2MaskFormat = { 16, 0x1F },
        r3MaskFormat = { 11, 0x1F },
        functMaskFormat = { 6, 0x1F },
        offsetMaskFormat = { 0, 0x3F },
        jsecMaskFormat = { 0, 0x3FFFFFF };              

    *op = (instruction &gt;&gt; opMaskFormat.offset) &amp; opMaskFormat.bitMask;
    *r1 = (instruction &gt;&gt; r1MaskFormat.offset) &amp; opMaskFormat.bitMask;
    *r2 = (instruction &gt;&gt; r2MaskFormat.offset) &amp; r2MaskFormat.bitMask;
    *r3 = (instruction &gt;&gt; r3MaskFormat.offset) &amp; r3MaskFormat.bitMask;
    *offset = (instruction &gt;&gt; offsetMaskFormat.offset) &amp; offsetMaskFormat.bitMask;
    *funct = (instruction &gt;&gt; functMaskFormat.shift) &amp; functMaskFormat.bitMask;
    *jsec = (instruction &gt;&gt; jsecMaskFormat.offset) &amp; jsecMaskFormat.bitMask;
}




void instruction_partition(unsigned instruction, unsigned *op, unsigned *r1, unsigned *r2, unsigned *r3, unsigned *funct, unsigned *offset, unsigned *jsec)
{
    assert(op &amp;&amp; r1 &amp;&amp; r2 &amp;&amp; r3 &amp;&amp; funct &amp;&amp; offset &amp;&amp; jsec);

    unsigned SIX_BIT_MASK = 0x3F, 
        FIVE_BIT_MASK = 0x1F,
        TWENTY_SIX_BIT_MASK = 0x3FFFFFF;

    *op = (instruction &gt;&gt; 26) &amp; SIX_BIT_MASK;
    *r1 = (instruction &gt;&gt; 21) &amp; FIVE_BIT_MASK;
    *r2 = (instruction &gt;&gt; 16) &amp; FIVE_BIT_MASK;
    *r3 = (instruction &gt;&gt; 11) &amp; FIVE_BIT_MASK;
    *offset = (instruction &gt;&gt; 6) &amp; FIVE_BIT_MASK;
    *funct = instruction &amp; SIX_BIT_MASK;
    *jsec = instruction &amp; TWENTY_SIX_BIT_MASK;
}
Since the instruction is a 32-bit number, you should use uint32_t, rather than assuming that unsigned is 32 bits on the host.

The first version is fine, I think, because it has the least clutter.  An experienced programmer will have no problem figuring out what it does.  If you are concerned about the mental arithmetic required to figure out the width of the bitmasks, you could write it like this:

void instruction_partition(uint32_t instruction, unsigned *op, unsigned *r1, unsigned *r2, unsigned *r3, unsigned *funct, unsigned *offset, unsigned *jsec)
{
    assert(op &amp;&amp; r1 &amp;&amp; r2 &amp;&amp; r3 &amp;&amp; funct &amp;&amp; offset &amp;&amp; jsec);

    *op     = (instruction &gt;&gt; 26) &amp; ((1 &lt;&lt; 6) - 1); /* 6 most significant bits */
    *r1     = (instruction &gt;&gt; 21) &amp; ((1 &lt;&lt; 5) - 1);
    *r2     = (instruction &gt;&gt; 16) &amp; ((1 &lt;&lt; 5) - 1);
    *r3     = (instruction &gt;&gt; 11) &amp; ((1 &lt;&lt; 5) - 1);
    *offset = (instruction &gt;&gt;  6) &amp; ((1 &lt;&lt; 5) - 1);
    *funct  =  instruction        &amp; ((1 &lt;&lt; 6) - 1); /* 6 least significant bits */
    *jsec   =  instruction        &amp; ((1 &lt;&lt; 26) - 1);
}


This should compile to identical code as your first implementation.  Technically, the opcode doesn't need to be masked:

    *op = instruction &gt;&gt; 26;