I’m new to programming in C and I have the following situation where I know what needs to be done, I’m just not sure of the right way to do. I’ll try to keep it as simple as possible, and please let me know if I’m not providing enough info.
There’s a .h file with this line:
#define NS_CONN_ENTITYTOOLARGE 0x2000
Then in the C code we have the following flag being set:
connPtr->flags |= NS_CONN_ENTITYTOOLARGE;
Then later on this test:
if (connPtr->flags & NS_CONN_ENTITYTOOLARGE) {
}
Now what I want to do is basically “undo” the flag NS_CONN_ENTITYTOOLARGE. After some Googling I discovered that |= is a “bitwise OR”, but I don’t feel a lot more enlightened after learning that. Do I need to do a bitwise XOR, a NOT or what?
Here are all the possible values for flags that I can find in the header file.
#define NS_CONN_CLOSED 0x1
#define NS_CONN_SKIPHDRS 0x2
#define NS_CONN_SKIPBODY 0x4
#define NS_CONN_READHDRS 0x8
#define NS_CONN_SENTHDRS 0x10
#define NS_CONN_KEEPALIVE 0x20
#define NS_CONN_WRITE_ENCODED 0x40
#define NS_CONN_FILECONTENT 0x80
#define NS_CONN_RUNNING 0x100
#define NS_CONN_OVERFLOW 0x200
#define NS_CONN_TIMEOUT 0x400
#define NS_CONN_GZIP 0x800
#define NS_CONN_CHUNK 0x1000
#define NS_CONN_ENTITYTOOLARGE 0x2000
This doesn’t exactly “undo”
|=because it may have been a no-op for some or all of the bits inNS_CONN_ENTITYTOOLARGE, if they were already in the variable. In these case, those bits will be cleared too regardless of whether they were there in the first place.