Enviado em 07/12/2017 - 20:18h
Pessoal estou com o seguinte codigo abaixo que converte numeros inteiros em binario#include <algorithm> // reverse #include <climits> // CHAR_BIT #include <iostream> #include <sstream> struct Bin { int b; friend std::ostream& operator<<(std::ostream& os, const Bin& f); std::string binary(unsigned int n) { b=n; //converte numero para string de bits std::stringstream bitsInReverse; //int nBits = sizeof(b) * CHAR_BIT; unsigned int nBits = sizeof(b) * 3; while (nBits-- > 0) { bitsInReverse << (b & 1); b >>= 1; } std::string bits(bitsInReverse.str()); std::reverse(bits.begin(), bits.end()); return bits; } }bin; std::ostream &operator<<( std::ostream &saida, const Bin& f) { saida << f.binary(f.b); return saida; } int main() { //127 em binario 000001111111 std::cout << "\n\t127 em hexadecimal: " << std::hex << 127 << "\n\t127 em octal: " << std::oct << 127 << "\n\t127 em decimal: " << std::dec << 127 << "\n\t127 em binario: " << bin << 127 <<"\n\n"; }
Enviado em 08/12/2017 - 00:44h
Problema de fundo: um ponteiro constante não pode ser convertido implicitamente para ponteiro não-constante, pois isso violaria garantias do sistema de tipos.class X { private: std::string _str; public: void str(const std::string &nova_str){ // Altera a string. Função exige objeto não-constante. _str=nova_str; } std::string str() const { // <--- Note o “const” após a lista de parâmetros. // Retorna uma cópia da string. Não altera o objeto, que pode ser constante ou não. return _str; // Gera uma cópia do valor de _str. } };
Enviado em 08/12/2017 - 16:36h
class X { private: std::string _str; public: void str(const std::string &nova_str){ // Altera a string. Função exige objeto não-constante. _str=nova_str; } std::string str() const { // <--- Note o “const” após a lista de parâmetros. // Retorna uma cópia da string. Não altera o objeto, que pode ser constante ou não. return _str; // Gera uma cópia do valor de _str. } };
Enviado em 12/12/2017 - 13:44h
Enviado em 12/12/2017 - 22:03h
você não poderia utilizar std::bitset ( http://en.cppreference.com/w/cpp/utility/bitset ) para facilitar ?//g++ -O2 -Wall -Wpedantic -std=c++11 -o binary binary.cpp #include <iostream> #include <string> #include <bitset> #include <type_traits> template<typename T> typename std::enable_if<std::is_integral<T>::value, std::string>::type binary(const T& n){ return std::bitset<sizeof(T)*8>(n).to_string(); } int main() { std::cout << binary(127) << std::endl; return 0; }
Enviado em 13/12/2017 - 10:18h
std::cout << "\n\t127 em hexadecimal: " << std::hex << 127 << "\n\t127 em octal: " << std::oct << 127 << "\n\t127 em decimal: " << std::dec << 127 << "\n\t127 em binario: " << bin << 127 <<"\n\n";
Enviado em 13/12/2017 - 10:37h
Caro dark777,std::cout << std::dec << 127 << '\n' // Primeiro imprime o nº em decimal << binary(127) << '\n' // e depois em binário. ;
#include <ostream> #include <stdexcept> #include <string> #include <cstdint> using namespace std; class my_binary_conversion_t { private: ostream *p_out; friend my_binary_conversion_t &operator<<(ostream &, my_binary_conversion_t &); public: my_binary_conversion_t(): p_out(nullptr) { } ostream &operator<<(uintmax_t val){ if(!p_out) throw logic_error("manipulator not tied to any output stream"); string s; // Faz com que ‘s’ receba a representação binária de ‘val’ — pode ser // usando a sugestão do phoemur ou alguma outra técnica. ostream &out=*p_out; p_out=nullptr; // Limpa o ponteiro, para evitar que se use apenas o manipulador, em lugar do stream. return out << s; } } bin; my_binary_conversion_t &operator<<(ostream &out, my_binary_conversion_t &wrapper){ wrapper.p_obj=&out; return wrapper; // Note que eu retorno uma referência para o wrapper, não para o stream. }
Enviado em 13/12/2017 - 17:19h
//g++ -O2 -Wall -Wpedantic -std=c++11 -o binary binary.cpp #include <iostream> #include <string> #include <bitset> #include <type_traits> template<typename T> typename std::enable_if<std::is_integral<T>::value, std::string>::type binary(const T& n){ return std::bitset<sizeof(T)*8>(n).to_string(); } int main() { std::cout << binary(127) << std::endl; return 0; }
Enviado em 13/12/2017 - 17:38h
std::cout << std::dec << 127 << '\n' // Primeiro imprime o nº em decimal << binary(127) << '\n' // e depois em binário. ;
#include <ostream> #include <stdexcept> #include <string> #include <cstdint> using namespace std; class my_binary_conversion_t { private: ostream *p_out; friend my_binary_conversion_t &operator<<(ostream &, my_binary_conversion_t &); public: my_binary_conversion_t(): p_out(nullptr) { } ostream &operator<<(uintmax_t val){ if(!p_out) throw logic_error("manipulator not tied to any output stream"); string s; // Faz com que ‘s’ receba a representação binária de ‘val’ — pode ser // usando a sugestão do phoemur ou alguma outra técnica. ostream &out=*p_out; p_out=nullptr; // Limpa o ponteiro, para evitar que se use apenas o manipulador, em lugar do stream. return out << s; } } bin; my_binary_conversion_t &operator<<(ostream &out, my_binary_conversion_t &wrapper){ wrapper.p_obj=&out; return wrapper; // Note que eu retorno uma referência para o wrapper, não para o stream. }
#include <ios> //ios_base #include <climits> // CHAR_BIT #include <sstream> #include <iostream> #include <algorithm> // reverse struct BinStream { 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; } std::ostream& os; BinStream(std::ostream& os): os(os) {} template<class T> BinStream& operator<<(T& value) { os << value; return *this; } BinStream& operator<<(int value) { os << binario(value); return *this; } /* std::ostream& operator<<(std::ios_base& (__cdecl *_Pfn)(std::ios_base&)) { return os <<_Pfn; } */ }; struct Bin { friend BinStream operator<<(std::ostream& os, const Bin& f); }bin; BinStream operator<<(std::ostream& os, const Bin& f) { return BinStream(os); } int main() { std::cout << "\n\t127 em decimal: " << std::dec << 127 << "\n\t127 em octal: " << std::oct << 127 << "\n\t127 em hexadecimal: " << std::hex << 127 << "\n\t127 em binario: " << bin << 12 << "\n\n"; }
std::ostream& operator<<(std::ios_base& (__cdecl *_Pfn)(std::ios_base&)) { return os <<_Pfn; }
error: expected ‘,’ or ‘...’ before ‘(’ token std::ostream& operator<<(std::ios_base& (__cdecl *_Pfn)(std::ios_base&)) ^ operatorBin2.cxx: In member function ‘std::ostream& std::BinStream::operator<<(std::ios_base&)’: operatorBin2.cxx:49:16: error: ‘_Pfn’ was not declared in this scope return os <<_Pfn;
std::cout << "\n\t127 em binario: " << bin << 127 << "\n\t127 em octal: " << std::oct << 127 << "\n\t127 em hexadecimal: " << std::hex << 127 << "\n\t127 em decimal: " << std::dec << 127 << "\n\n";
127 em binario: 0001111111 127 em octal: 0001111111 127 em hexadecimal: 0001111111 127 em decimal: 0001111111
std::cout << "\n\t127 em binario: " << bin << 127 << "\n\t127 em octal: " << std::oct << 127 << "\n\t127 em binario: " << bin << 127 << "\n\t127 em hexadecimal: " << std::hex << 127 << "\n\t127 em binario: " << bin << 127 << "\n\t127 em decimal: " << std::dec << 127 << "\n\t127 em binario: " << bin << 127 << "\n\n";*/
127 em binario: 0001111111 127 em octal: 177 127 em binario: 0001111111 127 em hexadecimal: 7f 127 em decimal: 127 127 em binario: 0001111111
Enviado em 15/12/2017 - 01:14h
#if !defined(_WIN16) && !defined(_WIN32) && !defined(_WIN64) #undef __cdecl #undef __syscall #undef __clrcall #undef __thiscall #undef __fastcall #undef __vectorcall // Que porre! #define __cdecl #define __syscall #define __clrcall #define __thiscall #define __fastcall #define __vectorcall #endif
std::ostream &operator<<(decltype(std::dec) &_Pfn){ /* bla, bla, bla */ }
Enviado em 15/12/2017 - 03:59h
#if !defined(_WIN16) && !defined(_WIN32) && !defined(_WIN64) #undef __cdecl #undef __syscall #undef __clrcall #undef __thiscall #undef __fastcall #undef __vectorcall // Que porre! #define __cdecl #define __syscall #define __clrcall #define __thiscall #define __fastcall #define __vectorcall #endif
std::ostream &operator<<(decltype(std::dec) &_Pfn){ /* bla, bla, bla */ }
Instalação e configuração do Chrony
Programa IRPF - Guia de Instalação e Resolução de alguns Problemas
Criando uma Infraestrutura para uma micro Empresa
O Que Fazer Após Instalar Ubuntu 25.04
O Que Fazer Após Instalar Fedora 42
Debian 12 -- Errata - Correções de segurança
Instalando o Pi-Hole versão v5.18.4 depois do lançamento da versão v6.0
modo de emergencia no linux [RESOLVIDO] (1)
Como criar um arquivo ISO de um sistema personalizado (3)