int2bit.c - int2bit - Convert integer to funny way.
HTML git clone git://bitreich.org/int2bit git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/int2bit
DIR Log
DIR Files
DIR Refs
DIR Tags
DIR README
DIR LICENSE
---
int2bit.c (1085B)
---
1 /*
2 * Copy me if you can.
3 * by 20h
4 */
5
6 #include <stdlib.h>
7 #include <stdio.h>
8
9 void
10 usage(char *argc)
11 {
12 fprintf(stderr, "usage: %s integer\n", argc);
13 exit(1);
14 }
15
16 int
17 main(int argc, char *argv[])
18 {
19 int in, i, hasprint, hexprint;
20
21 if (argc < 2)
22 usage(argv[0]);
23
24 in = atoi(argv[1]);
25 srand(in);
26
27 if (in == 0) {
28 printf("0x0\n");
29 return 0;
30 }
31
32 for (hasprint = 0, hexprint = 0, i = (sizeof(in) * 8)-1; i >= 0; i--) {
33 if (in & (1<<i)) {
34 /* Decide about next step. */
35 /* printf("\nint = %d\n", (1<<i)); */
36 switch (rand() % 3) {
37 case 0:
38 /* print shift */
39 if (hasprint)
40 printf("|");
41 printf("(1<<%d)", i);
42 hasprint = 1;
43 if (hexprint == 0)
44 break;
45 default:
46 /* print hexprint */
47 if (hasprint)
48 printf("|");
49 hexprint |= (1<<i);
50 /* hexadecimal */
51 printf("0x%x", hexprint);
52 hexprint = 0;
53 hasprint = 1;
54 break;
55 case 1:
56 /* add to hexprint */
57 hexprint |= (1<<i);
58 break;
59 }
60 }
61 }
62
63 if (hexprint) {
64 if (hasprint)
65 printf("|");
66 printf("0x%x", hexprint);
67 }
68 printf("\n");
69
70 return 0;
71 }
72