summaryrefslogtreecommitdiff
path: root/reference/C/PROBLEMS/binary_op.c
blob: 2e5a2146052b1e5a576ef5e54724083be32c574f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/************************************************************************
 *
 * Purpose: Display a byte in binary format.
 * 
 * Author:  M.J. Leslie.
 *
 * Date:    19-Mar-95
 *
 * Improvements: 
 *	1) Return the result to the calling function.
 *	2) Make it work on integers (4 bytes long).
 *
 ************************************************************************/

void binary_op(char byte);		/* Print a byte in binary.	*/

/************************************************************************/

main()
   {
   char byte=55;	

   binary_op(byte);			/* Print a byte in binary.	*/
   }

/************************************************************************/

void binary_op(char byte)
   {
   int count=8;				/* Number of bits in a byte.	*/

   while(count--)
      {
					/* AND the high order bit (the
					 * left one) If the bit is set,
					 * print a ONE.			*/
      printf("%d", ( byte & 128 ) ? 1 : 0 );

					/* Move all the bits LEFT.	*/
      byte <<= 1;
      } 

   printf("\n");
   }

/************************************************************************
 *
 *	Program results.
 *	----------------
 *
 *	00110111
 *
 ************************************************************************/