Thread: << operator and the minus before brackets

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    29

    Question << operator and the minus before brackets

    #define MYCONSTANT -( ((MYID + 1) << 5 ) + 1 )

    What is the purpose of the minus behind the brackets?

    What is actually going on the red?

  2. #2
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    the minus sign returns the negative of everything in brackets

    the << multiplies the number on the left by two to the power of the number on the right.

    Ex 1.

    5 << 3 = 5 * (2 to the third)

    Ex 2.

    3 << 5 = 3 * (2 to the power of five)

    a >> would divide
    Don't quote me on that... ...seriously

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    29

    Question

    Thanks. Useful to know. What is computationally faster to multiply '*' or '<<' ?

  4. #4
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    << is much faster when multiplying by a power of 2
    Don't quote me on that... ...seriously

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    135

    ..

    Quote Originally Posted by Brad0407
    << is much faster when multiplying by a power of 2
    I think that's compiler dependant nowadays since most compilers optimize your code to perform better. MS's compilers do for one, but check your compiler to be sure. One example that comes to mind is; I was doing a GDI render in a loop once in one of my projects and I was using multiplication by two's and everything went as expected - smooth,.. but when I changed it to the leftshift operator you could literally - visually - tell the difference in the rendering being done. The leftshift OP was slower by a couple seconds.

    I think you really shouldn't have to worry about optimizing your code with Bitwise Operators because eventually, or most likely, your compiler knows what's best and will do the optimization for you, and probably better.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Not really. A decent compiler knows how to optimize it for you, so there really isn't any speed gain any more with it. Its real purpose is for setting and unsetting of specific bits.

    There's a FAQ entry on bitwise operators if you're so inclined.

    [edit] Curses, foiled again. [/edit]


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    quzah's right.

    The << is most commonly used for bit shifting.

    Ex 1.

    00001101 << 2 = 00110100

    Ex 2.
    01110100 >> 1 = 00111010

    There's a pair of examples for you.

    You can then set specific bits with the & and | operators.

    Ex. 1 force the third bit to be 1
    (00001011 & (1 << 3)) = 00001111

    Ex. 2 force the fourth bit to be zero
    (00011011 | (255 - (1 << 4)) = 00010011

    Hope that clearifies a few things.
    Last edited by Brad0407; 10-25-2005 at 06:04 PM.
    Don't quote me on that... ...seriously

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    29

    Question

    Thanks for all your informative responses. I will certainly consult my compiler.

    example:

    var1.var2.var3 |= (1<<3)

    I understand the above sets the 3rd bit (litte endian) to one. How is this working exactly?

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed