Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Traductor decimal binario Antonio Villanueva
- #include <iostream>
- #include <cmath>
- using namespace std;
- #define MIN 0
- #define MAX 255
- /*----------------------------------------------------------------------*/
- string binario(int decimal){//calculo binario
- string bin("");//cadena representa binario
- int exp(0);//exponente maximo 2^exp en el rango del numero decimal
- while (pow(2,++exp) <=decimal) {};//Maxima potencia de 2 relativa al numero
- while (exp>=0){bin+= (decimal & (int)(pow(2,exp--)) ) ? "1" :"0";}//and ultimo bit}
- return bin;
- };
- /*----------------------------------------------------------------------*/
- /*----------------------------------------------------------------------*/
- int main (){
- for (int decimal=MIN ;decimal<=MAX;decimal++){
- cout <<"num =" <<decimal<< " bin= "<< binario(decimal)<<endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement