|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Member (9 bit)
Join Date: Nov 2002
Posts: 502
|
C++ Program Help, Numbers conversor
Does anybody has an idea of how can I add a function to this program to display the Complement 1 and complement 2 of the inputed number?
For example the C1 and C2 of 10000001 C1 01111110 C2 01111111 // Este programa lee numeros en binario, decimal, y hex, he imprime // en binario, decimal, y hex. #include #include #include #include #include // Funciones para obtener los numeros en los dif formatos int get_binary(); int get_hex(); int get_decimal(); // Imprime los numeros en diferentes formatos void display(int); void display_binary(int); void display_hex(int); void display_decimal(int); // Explica como funciona el programa void help(void); // Imprime los digitos en binario o hex void print_binary_digit(int); void print_hex_digit(int); // La funcion main void main(void) { char c; int x; int done = 0; cout << "\nConversor de numeros\n"; // Imprime la funcion de ayuda help(); while(!done) { // Obtiene command cout << "Command: "; cout << flush; c = getch(); cout << c << "\n"; switch(c) { // Entrada binaria case 'b': cout << "Numero Binario: "; x = get_binary(); display(x); break; // Entrada Hexadecimal case 'h': cout << "Numero Hex: "; x = get_hex(); display(x); break; // Entrada decimal case 'd': cout << "Numero Decimal: "; x = get_decimal(); display(x); break; // Ayuda case '?': help(); break; // Salir case 'q': done = 1; break; // caso default - Imprime ayuda default: help(); break; } } } // Imprime ayuda void help(void) { cout << "\nIngrese d para decimal\n" << " h para hex\n" << " b para binario\n" << " q para salir\n" << " ? para ayuda\n\n"; } // Imprime el numero en los 3 formatos void display(int x) { cout << "\n\n"; display_hex(x); display_decimal(x); display_binary(x); cout << "\n"; } // Convierte un caracter a nuemro int ctoi(char c) { switch(c) { case '0': return(0); case '1': return(1); case '2': return(2); case '3': return(3); case '4': return(4); case '5': return(5); case '6': return(6); case '7': return(7); case '8': return(8); case '9': return(9); case 'a': case 'A': return(10); case 'b': case 'B': return(11); case 'c': case 'C': return(12); case 'd': case 'D': return(13); case 'e': case 'E': return(14); case 'f': case 'F': return(15); default: cout << "ctoi: Caracter no numerico\n"; break; } } // Obtiene un numero en binario int get_binary(void) { int bin = 0; char c; while(1) { cout << flush; c = getch(); if(c == '0' || c == '1') { cout << c; bin = bin*2 + ctoi(c); } else if(c == 13) { return(bin); } } } // Obtiene un numero en decimal int get_decimal(void) { int dec = 0; char c; while(1) { cout << flush; c = getch(); if(isdigit(c)) { cout << c; dec = dec*10 + ctoi(c); } else if(c == 13){ return(dec); } } } // Obtiene un numero en hex int get_hex() { int hex = 0; char c; while(1) { cout << flush; c = getch(); if(isxdigit(c)) { cout << c; hex = hex*16 + ctoi(c); } else if(c == 13) { return(hex); } } } // Imprime el numero en binario void display_binary(int x) { int i; int nonzero = 0; cout << "Binary: "; for(i = 31; i >= 0; i--) { nonzero += (x >> i) & 1; if(nonzero) print_binary_digit((x >> i) & 1); } if(x == 0) print_binary_digit(0); cout << "\n"; } // Imprime el nuemro en decimal void display_decimal(int x) { cout << "Decimal: " << x << "\n"; } // Imprime el nuemro en hex void display_hex(int x) { int i; int nonzero = 0; cout << "Hex: "; for(i = 7; i >= 0; i--) { nonzero += (x >> (i * 4)) & 0xF; if(nonzero) print_hex_digit((x >> (i * 4)) & 0xF); } if(x == 0) print_hex_digit(0); cout << "\n"; } // Imprime el digito en binario void print_binary_digit(int x) { switch(x) { case 0: cout << "0"; break; case 1: cout << "1"; break; default: cout << "Error, print_digit accepts 0-15 only\n"; exit(-1); break; } } // Imprime el numero en hex void print_hex_digit(int x) { switch(x) { case 0: cout << "0"; break; case 1: cout << "1"; break; case 2: cout << "2"; break; case 3: cout << "3"; break; case 4: cout << "4"; break; case 5: cout << "5"; break; case 6: cout << "6"; break; case 7: cout << "7"; break; case 8: cout << "8"; break; case 9: cout << "9"; break; case 10: cout << "A"; break; case 11: cout << "B"; break; case 12: cout << "C"; break; case 13: cout << "D"; break; case 14: cout << "E"; break; case 15: cout << "F"; break; default: cout << "Error, print_hex_digit accepts 0-15 only\n"; exit(-1); break; } } |
|
|
|
|
|
#2 |
|
Professional gadfly
|
To get the complement of a number, XOR it with all 1s.
For an 8-bit number, that means XORing it with 255 decimal, FF hex, or 11111111 binary. C ^ 11111111 = Complement of C To get the two's complement, just add 1 to the complement. (C ^ 11111111) + 1 = Two's complement. |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|