Byte.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include<iostream>
  2. #include<bitset>
  3. #include<iomanip>
  4. #include<vector>
  5. using Byte = unsigned char ;
  6. const int err(const int code, const std::string err){
  7. std::cerr << err << std::endl;
  8. return code;
  9. }
  10. struct BitsInByte{
  11. Byte ones;
  12. Byte zeros;
  13. Byte delim;//count of first ones
  14. };
  15. BitsInByte countMyBits( Byte byte ){
  16. std::bitset<8> bits_header(byte);
  17. BitsInByte retval{0,0,0};
  18. //std::vector<bool> bits = dynamic_cast<std::vector<bool>>(bits_header);
  19. auto & bits = bits_header;
  20. bool oneFound=false;
  21. std::cout << bits_header << " имеет - "<< std::endl;
  22. std::cout << bits.count() << " Единиц ";
  23. std::cout << (bits.size()-bits.count()) << " нулей.\n";
  24. for(int i =7; i>=0;i--){
  25. std::cout << i << ":" << bits[i] << std::endl;
  26. if(bits[i]==1) {
  27. retval.ones++;
  28. if( !oneFound ) retval.delim++;;
  29. }else{
  30. if( !oneFound )oneFound=true;
  31. retval.zeros++;
  32. }
  33. }
  34. return retval;
  35. }
  36. int main(int argc, char * argv[]){
  37. if ( argc < 2 ) return err(1, (argv[0] + std::string(" byte") ));
  38. Byte searchbyte;
  39. if ( argv[1][0] >= '0' && argv[1][0] <= '9' ) searchbyte=atoi(argv[1]);
  40. else searchbyte=argv[1][0];
  41. std::cout << "Search: " << short(searchbyte) << std::endl;
  42. auto myCount = countMyBits(searchbyte);
  43. printf(" count of zeros %d ;count of ones %d;лидирующие(?) %d\n ", myCount.zeros, myCount.ones, myCount.delim);
  44. }