Bitwise operation

I am working on seven segment display that i pulled off from washing machine.there are many segments .so i diside to make 6*2 2d array for storing 2 byte values (becouse i am having a 2 74hc595) and 6 digits. I want to periodically asigne values like 0b01000001( for number 1) in to 2d array without affecting existing bits if it has 101 and i want to add 010 then i want to result to be 111.
And also i want to remove 010 from resulted 111 value.

byte result = 0b101 | 0b010;          // will produce 0b111

byte result2 = 0b111 & ( ~ 0b010);    // remove 010 from 111
byte result |=  0b001; // to add
byte result &= ~0b010; // to remove
byte result ^=  0b010; // to toggle

identical to

byte result = result |  0b001; // to add
byte result = result & ~0b010; // to remove
byte result = result ^  0b010; // to toggle

consider following which i use with a Multifunction Shield

// shift 16-bits from data into the shift register
void output (
    uint16_t  data)
{
    digitalWrite (Latch, LOW);

    for (unsigned i = 0; i < 16; i++, data <<= 1)  {
        digitalWrite (Data, 0 != (data & 0x8000));

        digitalWrite (Clock, HIGH);
        digitalWrite (Clock, LOW);
    }

    digitalWrite (Latch, HIGH);
}

Only if you remove the byte type specifiers. The first isn't even valid code with byte since that makes the line a declaration with an initializer, and you can't use operators like |= in an initializer like that. In the second code, it's valid only if result already existed as another variable (in an outer scope) before this line.

Actually, it should have been written

byte result = 0bxxxxxxxx;
.
.
.
result |=  0b001; // to add
result &= ~0b010; // to remove
result ^=  0b010; // to toggle
1 Like

"add" is a bad word to use here, because addition exists. "set" and "clear" are the usual words for bit operations.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.