Advertisement
STANAANDREY

replace byte in nr

Oct 26th, 2022
729
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3.  
  4. uint64_t replaceByte(uint64_t nr, uint8_t n, int p) {
  5.   uint64_t shiftedN = n << (p * 8);
  6.   uint64_t mask = 0xFF << (p * 8);
  7.   return (~mask & nr) | shiftedN;
  8. }
  9.  
  10. void showBits(uint64_t x) {
  11.   for (int i = 63; i >= 0; i--) {
  12.     printf("%llu", (x >> i) & 1);
  13.     if (i % 4 == 0) {
  14.       putchar(' ');
  15.     }
  16.   }
  17.   puts("");
  18. }
  19.  
  20. int main() {
  21.   uint64_t nr;
  22.   uint8_t n;
  23.   int p;
  24.  
  25.   printf("nr="); scanf("%llu", &nr);
  26.   printf("n="); scanf("%hhu", &n);
  27.   printf("p="); scanf("%d", &p);
  28.  
  29.   showBits(nr);
  30.   showBits(n << (p * 8ULL));
  31.   showBits(replaceByte(nr, n, p));
  32.   return 0;
  33. }
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement