Thread: Meaning of the instruction >>=

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    7

    Meaning of the instruction >>=

    Hello,
    I do not know what this instruction does:
    temp >>= 15;

    The context where I found is:


    Code:
    int my_random(int max)  {   //  Return Random Int Between 0 and max
    	unsigned long temp;
      	temp = (unsigned long) rand();
    	temp *= (unsigned long) max;
    	temp >>= 15;
    	return (int) temp; 
    }

    Help!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It is, technically, not an instruction, but an operator. What does a += 4 do? Do you know what >> does? If you know both of those, it should be obvious...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    ">>=" is a bitwise assignment operator.
    Code:
    temp >>= 15;
    means right shift temp by 15 and assign it back to temp.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    83

    Right shift by 15 times contents of tmp

    Quote Originally Posted by stevesmithx View Post
    ">>=" is a bitwise assignment operator.
    Code:
    temp >>= 15;
    means right shift temp by 15 and assign it back to temp.
    Go throgh following code
    Code:
    int main(int argc, char* argv[])
    {
      int tmp = 0xffff;
      printf("%d", tmp = tmp >> 15);  //tmp >>= 15 same as tmp = tmp >> 15
       return 0;
    
    }
    Ouput is 1

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by frodo_jedi View Post
    Hello,
    I do not know what this instruction does:
    temp >>= 15;

    The context where I found is:


    Code:
    int my_random(int max)  {   //  Return Random Int Between 0 and max
    	unsigned long temp;
      	temp = (unsigned long) rand();
    	temp *= (unsigned long) max;
    	temp >>= 15;
    	return (int) temp; 
    }

    Help!
    x=a>>n
    x will be equal to a divided by 2 raised to the power n.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simulator
    By MasterAchilles in forum C Programming
    Replies: 10
    Last Post: 11-30-2008, 10:31 PM
  2. Atomic Operations
    By Elysia in forum Windows Programming
    Replies: 27
    Last Post: 03-27-2008, 02:38 AM
  3. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  4. The Meaning of Life: A Trick Question?
    By chix/w/guns in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 07-12-2004, 07:53 PM