DOCUMENTATION

The program extracts the four parameters to function setbits from a single input line by invoking function loadnext four times.  Function loadnext first invokes function loadstr to load the next string of non-whitespace characters found in the input line to an array.  Then, depending on the parameter count, loadnext invokes either function htol to convert the array from hex characters to an unsigned long int, or function atoi to convert it from decimal characters to an int.

Then, function setbits returns an unsigned long containing the same bits as x, but with the n bits at position p replaced by the rightmost n bits in y.  This is constructed from an OR of two expressions.

The first expression evaluates to x with the designated bit string zeroed out.  This is the result of taking x AND a mask consisting of all 1-bits except for n 0-bits at position p.  The mask is formed by taking an unsigned long of leading 0-bits and n rightmost 1-bits, ~(~0UL << n), shifting it left by the number of bits to the right of the bit string, p + 1 - n, and finally, inverting all bits.

The second expression evaluates to the rightmost n bits of y at position p with leading and trailing 0-bits.  This is the result of first taking y AND a mask consisting of leading 0-bits and n rightmost 1-bits, and then shifting the outcome left by the number of bits to the right of the bit string.

Then, function ltoh converts the altered unsigned long to an array of hex characters.