Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Alexandru Scvortov

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Print a binary number in C

These are two functions that print the binary representation of an integer. The first simply prints it out, while the second only prints out the relevant digits (i.e. cuts the first x 0 digits) in groups of four.

Explanation here.

   1  
   2  #include <stdio.h>
   3  
   4  /* Print n as a binary number */
   5  void printbitssimple(int n) {
   6  	unsigned int i;
   7  	i = 1<<(sizeof(n) * 8 - 1);
   8  
   9  	while (i > 0) {
  10  		if (n & i)
  11  			printf("1");
  12  		else
  13  			printf("0");
  14  		i >>= 1;
  15  	}
  16  }
  17  
  18  /* Print n as a binary number */
  19  void printbits(int n) {
  20  	unsigned int i, step;
  21  
  22  	if (0 == n) { /* For simplicity's sake, I treat 0 as a special case*/
  23  		printf("0000");
  24  		return;
  25  	}
  26  
  27  	i = 1<<(sizeof(n) * 8 - 1);
  28  
  29  	step = -1; /* Only print the relevant digits */
  30  	step >>= 4; /* In groups of 4 */
  31  	while (step >= n) {
  32  		i >>= 4;
  33  		step >>= 4;
  34  	}
  35  
  36  	/* At this point, i is the smallest power of two larger or equal to n */
  37  	while (i > 0) {
  38  		if (n & i)
  39  			printf("1");
  40  		else
  41  			printf("0");
  42  		i >>= 1;
  43  	}
  44  }
  45  
  46  int main(int argc, char *argv[]) {
  47  	int i;
  48  	for (i = 0; i < 16; ++i) {
  49  		printf("%d = ", i);
  50  		printbitssimple(i);
  51  		printf("\n");
  52  	}
  53  
  54  	return 0;
  55  }
  56  

Generate all subsets of a set

This code generates all the subsets of {1, 2, ..., n} and prints them.

This also contains a very fast binary counter implementation.

See this for further explanations.

   1  
   2  #include <stdio.h>
   3  
   4  /* Applies the mask to a set like {1, 2, ..., n} and prints it */
   5  void printv(int mask[], int n) {
   6  	int i;
   7  	printf("{ ");
   8  	for (i = 0; i < n; ++i)
   9  		if (mask[i])
  10  			printf("%d ", i + 1); /*i+1 is part of the subset*/
  11  	printf("\b }\n");
  12  }
  13  
  14  /* Generates the next mask*/
  15  int next(int mask[], int n) {
  16  	int i;
  17  	for (i = 0; (i < n) && mask[i]; ++i)
  18  		mask[i] = 0;
  19  
  20  	if (i < n) {
  21  		mask[i] = 1;
  22  		return 1;
  23  	}
  24  	return 0;
  25  }
  26  
  27  int main(int argc, char *argv[]) {
  28  	int n = 3;
  29  
  30  	int mask[16]; /* Guess what this is */
  31  	int i;
  32  	for (i = 0; i < n; ++i)
  33  		mask[i] = 0;
  34  
  35  	/* Print the first set */
  36  	printv(mask, n);
  37  
  38  	/* Print all the others */
  39  	while (next(mask, n))
  40  		printv(mask, n);
  41  
  42  	return 0;
  43  }
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS