Curso de Programación en C/Prog127

De WikiCabal
Ir a la navegación Ir a la búsqueda

Prog127

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 int main( void )
 6 {
 7   unsigned char Mascara,
 8                 Semaforos;
 9 
10   char Buf[7];
11 
12   int  i,
13        Prueba;
14 
15   puts( "\nEl bitwise AND esta usado a veces a ver" );
16   puts( "si algunas bits en un entero están prendidos o pagados\n" );
17   printf( "Dame un número positivo entre 0 y 255: " );
18   while( fgets( Buf, 5, stdin ) && Buf[0] != '\n' )
19   {
20     if( *(Buf + strlen( Buf ) -1 ) != '\n' )
21     {
22       fputs( "La línea que entraste es demaciado largo.\nSolo 3 chars MAX",
23                                                                      stderr );
24       exit(1);
25     }
26     *(Buf + strlen( Buf ) -1 ) = '\0';
27 
28     for( i = 0; i < strlen( Buf ); i++ )
29     {
30       if( Buf[i] < '0' || Buf[i] > '9' )
31       {
32         fputs( "Caracteres ilegales en su ingreso\n", stderr );
33         exit(1);
34       }
35     }
36 
37     Prueba = atoi( Buf);
38     if( Prueba > 255 )
39     {
40       fputs( "Su entrada está fuera del rango especificado\n", stderr );
41       exit(1);
42     }
43 
44     Semaforos = Prueba;
45 
46     for( i = 1; i <= 8; i++ )
47     {
48       if( i == 1 )
49         Mascara = 0x01;
50       else
51         Mascara <<= 1;
52       Semaforos & Mascara ?
53         printf( "Bit No. %d en %d está prendida    1\n", i -1, Semaforos ) :
54         printf( "Bit No. %d en %d no está prendida 0\n", i -1, Semaforos );
55     }
56     printf( "\nDame un número positivo entre 0 y 255"
57             " (<Enter> para terminar): " );
58   }
59   return 0;
60 }

Resultado


El bitwise AND esta usado a veces a ver
si algunas bits en un entero están prendidos o pagados

Dame un número positivo entre 0 y 255: 1
Bit No. 0 en 1 está prendida    1
Bit No. 1 en 1 no está prendida 0
Bit No. 2 en 1 no está prendida 0
Bit No. 3 en 1 no está prendida 0
Bit No. 4 en 1 no está prendida 0
Bit No. 5 en 1 no está prendida 0
Bit No. 6 en 1 no está prendida 0
Bit No. 7 en 1 no está prendida 0

Dame un número positivo entre 0 y 255 (<Enter> para terminar): 2
Bit No. 0 en 2 no está prendida 0
Bit No. 1 en 2 está prendida    1
Bit No. 2 en 2 no está prendida 0
Bit No. 3 en 2 no está prendida 0
Bit No. 4 en 2 no está prendida 0
Bit No. 5 en 2 no está prendida 0
Bit No. 6 en 2 no está prendida 0
Bit No. 7 en 2 no está prendida 0

Dame un número positivo entre 0 y 255 (<Enter> para terminar): 3
Bit No. 0 en 3 está prendida    1
Bit No. 1 en 3 está prendida    1
Bit No. 2 en 3 no está prendida 0
Bit No. 3 en 3 no está prendida 0
Bit No. 4 en 3 no está prendida 0
Bit No. 5 en 3 no está prendida 0
Bit No. 6 en 3 no está prendida 0
Bit No. 7 en 3 no está prendida 0

Dame un número positivo entre 0 y 255 (<Enter> para terminar): 4
Bit No. 0 en 4 no está prendida 0
Bit No. 1 en 4 no está prendida 0
Bit No. 2 en 4 está prendida    1
Bit No. 3 en 4 no está prendida 0
Bit No. 4 en 4 no está prendida 0
Bit No. 5 en 4 no está prendida 0
Bit No. 6 en 4 no está prendida 0
Bit No. 7 en 4 no está prendida 0

Dame un número positivo entre 0 y 255 (<Enter> para terminar): 7
Bit No. 0 en 7 está prendida    1
Bit No. 1 en 7 está prendida    1
Bit No. 2 en 7 está prendida    1
Bit No. 3 en 7 no está prendida 0
Bit No. 4 en 7 no está prendida 0
Bit No. 5 en 7 no está prendida 0
Bit No. 6 en 7 no está prendida 0
Bit No. 7 en 7 no está prendida 0

Dame un número positivo entre 0 y 255 (<Enter> para terminar): 57
Bit No. 0 en 57 está prendida    1
Bit No. 1 en 57 no está prendida 0
Bit No. 2 en 57 no está prendida 0
Bit No. 3 en 57 está prendida    1
Bit No. 4 en 57 está prendida    1
Bit No. 5 en 57 está prendida    1
Bit No. 6 en 57 no está prendida 0
Bit No. 7 en 57 no está prendida 0

Dame un número positivo entre 0 y 255 (<Enter> para terminar): 255
Bit No. 0 en 255 está prendida    1
Bit No. 1 en 255 está prendida    1
Bit No. 2 en 255 está prendida    1
Bit No. 3 en 255 está prendida    1
Bit No. 4 en 255 está prendida    1
Bit No. 5 en 255 está prendida    1
Bit No. 6 en 255 está prendida    1
Bit No. 7 en 255 está prendida    1

Dame un número positivo entre 0 y 255 (<Enter> para terminar): 256
Su entrada está fuera del rango especificado

Explicación