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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <stdint.h>

//////////////////////////////////////////////
// Write bits given as a string to a buffer //
//////////////////////////////////////////////

void print_byte(uint8_t byte) {
    for(int pos = 0; pos < 8; pos++) {
        int bit_value = (byte >> (8 - 1 - pos)) & 1;    // 001
        char ch = '0' + bit_value;
        fputc(ch, stdout);
    }
}

void print_bytes(uint8_t* buf, size_t buf_len) {
    for(int byte_idx = 0; byte_idx < buf_len; byte_idx++) {
        print_byte(buf[byte_idx]);
        fputc(byte_idx < buf_len - 1 ? ' ' : '\n', stdout);
    }
}

void set_bit(uint8_t* a_buf_byte, int* a_pos_bit, int bit_value) {
    assert(bit_value == 0 || bit_value == 1);
    if(bit_value == 1) {
        *a_buf_byte |= 1 << (8 - 1 - *a_pos_bit);
        // If we are writing to position 0, then 1 << (8 - 1 - *a_pos_bit) should be 10000000₂
        // If we are writing to position 1, then 1 << (8 - 1 - *a_pos_bit) should be 01000000₂
        // ...
        // If we are writing to position 7, then 1 << (8 - 1 - *a_pos_bit) should be 00000001₂
        //
        // Then, logical-or'ing that with the existing value of *a_buf_byte will set the righ bit.
    }

    // Increment the cursor (position within the byte).
    *a_pos_bit += 1;
}

void set_bits_from_string(uint8_t* buf, int* a_pos_byte, int* a_pos_bit, const char* s) {
    for(const char* curr = s; *curr != '\0'; curr++) {
        assert(*curr == '0' || *curr == '1' || *curr == ' ');
        if(*curr != ' ') {
            int bit_value = *curr - '0';
            assert(bit_value == 0 || bit_value == 1);
            set_bit(&buf[*a_pos_byte], a_pos_bit, bit_value);
            // &buf[*a_buf_byte] is the address of the *byte* where we will *start* writing.
        }

        if(*a_pos_bit == 8) {
        // If we are rolling into a new byte in the buffer…
            *a_pos_byte += 1;  // Advance the byte position
            *a_pos_bit = 0;    // Reset the bit position to 0
        }
    }
}

int main(int argc, char* argv[]) {
    // Task is to write this string to a buffer (bytes).

    // INPUT
    printf("Input:\n");
    const char* s = "0100 100 00 00 110 1011 1110 011 00 0101 100 00 00 1111 011 110 1011 1110 1010 011 110";
    printf("%s\n\n", s);

    // CREATE the bit string
    uint8_t buf[9] = {0};  // ⚠ Initialize all bytes to zero.
    size_t buf_len = sizeof(buf) / sizeof(*buf);  // ⚠ Only works with a true "array" (declared with [])
    int pos_bytes = 0;
    int pos_bit   = 0;  // 0 is most-significant (128's place), 7 is least significant (1's place)
    set_bits_from_string(buf, &pos_bytes, &pos_bit, s);

    // Print ACTUAL
    printf("Actual:\n");
    print_bytes(buf, buf_len); // 01001000  ←  this is correct
    printf("\n");

    // Print EXPECTED (with same spacing)
    printf("Expect:\n");
    printf("01001000 00011010 11111001 10001011 00000011 11011110 10111110 10100111 10₀₀₀₀₀₀");
    printf("\n");

    return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */

© Copyright 2020 Alexander J. Quinn         This content is protected and may not be shared, uploaded, or distributed.