Advertisement
alexander_seredinov

Untitled

Feb 22nd, 2022
968
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.05 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3.  
  4. #define BYTE_TO_BINARY_PATTERN "%c%c%c%c%c%c%c%c"
  5. #define BYTE_TO_BINARY(byte)  \
  6.   (byte & 0x80 ? '1' : '0'), \
  7.   (byte & 0x40 ? '1' : '0'), \
  8.   (byte & 0x20 ? '1' : '0'), \
  9.   (byte & 0x10 ? '1' : '0'), \
  10.   (byte & 0x08 ? '1' : '0'), \
  11.   (byte & 0x04 ? '1' : '0'), \
  12.   (byte & 0x02 ? '1' : '0'), \
  13.   (byte & 0x01 ? '1' : '0')
  14.  
  15.  
  16. //--------------------------------------------------------------
  17.  
  18. uint8_t scratch[16] = {
  19.     0xFF, 0x00, 0x00, 0xFF,
  20.     0x00, 0xFF, 0x00, 0x00,
  21.     0x00, 0x00, 0xFF, 0x00,
  22.     0xFF, 0x00, 0x00, 0x00,
  23. };
  24.  
  25.  
  26. int main()
  27. {
  28.     uint16_t tt = 0;
  29.  
  30.     for(int i = 0; i < 16; i++) {
  31.         if (scratch[i] == 0xFF)
  32.             tt = (tt>>1) | 0x8000;
  33.         else
  34.             tt = tt>>1;
  35.  
  36.         printf("i=%02d (0x%02X) tt=0x%04X " BYTE_TO_BINARY_PATTERN BYTE_TO_BINARY_PATTERN "\n",
  37.                i,
  38.                scratch[i],
  39.                tt,
  40.                BYTE_TO_BINARY(((tt>>8) & 0xFF)),
  41.                BYTE_TO_BINARY(((tt) & 0xFF)));
  42.     }
  43.  
  44.     return 0;
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement