Byte.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct lools_foracetone{
  4. int a:2;
  5. int b:2;
  6. int c:2;
  7. int d:1;
  8. int e:1;
  9. };// бля a 2 бита, для б2 ))))
  10. struct BitsInByte{
  11. unsigned char ones;
  12. unsigned char zeros;
  13. unsigned char delim;//count of first ones
  14. };
  15. typedef unsigned char Byte;
  16. typedef enum{false,true}bool;
  17. struct BitsInByte countMyBits( Byte byte ){
  18. bool oneFound=false;
  19. struct BitsInByte retval={0,0};
  20. char examplebinary[9];
  21. examplebinary[8]=0;
  22. for(int i =0; i < 8; i++ ){
  23. unsigned char bit = (byte << i) & 0x80;
  24. printf("%d bit\n", bit);
  25. if(bit > 0){
  26. if( !oneFound ) retval.delim++;;
  27. retval.ones++;
  28. examplebinary[i]='1';
  29. }
  30. else{
  31. if( !oneFound )oneFound=true;
  32. retval.zeros++;
  33. examplebinary[i]='0';
  34. }
  35. }
  36. printf("Example binary: %s\n", examplebinary);
  37. return retval;
  38. }
  39. int main(int argCount, char**arguments){
  40. if( argCount < 2 )
  41. return fprintf(stderr, "%s [byte]/[number < 255]\n", arguments[0]);
  42. Byte searchbyte;
  43. if ( arguments[1][0] >= 48 && arguments[1][0] <= '9' ) searchbyte=atoi(arguments[1]);
  44. else searchbyte=arguments[1][0];
  45. printf("Search %d\n", searchbyte);
  46. struct BitsInByte myCount = countMyBits( searchbyte );
  47. printf(" count of zeros %d ;count of ones %d;лидирующие(?) %d\n ", myCount.zeros, myCount.ones, myCount.delim);
  48. }