#include #include #include "02-bitwise.h" unsigned setbits(unsigned x, int p, int n, unsigned y); /* parse command line args for setbits, print result */ int main(int argc, char *argv[]) { if (argc < 5) { printf("error : expected 4 arguments x p n y\n"); return 1; } unsigned x, y; int bx = int_of_string(&x, argv[1]); int p = atoi(argv[2]); int n = atoi(argv[3]); int by = int_of_string(&y, argv[4]); if (!bx || !by) { return 2; } printd_base(bx, setbits(x, p, n, y)); putchar('\n'); return 0; } /* setbits: replace n bits in position p (right adjusted) in * x with the rightmost n bits of y, return the result */ unsigned setbits(unsigned x, int p, int n, unsigned y) { unsigned mask = ~(~0 << n); return ((y & mask) << (p+1-n)) | (x & ~(mask << (p+1-n))); }