daily pastebin goal
23%
SHARE
TWEET

dasdas

a guest Jan 17th, 2018 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4. #include <math.h>
  5. #include <stdint.h>
  6.  
  7. uint8_t getMask(uint8_t index, uint8_t length)
  8. {
  9.     uint8_t Bits = 00000000;
  10.     if(length!=0)
  11.     {
  12.     for (int i = length; i > 0; i--)
  13.     {
  14.         Bits |= 1 << index;
  15.         index--;
  16.     }
  17.     }
  18.     return Bits;
  19. }
  20.  
  21. void displayBits(uint8_t value)
  22. {
  23.     unsigned int i; // loop counter
  24.                     // define mask and left shift 31 bits
  25.     const unsigned int SHIFT = 8 * sizeof(value) - 1;
  26.     const unsigned int MASK = 1 << SHIFT;
  27.  
  28.     printf("0X%1.2X (%3u = ", value,value);
  29.  
  30.     // loop through bits
  31.     for (i = 1; i <= SHIFT + 1; ++i) {
  32.         putchar(value & MASK ? '1' : '0');
  33.         value <<= 1; // shift value left by 1
  34.  
  35.         if (i % 8 == 0) { // output space after 8 bits
  36.             putchar(' ');
  37.         } // end if
  38.     } // end for
  39.  
  40.     puts(")");
  41. } // end function displayBits
  42.  
  43. void bitnInvert(uint8_t* bytePtr, uint8_t index, uint8_t length)
  44. {
  45.     uint8_t value = getMask(index, length) &~*bytePtr;
  46.     displayBits(value);
  47. }
  48.  
  49. void bitnSet(uint8_t* bytePtr, uint8_t index, uint8_t length)
  50. {
  51.     uint8_t value = getMask(index, length) &~*bytePtr;
  52.     displayBits(value);
  53. }
  54.  
  55. void bitnClear(uint8_t* bytePtr, uint8_t index, uint8_t length)
  56. {
  57.     uint8_t value = getMask(index, length) &~*bytePtr;
  58.     displayBits(value);
  59. }
  60.  
  61.  
  62. int main(void)
  63. {
  64.     srand(time(NULL));
  65.     int Length = 0;
  66.     int Index = 0;
  67.     printf("Erzeugung einer Bitmaske (8 Bit)\n Position der ersten '1' eingeben (0-7) : ");
  68.     scanf_s("%d", &Index);
  69.     printf("Anzahl der '1' eingeben (0-8) : ");
  70.     scanf_s("%d", &Length);
  71.     printf("\n");
  72.     printf("Bitmaske: ",getMask(Index, Length));
  73.     displayBits(getMask(Index, Length));
  74.     printf("\n");
  75.  
  76.     for (int i = 3; i > 0; i--)
  77.     {
  78.         int number = (0 + rand() % 255);
  79.         printf("Byte ");
  80.         displayBits(number);
  81.         printf("Pos. %d, Laenge %d inventiert: ", Index, Length);
  82.         bitnInvert(00000000,Index,Length);
  83.         printf("\n");
  84.     }
  85.     printf("\n");
  86. }
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