Modifying an irregular length bit field in a register in C/C++
You don't need to use the new value to make a mask. Instead you simply need to clear all bits in the region you care about, and then the set mask is the new value.
For example, for DIVR1, you would do:
MODIFY_REG(&lt;reg&gt;,0x7F&lt;&lt;24,(newVal &amp; 0x7F)&lt;&lt;24);

To understand this further, lets split it up into what's going on. Under the assumption you are using an STM32 type processor (just a guess), the MODIFY_REG macro expands to:
WRITE_REG((REG), (((READ_REG(REG)) &amp; (~(CLEARMASK))) | (SETMASK)))

Which is equivalent to:
uint32_t tempVal = REG; //Read the register value
tempVal &amp;= ~CLEARMASK;  //Clear all bits that are set to 1 in the mask
tempVal |= SETMASK;     //Set the new bits
REG = tempVal;          //Assign the value back

The clearing step basically will take any bit set to a 1 in the CLEARMASK, and set the corresponding bit in the temporary value to 0. It doesn't matter if one of those bits is already zero.
With all the bits in your register chunk clear, then the SETMASK becomes simply your value to set the chunk to. Any bits that are 1 in the mask will be set as expected, any bits that are 0 will already be zero in the temporary value as you've already cleared them.
So the first example expands to:
uint32_t temp = &lt;reg&gt;;
temp &amp;= ~(0x7F &lt;&lt; 24); //Clear bits 30:24 inclusive (0x7F is a value of seven consecutive ones)
temp |= ((newVal &amp; 0x7F) &lt;&lt; 24); //Sets the new value into bits 30:24.
&lt;reg&gt; = temp;

Note that we mask the new value to ensure that any bits beyond the seven we are interested in are not accidentally affected. You could also do:
uint32_t mask = 0x7F &lt;&lt; 24;
MODIFY_REG(&lt;reg&gt;,mask,(data &lt;&lt; 24) &amp; mask);

Which would be equivalent.