K&R Exercise 2-6 : Inserting part of a bitset into another "Inserting" part of a bitset into another

This is Exercise 2-6 on the K&amp;R book, 2nd edition. The reader is asked to:


  Write a function setbits(x,p,n,y) that returns x with the n bits that begin at position p set to the rightmost n bits of y, leaving the other bits unchanged.


Parameters Description

Here's what each parameter does informally:


x -> serves as the "destination" bitset
y -> serves as the "source" bitset
p -> serves as the insertion point for the bits from y to be inserted on x
n -> serves as the length of the segment to "cut" from y and replace in x


Special Notes


I've tested with a number of different scenarios, and this code has passed all of them.
At the bottom of the question there's an outline of a test-case and my logic behind solving the problem


Code

#include &lt;stdio.h&gt;

//setbits: returns x with n bits that begin at p set to the rightmost n bits of y
//informally: "chop" n bits right-&gt;left from y, insert on x at position p
unsigned setbits(unsigned x, int p, int n, unsigned y)
{
    return ((~(~0 &lt;&lt; n) &amp; y) &lt;&lt; p) | ((~(~(~0 &lt;&lt; n) &lt;&lt; p)) &amp; x);
}

int main(void) {
    // Example test cases:
    // First scenario:
    // x = 100 -&gt;           0110 0100
    // y = 5 -&gt;             0000 0101
    // p = 4
    // n = 3
    // Expected outcome:    0101 0100  (84)
    // Second scenario:
    // x = 94 -&gt;            0101 1110
    // y = 195 -&gt;           1100 0011
    // p = 0
    // n = 3
    // Expected outcome:    0101 1011  (91)
    // Third scenario:
    // x = 94 -&gt;            0101 1110
    // y = 195 -&gt;           1100 0011
    // p = 6 
    // n = 4
    // Expected outcome:    1101 1110  (222)

    printf("%d\n", setbits(100, 4, 3, 5));
    printf("%d\n", setbits(94, 0, 3, 195));
    printf("%d\n", setbits(94, 6, 4, 195));

    return 0;
}


Outline


   
 Click for full-size image
Avoid UB

Shifting a 1 into the sign position is undefined behavior (UB). (C11 §6.5.7 4)  Instead, use unsigned types.  0 is an int constant.  0u is an unsigned constant.

// return ((~(~0 &lt;&lt; n) &amp; y) &lt;&lt; p) | ((~(~(~0 &lt;&lt; n) &lt;&lt; p)) &amp; x);
return ((~(~0u &lt;&lt; n) &amp; y) &lt;&lt; p) | ((~(~(~0u &lt;&lt; n) &lt;&lt; p)) &amp; x);


Clarity

Code for clarity and let the compiler form optimal code.

// return ((~(~0u &lt;&lt; n) &amp; y) &lt;&lt; p) | ((~(~(~0u &lt;&lt; n) &lt;&lt; p)) &amp; x);
unsigned new_mask = ~(~0u &lt;&lt; n);
unsigned old_mask = ~(mask &lt;&lt; p);
return ((new_mask &amp; y) &lt;&lt; p) | (old_mask &amp; x);


Specifier matching

"%d" with unsigned should have raised a compiler warning/note.  Enable all compiler warnings to avoid such easy to identify problems.

// printf("%d\n", setbits(100, 4, 3, 5));
printf("%u\n", setbits(100, 4, 3, 5));


For this task, using "%4x" would be more informative than "%u".

Wider testing

"I've tested with a number of different scenarios, and this code has passed all of them.". --> Testing with UINT_MAX, UINT_MAX-1, 1, 0 would help check out the "corners" of the function.

Document restrictions

Code limited to 0 &lt;= p &lt; bit_width and 0 &lt;= n &lt; bit_width.  Since code cannot handle all possible input values, stating  restrictions lessens incorrect usage.  

Defining behavior for all possible inputs has the advantage of not needing to list limitations, yet may not be functional needed (and code over-kill).  

Still good to post limitations.

On this last point, perhaps a slight code re-write is in order as one could easily expect n == bit_width to be valid.

// This assumes no padding in `unsigned`
#define UINT_BIT_WIDTH (CHAR_BIT * sizeof(unsigned))

unsigned setbits(unsigned x, int p, int n, unsigned y) {
  unsigned new_mask = n &gt;= UINT_BIT_WIDTH ? UINT_MAX : ~(~0u &lt;&lt; n);
  ...
}