SHARE
TWEET

Aufgabe 1b

RandomGuy32 Mar 22nd, 2019 17 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2.  
  3.  
  4. int main()
  5. {
  6.     unsigned int x;
  7.     unsigned int y;
  8.  
  9.     unsigned int p;
  10.     unsigned int n;
  11.  
  12.     printf("Gib eine Zahl x ein.\n");
  13.     scanf("%d", &x);
  14.  
  15.     printf("Gib eine Zahl y ein.\n");
  16.     scanf("%d", &y);
  17.  
  18.     printf("Gib eine Position p ein.\n");
  19.     scanf("%d", &p);
  20.  
  21.     printf("Gib eine Anzahl n ein.\n");
  22.     scanf("%d", &n);
  23.  
  24.     unsigned int m = 1;
  25.  
  26.     unsigned int offset = p - n + 1;
  27.  
  28.     for (int i = 0; i < n; i++) {
  29.         if (x & (m << offset)) {
  30.             if (y & m) {
  31.                 x = x | ((y & m) << offset);
  32.             }
  33.             else {
  34.                 x = (x & ~(m << offset));
  35.             }
  36.         }
  37.         else {
  38.             x = x | ((y & m) << offset);
  39.         }
  40.  
  41.         m = m << 1;
  42.     }
  43.  
  44.     printf("Ergebnis: %d\n", x);
  45. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top