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

  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,218
    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,318
    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
    28,413
    Quote Originally Posted by errigour
    ~(00001000)

    that what would it be?
    hmm...
    Quote Originally Posted by itsme86
    The ~ operator flips all the bits.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,318
    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,218
    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,218
    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, 10:00 AM
  2. Need help assignment
    By 6kaine9 in forum C Programming
    Replies: 26
    Last Post: 10-19-2008, 08: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, 08:08 AM
  5. Vigenere Decipher/Encipher
    By Xander in forum C++ Programming
    Replies: 5
    Last Post: 02-15-2002, 09:24 AM