home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!ukma!darwin.sura.net!sgiblab!munnari.oz.au!jabaru.cec.edu.au!csource!gateway
- From: Scott.Pitcher@f395.n634.z3.fidonet.org (Scott Pitcher)
- Newsgroups: comp.lang.c
- Subject: Re: How to print an integer as binary?
- Message-ID: <721610886.AA08867@csource.oz.au>
- Date: Wed, 11 Nov 1992 06:40:00 +1100
- Sender: gateway@csource.oz.au
- Lines: 31
-
- TO> Assuming that:-
- TO>
- TO> int i = 203;
- TO>
- TO> How do I display 'i' as 11001011?
-
- #include <stdio.h>
- #include <values.h>
-
- void main()
- {
- int i, j;
-
- printf("\nEnter 'i' : ");
- scanf("%d", &i);
- for(j = 0; j < 16; i <<= 1, ++j)
- printf("%c", (i & MAXINT + 1) ? '1' : '0');
- printf("\n%u", MAXINT + 1);
-
- }
-
- This will print with leading zeros and it assumes that the integer is
- 16 bits wide, but it is simple and it works. What more could you want?
-
- Scott.
-
- P.S. If you were using FORTH instead of C, a simple "2 BASE" would
- allow you to print in Binary with out any hassles, and no leading
- zeros.
-
- * Origin: Starbase Alpha On the Edge of the Universe! (3:634/395.0)
-