((var) &= ~(bit))

This is a discussion on ((var) &= ~(bit)) within the C Programming forums, part of the General Programming Boards category; Code: ((var) &= ~(bit)) What exactly is that statement saying? Is that saying toggle 0 or 1? Or is that ...

  1. #1
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102

    ((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"?
    Last edited by errigour; 11-09-2010 at 10:31 PM.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    What is var and what is bit?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    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
    If you understand what you're doing, you're not learning anything.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    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))
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102

    ~(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?
    Last edited by errigour; 11-10-2010 at 12:15 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,306
    Quote Originally Posted by errigour
    ~(00001000)

    that what would it be?
    hmm...
    Quote Originally Posted by itsme86
    The ~ operator flips all the bits.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102
    i meant to ask what that number would be after the ~ mark.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by errigour View Post
    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 ________.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by iMalc View Post
    You know how to flip bits right?
    While some prefer a fork, I've always found a spatchula to be most effective...
    Last edited by CommonTater; 11-10-2010 at 01:35 PM.

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Quote Originally Posted by errigour View Post
    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?
    If you understand what you're doing, you're not learning anything.

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by errigour View Post
    i meant to ask what that number would be after the ~ mark.
    For even more fun, try this:

    Code:
    printf("%d",(var+(~var + 1));
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  12. #12
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102

    would it flip?

    whould it flip this intiger

    00000100 00000100
    to this
    00100000 00100000

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Quote Originally Posted by errigour View Post
    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.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 11:00 AM
  2. Need help assignment
    By 6kaine9 in forum C Programming
    Replies: 26
    Last Post: 10-19-2008, 09:51 PM
  3. server question
    By xddxogm3 in forum Tech Board
    Replies: 24
    Last Post: 01-21-2004, 01:12 AM
  4. Void functions
    By cap in forum C Programming
    Replies: 16
    Last Post: 09-04-2002, 09:08 AM
  5. Vigenere Decipher/Encipher
    By Xander in forum C++ Programming
    Replies: 5
    Last Post: 02-15-2002, 09:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21