# Thread: ((var) &= ~(bit))

1. ## ((var) &= ~(bit))

Code:
`  ((var) &= ~(bit))`
What exactly is that statement saying?
Is that saying toggle 0 or 1?

Or is that saying "whatever it equals
now it equals 0"?

2. What is var and what is bit?

3. The ~ operator flips all the bits. So if bit was set to, let's say, 6 (0110), ~bit would be 9 (1001), if bit was a 4-bit integer. Basically, it's a good way to set one or more bits to 0. In the example where bit is 6, you're turning off 2 bits:

val = 01011011 (hypothetically)
bit = 00000110
~bit = 11111001

01011011
& 11111001
---------------
01011001

4. To me it's saying:
OMG I must be a macro because I have far more brackets than necessary.

As for a description; It does the opposite of:
Code:
`((var) |= (bit))`

5. ## ~(00001000)!?

thats why there so many brackets.
[/code]utils.h:186:#define REMOVE_BIT(var,bit) ((var) &= ~(bit))[/code]

ch->char_specials.saved.affected_by) ( 1 << 8)

but if
ch->char_specials.saved.affected_by doesnt equal (1<<8)

then functions that depend on 1<<8 wont be in affect.
lets say after

~(00001000)

what would that be?

6. Originally Posted by errigour
~(00001000)

that what would it be?
hmm...
Originally Posted by itsme86
The ~ operator flips all the bits.

7. i meant to ask what that number would be after the ~ mark.

8. Originally Posted by errigour
i meant to ask what that number would be after the ~ mark.
You know how to flip bits right? Turn a zero into a one or a one into a zero.

So if only a single bit was a one originally, then only a single bit will be a ____ afterwards. Therefore the value will be ________.

9. Originally Posted by iMalc
You know how to flip bits right?
While some prefer a fork, I've always found a spatchula to be most effective...

10. Originally Posted by errigour
i meant to ask what that number would be after the ~ mark.
It depends on the size of the value you're flipping. If it's 32 bits in size, then you'd get more 0's turned into 1's, giving you a larger "number".

If you were dealing with 4-bit numbers then ~0x2 would be 1101 (13, 0xD), but if you're dealing with 8-bit numbers then ~0x2 would be 11111101 (253, 0xFD).

Is that what you're asking?

11. Originally Posted by errigour
i meant to ask what that number would be after the ~ mark.
For even more fun, try this:

Code:
`printf("%d",(var+(~var + 1));`

12. ## would it flip?

whould it flip this intiger

00000100 00000100
to this
00100000 00100000

13. Originally Posted by errigour
whould it flip this intiger

00000100 00000100
to this
00100000 00100000
Why in god's name would it? It just turns all the 0 bits into 1s and all the 1 bits into 0s. It would turn 00000100 00000100 into 11111011 11111011.

Popular pages Recent additions