Advertisement
STANAANDREY

concat 2 nr on bits

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