SoloCodigo
Programación General => C/C++ => Mensaje iniciado por: ijescobarj en Miércoles 19 de Febrero de 2003, 15:56
-
NESCESITO URGENTE CREAR UNA FUNCION QUE ME DEVUELVA LOS VALORES EN BINARIO DE LOS NUMEROS DEL 1 AL 15:( :(;) ;)
-
Como seria: vos le pasas un numero (char) y te lo convierte a binario ?
Se me ocurre hacerlo usando el shift y una AND: por ejemplo
#include "stdio.h"
main ()
{
char i, x;
printf("ingrese un numero: ");
scanf("%d",&x);
for (i=0;i<8;i++)
{
if ( ( x << i ) & 0x80 )
printf("1");
else printf("0");
}
}
-
#include <stdio.h>
int main(void)
{
struct
{
unsigned int bit_0: 1;
unsigned int bit_1: 1;
unsigned int bit_2: 1;
unsigned int bit_3: 1;
} decimal;
printf("Introduce un número en base decimal del 1 al 15: ");
scanf("%d", &decimal); //warning, pero funciona.
printf("%d", decimal.bit_3);
printf("%d", decimal.bit_2);
printf("%d", decimal.bit_1);
printf("%d", decimal.bit_0);
return 0;
}