Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #include <iomanip>
  6.  
  7. typedef unsigned char byte;
  8.  
  9. #define RED 4
  10. #define GREEN 2
  11. #define BLUE 1
  12.  
  13. typedef struct
  14. {
  15. char status; // 1 bit
  16. char brightness; // 4 bit
  17. char red; // 1 bit
  18. char green; // 1 bit
  19. char blue; // 1 bit
  20. } light;
  21.  
  22. void prnBin(byte b)
  23. {
  24. for (int i = 7; i >= 0; --i)
  25. {
  26. printf("%d", b >> i & 1);
  27. }
  28. printf("\n");
  29. }
  30.  
  31. unsigned char pack(light a)
  32. {
  33. byte lightSettings = a.status << 7;
  34. lightSettings |= a.brightness << 3;
  35. lightSettings |= a.red << 2;
  36. lightSettings |= a.green << 1;
  37. lightSettings |= a.blue;
  38. prnBin(lightSettings);
  39. printf("%x\n", lightSettings);
  40. return lightSettings;
  41. }
  42. void unpack(unsigned char SettingsCode,light settings)
  43. {
  44.  
  45.  
  46. printf("status = %d ", SettingsCode >> 7 & 1);
  47. settings.status = SettingsCode >> 7 & 1;
  48. int c = 0;
  49. for (int i = 6,k = 8; i > 2; --i,k/=2)
  50. {
  51. if (SettingsCode >> i & 1 > 0)
  52. {
  53. c += k;
  54. }
  55.  
  56.  
  57. }
  58. printf("brigtness = %d ", c);
  59. settings.brightness = c;
  60.  
  61. printf("red = %d ", SettingsCode >> 2 & 1);
  62. settings.red = SettingsCode >> 2 & 1;
  63.  
  64. printf("green = %d ", SettingsCode >> 1 & 1);
  65. settings.green = SettingsCode >> 1 & 1;
  66.  
  67. printf("blu = %d\n", SettingsCode >> 0 & 1);
  68. settings.blue = SettingsCode >> 0 & 1;
  69.  
  70. }
  71.  
  72. byte setColor(byte a, byte color)
  73. {
  74. return a | color;
  75. }
  76.  
  77. int main(int argc, char argv[])
  78. {
  79.  
  80.  
  81. light light1;
  82. light1.blue = 0;
  83. light1.red = 0;
  84. light1.green = 0;
  85. light1.brightness = 7;
  86. light1.status = 1;
  87. byte temp = pack(light1);
  88.  
  89. unpack(pack(light1),light1);
  90. prnBin(setColor(temp, GREEN | BLUE));
  91.  
  92. byte a = 0xff;
  93. unpack(a, light1);
  94. printf("light\n%d\n %d\n %d\n %d\n %d\n", light1.status, light1.brightness, light1.red, light1.green, light1.blue);
  95. system("pause");
  96. return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement