Skip to content

Instantly share code, notes, and snippets.

@Mahedi-61
Last active July 11, 2018 18:14
Show Gist options
  • Select an option

  • Save Mahedi-61/85a1862e8a4b963b7d1db7160132b036 to your computer and use it in GitHub Desktop.

Select an option

Save Mahedi-61/85a1862e8a4b963b7d1db7160132b036 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// function for concatenation
char* concat(const char *s1, const char *s2)
{
char *result = malloc(strlen(s1) + strlen(s2) + 1); // +1 for the null-terminator
strcpy(result, s1);
strcat(result, s2);
return result;
}
// main function
int main()
{
int inBits, i, c, j, k;
FILE *fp = fopen("binary.txt", "a");
char* temp[32];
// logic here
for(c = 0; c < 32; c++){
//printf("%d:" ,c+1);
char *bin = malloc(5 + 1);
for(i = 4; i >= 0; i--){
if((c & (1 << i)) != 0){
printf("1");
strcat(bin, "1");
}else{
printf("0");
strcat(bin, "0");
}
}
printf("\n");
temp[c] = bin;
}
printf("\n\nconcatenating ...\n");
for(k = 0; k < 32; k++){
// first part of the code
char* f_p = temp[k];
for (j = 0; j < 32; j++){
// second part of the code
char* s_p = temp[j];
// concatenating two part
char* line = concat(f_p, s_p);
printf("%s\n", line);
fprintf(fp,"%s\n", line);
}
}
// closing file
fclose(fp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment