1

Here is my code:

int main(){
 unsigned int z = 18;
 z = z>>1;
 std::cout << z << std::endl;
}

I know my result is going to be 9 but I dont understand the line "z = z>>1" What does it do? what does >> mean? I thought it was only used in cin << z; please explain. Thank you

user2843560
  • 115
  • 4
  • 1
    Hint: it's an operator, which should make it easy to research. – chris Dec 11 '13 at 03:21
  • possible duplicate of [Absolute Beginner's Guide to Bit Shifting?](http://stackoverflow.com/questions/141525/absolute-beginners-guide-to-bit-shifting) – Bryan Chen Dec 11 '13 at 03:25
  • 2
    Hmm, I sometimes despair at how little thought people put into their close reasons. Dupe I could possibly understand but "off-topic" for the reason given? The question didn't ask for code and I fail to see how questioner could have exhibited more understanding of the problem without actually already having the answer. It sometimes appears that SO is shifting away from the "any skill level accepted" model and, while that will no doubt remove some of the rubbish, it may also throw out the baby with the bathwater. Still, I'm just one guy :-) – paxdiablo Dec 11 '13 at 03:53
  • 1
    operator>> and operator<< had other meanings, before the iostream part of the library decides to overload them. – RichardPlunkett Dec 11 '13 at 04:03

2 Answers2

7

>> is the right bit-shift (a) which shifts all bits in a value right by a given value. For unsigned integers, >> 1 is effectively an integral divide by two.

The value 18 in binary is:

0001 0010
   |   |
   |   +---  2
   +------- 16
            --
            18

Right-shifting that by one bit position (bits "drop off" the right hand side and 0 bits come in from the left) gives you:

0000 1001
     |  |
     |  +--  1
     +-----  8
            --
             9

(a) It is in this particular case. C++ also allows for >> to be used as an operator for classes, such as with std::cin >> myInputVar; which will attempt to read a value from standard input and place it into the myInputVar variable.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
1

It is shift to left for 1 bite of z. For example your z is 18, that is (if int is 2 bytes) 0000 0000 0001 0010 bynary, when you shift it, z becomes 0000 0000 0000 1001 whitch is 9. It actualy work as didide by two. If you have negative falues for example -18, in second complement it is

0000 0000 0001 0010
1111 1111 1110 1101//first complement
1111 1111 1110 1110//second complement

and when you shift it, the sign byte is puted on the top

1111 1111 1111 0111
0000 0000 0000 1000//first complement
0000 0000 0000 1001//second complement

witch is -9.

With this shift to right is easy, search web for shift to left, it is more complicated in case of signed values

RockLegend
  • 497
  • 4
  • 9
  • Left shifting a negative value might be undefined behaviour, yes, but right shifting isn't all that easy. Right shifting a negative value has an implementation-defined value. – chris Dec 11 '13 at 03:40