Thread: what is |= operator ?

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    92

    what is |= operator ?

    what is |= operator ? how it works ? can anybody give simple example ?

    is it a bitwise OR operator ?

    for example var1 = var1 | something

    when i should use it ?
    blue_gene

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    yes, it should be equivalent to:
    var1 = var1 | var2;

    so you would use it just as you would use say
    var1 += var2;
    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

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    It's useful when you're dealing with bit-flags. For example, when using Windows API:

    style = WS_VISIBLE | WS_OVERLAPPED | WS_MINIMIZEBOX

    But if you want to wrap your window-creation code in a function, for example (I probably wouldn't do this, but whatever):
    Code:
    void createMyWindow(int something, int somethingElse, bool visible, bool minimizeBox)
    {
    	DWORD flags = WS_OVERLAPPED;
    	if(visible)
    		flags |= WS_VISIBLE;
    	if(minimizeBox)
    		flags |= WS_MINIMIZEBOX;
     
    	(...)
    	CreateWindow(....., flags, .....);
    }
    Or, you could pass a parameter for 'additional flags', and do flags |= (the parameter). Stuff like that. As a note, the & operator is useful too, if you want to find out if a certain flag is set: if(flags & WS_VISIBLE) //then WS_VISIBLE bit is set

    Hope this helps
    Last edited by Hunter2; 04-27-2004 at 10:37 AM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    hunter, i dont have any idea about window API. so your code is out of my bound . i was expecting a simple example.

    in fact i was expecting a simple bit level example. i think it is only for setting the bits. is not it?



    thanks for the response
    blue_gene

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Those are bits. Your original thoughts were correct. Here is another example:
    Code:
    const int BIT_ONE = 0x0001;   // 00000001
    const int BIT_TWO = 0x0002;   // 00000010
    const int BIT_THREE = 0x0004; // 00000100
    const int BIT_FOUR = 0x0008;  // 00001000
    const int BIT_FIVE = 0x0010;  // 00010000
    const int BIT_SIX = 0x0020;   // 00100000
    const int BIT_SEVEN = 0x0040; // 01000000
    const int BIT_EIGHT = 0x0080; // 10000000
    
    int main()
    {
        int value = BIT_ONE; // value is 00000001 in binary.
        value |= BIT_FOUR;   // value is 00001001 in binary.
        value |= BIT_SEVEN;  // value is 01001001 in binary.
        value |= BIT_FOUR;   // value is 01001001 (still) in binary (bit four was already set).
    }

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Hardware related programming...

    There is a really good introduction to bitwise operations in the Programming FAQ.


    Most C++ programmers don't use binary or bitwise operations very often. This stuff is mostly used by hardware guys like me who probe-around with an oscilloscope all day, and by programmers who write firmware for embedded systems or programmers who write drivers.

    For example, data bit zero might be connected to an error-LED (thru an 'address decoder' chip.), bit 1 might be connected to a transmit-LED. To turn-on a single LED without changing the state of all other LEDs, you have to use bitwise operations. Other bits can be used as single-switch inputs, or there might be a read-write register where each bit represents the status/state of something... Maybe bit 2 of the register represents "copy protection enabled". This is similar to Hunter2's example, except in the hardware world, the flags exists in a special physical register at a fixed-physical address.

    As you probably know, all data is stored in binary insde the computer. C++ tries to insulate you from the binary, by automatically/transparently converting all input/output to/from decimal for you. In fact, you have to take some extra programming steps if you want to input or display the actual binary.

    If you want to learn this stuff, it really helps if you are comfortable with binary and hexadecimal. Hex input/output is easy in C++ (easier than binary), and it is easy (for us humans) to convert between hex and binary... With a little practice you can do it in your head!!! (Converting between decimal and binary is not so easy... for humans).
    Last edited by DougDbug; 04-27-2004 at 02:11 PM.

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    i think it is only for setting the bits. is not it?
    YES. As in jlou's example, operator |= will set particualar bits.

    The most common bitwise "things to do", are:
    Setting bits
    Clearing bits
    Testing bits... "If bit n is low...", or "if bit-n is high..."

    Bit shifting and toggling are used less often.

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>Bit shifting and toggling are used less often.
    Well, that's not 100% true. I do a lot of right-shift or left-shift when I'm trying to optimize code by getting rid of pesky division/multiplication operations... Although they probably get optimized out eventually anyways by the compiler.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    The & is quite useful as well, made a program to compare a given serial to a correct serial with this.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  10. #10
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    >>>I do a lot of right-shift or left-shift when I'm trying to optimize code by getting rid of pesky division/multiplication operations

    ...but that is limited. is not it ? bcoz bit shifting means multiply by 2 ^n or divide by 2^n so, you can not get all numbers. for example you can not gt odd numbers by doing bit shifting. so you are limited basically.
    blue_gene

  11. #11
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    Yes you are quite limited, but for simple mathematics it is positively faster by an extreme amount.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  12. #12
    Registered User
    Join Date
    May 2004
    Posts
    3

    Thumbs up

    [COLOR=Yellow]very simple yaar !![/COLOR]"hello | = C" is equivalent to "hello = hello | C".


    cheers,
    karthik bala guru.

  13. #13
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Please don't use yellow for your post. It makes it hard to read.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. |= operator in C++
    By sunny_master_07 in forum C++ Programming
    Replies: 2
    Last Post: 02-04-2009, 08:28 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM