Bit Manipulation Functions in C

This snippet demonstrates various bit manipulation functions in C, such as clearing memory and performing binary addition with carry handling. The functions include operations like bitwise AND, OR, and NOT to modify integer values at a bit level, which is essential in low-level programming.
 avatar
unknown
c_cpp
2 months ago
1.1 kB
2
Indexable
void clearMemory(int i) {
    int bitClear = i;
    bnot(bitClear - 1, bitClear);
    band(bitClear - 1 , bitClear, bitClear); 
    for(int i = bitClear + 1; i <= 1000; i++) {
        band(i - 1, i, i);
    }
}

void nuaCong(int x, int y, int loc, int locNho) {
    bor(x, y, x + 100);
    band(x, y, locNho); // bit nho
    bnot(locNho, x + 102);
    band(x + 100, x + 102, loc); // bit ket qua 
}

void congDayDu(int x, int y, int loc, int locNho) {
    nuaCong(x, y, locNho + 1, locNho + 2);
    
    nuaCong(locNho + 1, locNho, loc, locNho);
    //carry cuoi cung
    bor(locNho + 2, locNho, locNho);
}

void solve2() {
    nuaCong(5, 10, 16, 17);
    congDayDu(4, 9, 15, 17);
    congDayDu(3, 8, 14, 17);
    congDayDu(2, 7, 13, 17);
    congDayDu(1, 6, 12, 17);

    // Copy gia tri tu bit 1000 
    bnot(1000, 1000); // set bit 1000 thanh 1
    band(17, 1000, 1); // copy bit nho den bit 1
    band(12, 1000, 2);
    band(13, 1000, 3);
    band(14, 1000, 4);
    band(15, 1000, 5);
    band(16, 1000, 6);
    clearMemory(7);
}
Editor is loading...
Leave a Comment