Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include "lab01b_printBits_2.hpp"
- // Two different implementations of a function that prints
- // the contents of an unsigned integer bit by bit.
- void dumpBits0(bit_t v, int digitsPrinted)
- {
- if( v == 0 ) {
- int leadingZerosToPrint = sizeof(bit_t) * 8 - digitsPrinted;
- for( int i = 0; i < leadingZerosToPrint; i++ )
- std::cout << 0;
- if( digitsPrinted == 0 )
- std::cout << std::endl;
- return;
- }
- dumpBits0(v / 2, digitsPrinted + 1);
- std::cout << ( v % 2 ? 1 : 0 );
- if( digitsPrinted == 0 )
- std::cout << std::endl;
- }
- void dumpBits(bit_t v)
- {
- bit_t pos = 1;
- for( pos <<= sizeof(bit_t) * 8 - 1; pos; pos >>= 1 ) {
- std::cout << (v & pos ? 1 : 0);
- }
- std::cout << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement