It appears to me your confusing where exactly the bits are located.
test is already imm (it is not the entire instruction.......)
For instance if you want bits 10:1 you should NOT be right shifting by 11 bits line 168 there are only these fields to right off it (11|19:12). That is not 11 bits.
- The other shifts and masks are also off, but you may not notice it in this test as the offset is just 0.
- But all of it stems from the fact that you seem to have written the code assuming test is storing the entire instruction; not just imm.
Further remember that is bits 10:1 not the 0th bit. The offset does not need to store the 0th bit. Think why.