home *** CD-ROM | disk | FTP | other *** search
- /*********************************************************************
- * CH13_05.C Conversion d'un entier signΘ en binaire *
- * Exercice d'aprΦs un programme de William J-M MARIE *
- *********************************************************************/
-
- #include<stdio.h>
- #include<string.h>
-
- #define n (8* sizeof( int))
-
- void LitInt( int*);
- void convbin_n( int);
-
- main(void)
- {
- int entier;
-
- printf(" **** Conversion: int => binaire ****\t\t"
- "( 0 pour sortir)");
-
- while(1)
- {
- printf("\n\n Entrez un entier, de -32768 α 32767 : ");
- LitInt( &entier);
-
- if( entier== 0) break;
-
- convbin_n( entier);
- }
- }
-
- void convbin_n( int nombre)
- {
- int i;
- unsigned int valeur= 0x1 << ( n- 1); /* 1000 0000 0000 0000 */
-
- printf(" DΘcimal: %d => Binaire: ", nombre);
-
- for( i= 1; i<= n; i++)
- {
- printf("%d", ( nombre & valeur) ? 1 : 0);
-
- if( i % 4== 0) printf(" ");
-
- valeur>>= 1;
- }
- }
-
- void LitInt( int *entier)
- {
- double dble;
- do
- {
- while( scanf("%lf", &dble)!= 1)
- while( getchar() != '\n');
- while( getchar() != '\n');
- }
- while( dble< -32768.0 || dble> 32767.0);
- *entier= ( int) dble;
- }
-
-