Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- unsigned setbits(unsigned x, unsigned p, unsigned n, unsigned y) {
- unsigned mask = (1 << n) - 1;
- unsigned rmy = mask & y;
- return (x & (~mask << (p - n + 1))) | rmy;
- }
- void showBits(unsigned a){
- for(int i = sizeof(a) * 8 - 1; i>= 0; i--)
- printf("%d", ( a >> i) & 1);
- printf("\n");
- }
- unsigned read(char ch) {
- unsigned nr;
- printf("%c=", ch); scanf("%u", &nr);
- return nr;
- }
- int main() {
- unsigned x, p, n, y;
- x = read('x'), p = read('p'), n = read('n'), y = read('y');
- showBits(x);
- showBits(y);
- unsigned ans = setbits(x, p, n, y);
- //printf("%u\n", ans);
- showBits(ans);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement