Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

n>>1; n/=2 what is this in c++?

3 Answers

Relevance
  • Cronin
    Lv 6
    8 years ago
    Favorite Answer

    Both of these lines shift the bits over to the right one place

    both are equivalent to n = n / 2;

  • David
    Lv 6
    8 years ago

    n >> 1 is a bitwise right shift by 1 in C++. a >> b shifts the bit pattern of a the number of bits positions specified by b. Vacated bits on the left are 0-filled if the number is unsigned. If signed, the vacated bits are filled with the sign bit.

    Furthermore, the right shift operator only works with a positive right operand. If negative, the result is undefined.

    Here's an example of n >> 1. Let's assume that n is 10 (10 in binary is 1010); and we are also going to be using 16 bits here:

        0000000000001010

        0000000000000101

    The result is now 0101 which is 5 in binary. a >> b is equivalent to a * 2^b. Note that this only returns the result, it doesn't change the value of n.

    n /= 2 is using the compound assignment operator. n is being assigned the result of the division between itself and 2.

  • 8 years ago

    This is jalva

Still have questions? Get your answers by asking now.