tags:

views:

204

answers:

8

What does >> do in this situation?

int n = 500;
unsigned int max = n>>4;
cout << max;

It prints out 31.

What did it do to 500 to get it to 31?

+1  A: 

The >> and << operators are shifting operators.

http://www-numi.fnal.gov/offline_software/srt_public_context/WebDocs/Companion/cxx_crib/shift.html

Of course they may be overloaded just to confuse you a little more!

Alex
+4  A: 

500 got bit shifted to the right 4 times.

x >> y mathematically means x / 2^y.

Hence 500 / 2^4 which is equal to 500 / 16. In integer division the result is 31.

Yuval A
+2  A: 

It divided 500 by 16 using integer division.

>> is a right-shift operator, which shifted the bits of the binary representation of n to the right 4 times. This is equivalent to dividing n by 2 4 times, i. e. dividing it by 2^4=16. This is integer division, so the decimal part got truncated.

Dima
One should mention that 16 = 2^4.
vog
+2  A: 

It shifts the bits of 500 to the right by 4 bit positions, tossing out the rightmost bits as it does so.

500 = 111110100 (binary)

111110100 >> 4 = 11111 = 31

Robert Harvey
+2  A: 
abelenky
+8  A: 

Bit shifted!

Original binary of 500:
111110100

Shifted 4
000011111 which is 31!

Original: 111110100
1st Shift:011111010
2nd Shift:001111101
3rd Shift:000111110
4th Shift:000011111 which equals 31.

This is equivilent of doing integer division by 16.

500/16 = 31
500/2^4 = 31

Some facts pulled from here: http://www.cs.umd.edu/class/spring2003/cmsc311/Notes/BitOp/bitshift.html (because blarging from my head results in rambling that is unproductive..these folks state it much cleaner than i could)

Shifting left using << causes 0's to be shifted from the least significant end (the right side), and causes bits to fall off from the most significant end (the left side).

Shifting right using >> causes 0's to be shifted from the most significant end (the left side), and causes bits to fall off from the least significant end (the right side) if the number is unsigned.

Bitshifting doesn't change the value of the variable being shifted. Instead, a temporary value is created with the bitshifted result.

Caladain
Excellent explanation.
Slightly ambiguous on the >> part. The "if the number is unsigned" part applies to the whole sentence, not just the "bits fall off from the right". Furthermore, `500` is signed in C ! `500U` is unsigned.
MSalters
Good thing this is C++ then. But regardless, the original int is a signed int. If the number is signed, then it's implementation dependent. It may shift the bits over while preserving the sign bit, or it may shift over regardless and your sign bit now gets flipped (potentially), or summon GRAKNOR THE DESTROYER. Honestly, it's up to the compiler. But 9/10, it shifts it from the most significant end and causes bits to fall off. In the OP's case, it's, logically, the implementation his compiler chose.
Caladain
BUT, for arguments sake, lets look at the OP again. The results of the bitshift on a signed int (compiler's choice on how to handle it) is saved into an unsigned int. Now, if the sign bit is stored in the most-left bit, it will always be zero (and thus positive i believe? Not sure if the compiler gets implementor's choice). IF on the other hand, the sign bit is on the far left, and if the compiler's implementation is destructive with regards to the preservation of this sign bit, you could end up with results that you wouldn't expect. (going from signed to unsigned and a wobbly sign bit)
Caladain
+2  A: 

111110100 is 500 in binary. Move the bits to the right and you are left with 11111 which is 31 in binary.

Bill
+1  A: 

C++ has nice classes to animate what is going on at the bit level

#include <bitset>
#include <iostream>

int main() {
    std::bitset<16> s(500);
    for(int i = 0; i < 4; i++) {
      std::cout << s << std::endl;
      s >>= 1;
    }

    std::cout << s
              << " (dec " << s.to_ulong() << ")"
              << std::endl;
}
Johannes Schaub - litb