RicardasSim

padding bits

Feb 25th, 2024 (edited)
1,617
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4.  
  5. typedef struct {
  6.     bool b1:1;
  7.     bool b2:1;
  8.     unsigned char ch;
  9.     bool b3:1;
  10.     bool b4:1;
  11. } st1;
  12.  
  13. typedef struct {
  14.     bool b1;
  15.     bool b2;
  16.     unsigned char c;
  17.     bool b3;
  18.     bool b4;
  19. } st2;
  20.  
  21.  
  22. void PrintBinary(unsigned char n )
  23. {
  24.     for ( int i = 7; i >= 0; --i )
  25.     {
  26.         printf("%d", ( n >> i ) & 1);
  27.     }
  28. }
  29.  
  30. void PrintBool(bool v)
  31. {
  32.     printf( "%s ", v ? "true" : "false" );
  33. }
  34.  
  35. int main()
  36. {
  37.  
  38.     st1 s1 = {0};
  39.     st2 s2 = {0};
  40.     unsigned char *p;
  41.  
  42.     printf( "sizeof s1: %zu\n", sizeof s1 );
  43.  
  44.     p = (unsigned char *)&s1;
  45.  
  46.     printf("(A 1): ");
  47.  
  48.     for ( unsigned int i = 0; i < sizeof s1; ++i )
  49.     {
  50.         PrintBinary( *(p + i) );
  51.         printf(" ");
  52.     }
  53.  
  54.     printf("\n");
  55.  
  56.     PrintBool( s1.b1 );
  57.     PrintBool( s1.b2 );
  58.     PrintBool( s1.b3 );
  59.     PrintBool( s1.b4 );
  60.     printf("\n");
  61.  
  62.     s1.b2 = true;
  63.  
  64.     printf("(A 2): ");
  65.  
  66.     for ( unsigned int i = 0; i < sizeof s1; ++i )
  67.     {
  68.         PrintBinary( *(p + i) );
  69.         printf(" ");
  70.     }
  71.  
  72.     printf("\n");
  73.  
  74.     PrintBool( s1.b1 );
  75.     PrintBool( s1.b2 );
  76.     PrintBool( s1.b3 );
  77.     PrintBool( s1.b4 );
  78.     printf("\n");
  79.  
  80.     s1.b3 = true;
  81.  
  82.     printf("(A 3): ");
  83.  
  84.     for ( unsigned int i = 0; i < sizeof s1; ++i )
  85.     {
  86.         PrintBinary( *(p + i) );
  87.         printf(" ");
  88.     }
  89.  
  90.     printf("\n");
  91.  
  92.     PrintBool( s1.b1 );
  93.     PrintBool( s1.b2 );
  94.     PrintBool( s1.b3 );
  95.     PrintBool( s1.b4 );
  96.     printf("\n");
  97.  
  98.     printf( "sizeof s2: %zu\n", sizeof s2 );
  99.  
  100.     p = (unsigned char *)&s2;
  101.  
  102.     printf("(B 1): ");
  103.  
  104.     for ( unsigned int i = 0; i < sizeof s2; ++i )
  105.     {
  106.         PrintBinary( *(p + i) );
  107.         printf(" ");
  108.     }
  109.  
  110.     printf("\n");
  111.  
  112.     PrintBool( s2.b1 );
  113.     PrintBool( s2.b2 );
  114.     PrintBool( s2.b3 );
  115.     PrintBool( s2.b4 );
  116.     printf("\n");
  117.  
  118.     s2.b2 = true;
  119.  
  120.     printf("(B 2): ");
  121.  
  122.     for ( unsigned int i = 0; i < sizeof s2; ++i )
  123.     {
  124.         PrintBinary( *(p + i) );
  125.         printf(" ");
  126.     }
  127.  
  128.     printf("\n");
  129.  
  130.     PrintBool( s2.b1 );
  131.     PrintBool( s2.b2 );
  132.     PrintBool( s2.b3 );
  133.     PrintBool( s2.b4 );
  134.     printf("\n");
  135.  
  136.     s2.b3 = true;
  137.  
  138.     printf("(B 3): ");
  139.  
  140.     for ( unsigned int i = 0; i < sizeof s2; ++i )
  141.     {
  142.         PrintBinary( *(p + i) );
  143.         printf(" ");
  144.     }
  145.  
  146.     printf("\n");
  147.  
  148.     PrintBool( s2.b1 );
  149.     PrintBool( s2.b2 );
  150.     PrintBool( s2.b3 );
  151.     PrintBool( s2.b4 );
  152.     printf("\n");
  153.  
  154. }
  155.  
  156. /*
  157.  
  158. output:
  159.  
  160. sizeof s1: 3
  161. (A 1): 00000000 00000000 00000000
  162. false false false false
  163. (A 2): 00000010 00000000 00000000
  164. false true false false
  165. (A 3): 00000010 00000000 00000001
  166. false true true false
  167. sizeof s2: 5
  168. (B 1): 00000000 00000000 00000000 00000000 00000000
  169. false false false false
  170. (B 2): 00000000 00000001 00000000 00000000 00000000
  171. false true false false
  172. (B 3): 00000000 00000001 00000000 00000001 00000000
  173. false true true false
  174.  
  175. */
  176.  
Advertisement