Advertisement
STANAANDREY

k&r 2-7

Oct 23rd, 2022
889
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #define bits(x) sizeof(x) * 8
  3.  
  4. void showBits(unsigned x) {
  5.   for (int i = bits(x) - 1; i >= 0; i--) {
  6.     printf("%d", 1 & (x >> i));
  7.   }
  8.   puts("");
  9. }
  10.  
  11. unsigned invert(unsigned x,unsigned p, unsigned n) {
  12.   return x ^ (((1 << n) - 1) << (p - n + 1));
  13. }
  14.  
  15. int main() {
  16.   unsigned x, p, n;
  17.   printf("x="); scanf("%u", &x);
  18.   printf("p="); scanf("%u", &p);
  19.   printf("n="); scanf("%u", &n);
  20.   showBits(x);
  21.   showBits(invert(x, p, n));
  22.   return 0;
  23. }
  24.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement