Public paste
recurse
By: j00 | Date: Nov 19 2007 22:41 | Format: C++ | Expires: never | Size: 582 B | Hits: 836

  1. #include <iostream>
  2.  
  3.  
  4.  
  5. using namespace std;
  6.  
  7.  
  8. template < typename T >
  9. inline T highbit(T& t)
  10. {
  11. return t = (((T)(-1)) >> 1) + 1;
  12. }
  13.  
  14. template < typename T >
  15. ostream& bin(T& value, ostream &o)
  16. {
  17.            
  18.        
  19. for ( T bit = highbit(bit); bit; bit >>= 1 )
  20. {    
  21. o << ( ( value & bit ) ? '1' : '0' );
  22. }
  23.  
  24. return o;
  25. }
  26.  
  27.  
  28. int main()
  29. {
  30.  
  31.  
  32.  
  33. unsigned long value;
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40. cout << "Enter number: " << endl;
  41. cin>>value;
  42. cout << "dec: " << dec << value << endl;
  43. cout << "bin: ";
  44. bin(value, cout);
  45. cout << endl;
  46.  
  47.  
  48.  
  49. system("pause");
  50. return 0;
  51. }