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
88
89
90
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>

typedef unsigned char uchar;

uchar get_bit(uchar ch, int bit_idx) {
    return ch >> (7 - bit_idx) & 1;
}

uchar set_bit(uchar ch, int bit_idx, uchar new_value) {
    if(new_value) {                         //                                   bit_idx==2
        // Set bit (to 1)                   //                                      ↓
        return ch | (1 << (7 - bit_idx));   // bitwise-or with a byte like this:  00100000
    }
    else {
        // Unset bit (to 0)                   //                                    bit_idx==2
        assert(new_value == 0);               //                                       ↓
        return ch & (~ (1 << (7 - bit_idx))); // bitwise-and with a byte like this:  11011111
    }
}

/*
// NAÏVE way
uchar get_bits(uchar ch, int start_bit_idx, int end_bit_idx) {  // end_bit_idx is inclusive
    // Bits from the source byte ('ch') will be in the least signficant side of the result.
    //  - least-significant means the right side, i.e., the side that affects the magnitude least.
    uchar result = 0x00; // hex for 0 == 00000000₂
    int num_bits_to_get = end_bit_idx - start_bit_idx + 1;
    int result_start_bit_idx = 8 - num_bits_to_get;

    for(int idx = 0; idx < num_bits_to_get; idx++) {
        int src_idx    = start_bit_idx + idx;

        uchar bit = get_bit(ch, src_idx);
        assert(bit == 0 || bit == 1);

        int result_idx = result_start_bit_idx + idx;
        result = set_bit(result, result_idx, bit);
    }
    return result;
}

// TOO SMART FOR YOUR BRITCHES way -- Expect problems
uchar get_bits(uchar ch, int start_bit_idx, int end_bit_idx) {  // end_bit_idx is inclusive
    // Move the target bits to the left (most-significant) side, to knock off unwanted bits.
    uchar result = ch << start_bit_idx;

    // Move target bits all the way to the right (least significant) side to knock off any unwanted
    // bits from that side.  This also leaves the correct result.
    // result = result >> (end_bit_idx - start_bit_idx + 1);
    // result = (ch << start_bit_idx) >> (end_bit_idx - start_bit_idx + 1);        // BROKEN!!!
    result = (0xFF & (ch << start_bit_idx)) >> (end_bit_idx - start_bit_idx + 1);  // UGLY!!!!!

    return result;
}
*/

// SMART way
uchar get_bits(uchar ch, int start_bit_idx, int end_bit_idx) {  // end_bit_idx is inclusive

    int num_bits_to_get = end_bit_idx - start_bit_idx + 1;

    // Move the target bits to the left (most-significant) side, to knock off unwanted bits.
    uchar result = ch << start_bit_idx;

    // Move target bits all the way to the right (least significant) side to knock off any unwanted
    // bits from that side.  This also leaves the correct result.
    result >>= num_bits_to_get;

    return result;
}

// LESSON: Break up code into logical steps.


int main(int argc, char* argv[]) {
    assert( get_bit(0xcb, 1) == 1 );
    assert( set_bit(0x00, 0, 1) == 0x80 ); // 10000000₂
    assert( set_bit(0xcb, 2, 1) == 0xeb ); // 0xcb == 11001011₂, 0xeb == 11101011₂
    assert( set_bit(0xeb, 2, 0) == 0xcb ); // 0xcb == 11001011₂, 0xeb == 11101011₂

    // assert( get_bits(10111100₂, 3, 6) == ₀₀₀₀1110₂)
    //                     ‾‾‾‾
    assert( get_bits(0xbc, 3, 6) == 0x0e);

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

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