Advertisement
STANAANDREY

byte concat

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