aboutsummaryrefslogtreecommitdiff
path: root/challenge-114/abigail/c/ch-2.c
blob: 265f0ed0a46b05afff5c776b1921357b9cd2e152 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# include <stdlib.h>
# include <stdio.h>
# include <string.h>
# include <limits.h>

/*
 * See ../README.md
 */

/*
 * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o < input-file
 */

int main (void) {
    unsigned long long d;
    while (scanf ("%llu", &d) == 1) {
        char b [LONG_BIT + 1];
        /*
         * Transfer the decimal number into a binary representation,
         * LSB first.
         */
        for (size_t i = 0; i < LONG_BIT; i ++) {
            b [i] = (d & 1);
            d = d >> 1;
        }
        /*
         * End with a 0
         */
        b [LONG_BIT] = 0;

        /*
         * Count the number of 0s at the beginning, followed by a count of
         * the number of 1s. 
         */
        size_t count_0 = 0;
        size_t count_1 = 1;
        while (!b [count_0]) {
            count_0 ++;
        }
        while (b [count_0 + count_1]) {
            count_1 ++;
        }
        /*
         * Now, b [count_0 + count_1 - 1] == 1 and
         *      b [count_0 + count_1]     == 0.
         * Swap those two, and place the 1s and 0s
         */
        b [count_0 + count_1]     = 1;
        b [count_0 + count_1 - 1] = 0;
        count_1 --;
        for (size_t i = 0; i < count_1; i ++) {
            b [i] = 1;
        }
        for (size_t i = count_1; i < count_1 + count_0; i ++) {
            b [i] = 0;
        }

        /*
         * Turn binary expansion back to decimal
         */
        d = 0;
        unsigned long long p2 = 1;
        for (size_t i = 0; i < LONG_BIT; i ++) {
            d  += p2 * b [i];
            p2 *= 2;
        }
        printf ("%llu\n", d);
    }
}