Advertisement
Guest User

Untitled

a guest
Aug 25th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. #include<iostream>
  2. #include "lab01b_printBits_2.hpp"
  3.  
  4. // Two different implementations of a function that prints
  5. // the contents of an unsigned integer bit by bit.
  6.  
  7. void dumpBits0(bit_t v, int digitsPrinted)
  8. {
  9. if( v == 0 ) {
  10. int leadingZerosToPrint = sizeof(bit_t) * 8 - digitsPrinted;
  11. for( int i = 0; i < leadingZerosToPrint; i++ )
  12. std::cout << 0;
  13. if( digitsPrinted == 0 )
  14. std::cout << std::endl;
  15. return;
  16. }
  17. dumpBits0(v / 2, digitsPrinted + 1);
  18. std::cout << ( v % 2 ? 1 : 0 );
  19. if( digitsPrinted == 0 )
  20. std::cout << std::endl;
  21. }
  22.  
  23. void dumpBits(bit_t v)
  24. {
  25. bit_t pos = 1;
  26. for( pos <<= sizeof(bit_t) * 8 - 1; pos; pos >>= 1 ) {
  27. std::cout << (v & pos ? 1 : 0);
  28. }
  29. std::cout << std::endl;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement