Thread: COnfused about the |= symbol?

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    206

    Smile COnfused about the |= symbol?

    Hi read this in a book. What does this symbol mean the vertical line and the equals sign?

    |=

  2. #2
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    | is bitwise OR
    |= means you're setting a bit..i think
    You ended that sentence with a preposition...Bastard!

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It's very much like +=

    Only it's bitwise-or rather than addition.

    Look up assignment operators.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by shrink_tubing View Post
    Hi read this in a book. What does this symbol mean the vertical line and the equals sign?

    |=
    It's a shorthand version of : x = x | y;

    It does a bitwise OR...

    The logic "truth table" is like this...

    0 OR 0 = 0
    1 OR 0 = 1
    0 OR 1 = 1
    1 OR 1 = 1

    and it's done on every bit of the variable...

    That is to say that the two numbers are compared at the binary level and all the 1 bits are merged, like this...
    Code:
    char a = 3;             // in binary  00000011
    char b = 24;            // in binary  00011000
    a |= b;                 // in binary  00011011
    
    Printf("%d",a);       // should display 27;
    
    char a = 27;           // in binary  00011011
    char b = 24;           // in binary  00011000
    a |= b;                // in binary  00011011
    
    Printf("%d",a);        // should still display 27;
    It gets into binary logic which isn't really used in programming for general purpose things, but when working with I/O ports (etc) at the bit level it can be helpful.
    Last edited by CommonTater; 01-20-2011 at 11:03 AM.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    206
    Awesome answers!!!!! Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  4. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM
  5. <list>
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 02-24-2002, 04:07 PM