Search code examples
c++bitwise-operatorsbitwise-and

The opposite of bitwise AND


I don't like so many nested ifs, which is why I want the opposite of that condition if (section_header[i].Characteristics & IMAGE_SCN_MEM_WRITE). What would it be? I tried with if (section_header[i].Characteristics & ~IMAGE_SCN_MEM_WRITE) continue, but it's not the correct one.

for (WORD i = 0; i < nt_headers->FileHeader.NumberOfSections; ++i)
{
    if (functions[i] >= section_header[i].VirtualAddress && functions[i] < section_header[i].VirtualAddress + section_header[i].Misc.VirtualSize)
    {
        if (section_header[i].Characteristics & IMAGE_SCN_MEM_WRITE)
        {
            ... next
        }
    }
}

Solution

  • If you are certain that your mask will only have one bit set, then you can test whether that bit is cleared (i.e. not set) in value by doing

    if (~value & MASK)
        // bit is clear
    

    However, I do not recommend this. While logically correct, it is a lot less idiomatic than the other suggested options if (!(value & MASK)) or if ((value & MASK) == 0). People reading your code are much more likely to misunderstand, possibly leading them to create new bugs by "fixing" it.

    Also, if MASK should have more than one bit set, the expression ~value & MASK expression will be true if if any of those bits is cleared in value, and so it is no longer the logical negation of value & MASK. For instance if MASK == 0x3 and value == 0x2, then value & MASK is true and ~value & MASK is also true. By contrast !(value & MASK) is true iff all of those bits are cleared.