need assistance bargain "getbits()" slight section 2 k&r c
in section 2, domain bitwise operators (section 2.9), i'm carrying problem bargain illustration methods works.
here's slight provided:
unsigned int getbits(unsigned int x, int p, int n) {
relapse (x >> (p + 1 - n)) & ~(~0 << n);
}
the thought that, given array x, relapse n pieces starting during position p, counting right (with farthest right bit being position 0). given following main() method:
int main(void) {
int x = 0xf994, p = 4, n = 3;
int z = getbits(x, p, n);
printf("getbits(%u (%x), %d, %d) = %u (%x)\n", x, x, p, n, z, z);
relapse 0;
}
the cost is:
getbits(63892 (f994), 4, 3) = 5 (5)
i portions this, nonetheless am carrying problem "big picture," mostly since pieces (no fun intended) i don't understand.
the biased i'm personally carrying issues complements piece: ~(~0 << n). i cruise i initial part, traffic x; it's biased (and following mask) i'm struggling -- comes together indeed collect those bits. (which i've accurate doing, both formula checking law controlling calc.exe -- conclude God binary view!)
any help?
Comments
Post a Comment