Skip to content

What does '&=~' do in c++?

An answer to this question on Stack Overflow.

Question

I'm looking through some c++ code and saw the following and, for my understanding, I want to know what this does. I didn't see anything online other than "The tilde is the destructor"

#define i2c_scl_release()                 \
	*_sclDirReg &=~ _sclBitMask

I understand the definition, but I'm trying to figure out what &=~ does.

Ignore any best practices/formatting issues. I found this on github and it's relevant to what I'm looking at so I want to understand it. Both are typed uint8_t. Thanks!

Answer

It takes the inverse bitmask of _sclBitMask and combines it into whatever _sclDirReg is pointing at using the bitwise and operator.

You could rewrite this as:

*_sclDirReg = *_sclDirReg & (~sclBitmask);