SHARE
TWEET

C - simple packed bit display example

a guest Dec 17th, 2017 36 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*Packed pixels example 8x8 1-bit -- striped in y axis */
  2. /*(4x4)
  3. 1111
  4. 2222
  5. 4444
  6. 8888
  7. */
  8. #include <stdio.h>
  9. const int pixmap[]={0x1,0x2,0x4,0x8,0x10,0x20,0x40,0x80};
  10. unsigned char xy[]={0xFF,0x81,0x89,0xFF,0xFF,0x89,0xC81,0xFF};
  11.  
  12. void display(void)
  13. {
  14.     for (int j = 0; j<8;j++)
  15.     {
  16.         for (int i = 0; i<8;i++)
  17.         {
  18.             if (xy[i] & pixmap[j])
  19.                 printf("*");
  20.             else
  21.                 printf(" ");
  22.         }
  23.         printf("\n");
  24.     }
  25. }
  26.  
  27. unsigned char set_pix(int x, int y, unsigned char val)
  28. {
  29.     int mask = pixmap[y & 7];
  30.     val <<= ((y & 7));
  31.     xy[x] &= ~mask;
  32.     xy[x] |= (val & mask);
  33. }
  34.  
  35. unsigned char get_pix(int x, int y)
  36. {
  37.     int mask = pixmap[y & 7];
  38.     return (xy[x] & mask)>>(y & 7);
  39. }
  40.  
  41. int main()
  42. {
  43.     display();
  44.     set_pix(0, 5, 0xF0);
  45.     display();
  46.     printf("%d", get_pix(0,5));
  47.     return 0;
  48. }
  49. /*********************************************************************************/
  50. /*********************************************************************************/
  51. /*********************************************************************************/
  52. /*Packed pixels example 8x8 1-bit -- striped in x axis */
  53. /* (4x4)
  54. 1248
  55. 1248
  56. 1248
  57. 1248
  58. */
  59. #include <stdio.h>
  60. const int pixmap[]={0x1,0x2,0x4,0x8,0x10,0x20,0x40,0x80};
  61. unsigned char xy[]={0xFF,0x81,0x89,0xFF,0xFF,0x89,0xC81,0xFF};
  62.  
  63. void display(void)
  64. {
  65.     for (int j = 0; j<8;j++)
  66.     {
  67.         for (int i = 0; i<8;i++)
  68.         {
  69.             if (xy[j] & pixmap[i])
  70.                 printf("*");
  71.             else
  72.                 printf(" ");
  73.         }
  74.         printf("\n");
  75.     }
  76. }
  77. unsigned char set_pix(int x, int y, unsigned char val)
  78. {
  79.     int mask = pixmap[x & 7];
  80.     val <<= ((x & 7));
  81.     xy[y] &= ~mask;
  82.     xy[y] |= (val & mask);
  83. }
  84. unsigned char get_pix(int x, int y)
  85. {
  86.     int mask = pixmap[x & 7];
  87.     return (xy[y] & mask)>>(x & 7);
  88. }
  89. int main()
  90. {
  91.     display();
  92.     set_pix(5, 0, 0xF0);
  93.     display();
  94.     printf("%d", get_pix(5,0));
  95.     return 0;
  96. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top