Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm> // reverse
- #include <climits> // CHAR_BIT
- #include <iostream>
- #include <sstream>
- std::string binario(unsigned int n)
- {
- //converte numero para string de bits
- std::stringstream bitsInReverse;
- //int nBits = sizeof(n) * CHAR_BIT;
- unsigned int nBits = sizeof(n)*2.5;
- while (nBits-- > 0)
- {
- bitsInReverse << (n & 1);
- n >>= 1;
- }
- std::string bits(bitsInReverse.str());
- std::reverse(bits.begin(), bits.end());
- return bits;
- }
- struct Bin
- {
- friend std::ostream& operator<<(std::ostream& os, const Bin& f);
- int a;
- }bin;
- std::ostream &operator<<( std::ostream &saida, const Bin& f)
- {
- int c(f.a);
- //converte numero para string de bits
- std::stringstream bitsInReverse;
- //int nBits = sizeof(c) * CHAR_BIT;
- unsigned int nBits = sizeof(c) * 3;
- while (nBits-- > 0)
- {
- bitsInReverse << (c & 1);
- c >>= 1;
- }
- std::string bits(bitsInReverse.str());
- std::reverse(bits.begin(), bits.end());
- saida << bits;
- return saida;
- }
- int main()
- {
- std::cout<< "\n\t127 em binario: " << binario(127)
- << "\n\t127 em octal: " << std::oct << 127
- << "\n\t127 em hexadecimal: " << std::hex << 127
- << "\n\t127 em decimal: " << std::dec << 127
- << "\n\t127 em binario: " << bin<<127<<"\n\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement