Compound assignment

May 17, 2012 at 2:08am
I read the article this site has on Compound assignments. And I have yet to find the answer to what "<<=" and ">>=" do in general. I tryed google searching on 10 different sites but none have a noob friendly explanation. Any one care to help?.
Last edited on May 17, 2012 at 2:08am
May 17, 2012 at 2:10am
x>>=y is short for x=x>>y. Or do you mean >> and <<? They perform bit shifts to the right and left (which is equivalent to a division or multiplication by 2^y, respectively).
Last edited on May 17, 2012 at 2:12am
May 17, 2012 at 2:16am
I see. But why the number 2?. Why is it 2^y?.

Is it something like x = x >> (2^y). which translates to
x = x (Multiply or divided by) (2^y)?.
May 17, 2012 at 2:25am
Erm, no. It's 2y because the shifts are performed in the binary representation of the number (i.e. base 2). For example, the number 42 in binary is 101010[2]. If you calculate 42>>2 it shifts that two digits to the right (and 0s are shifted in from the left), so it becomes 001010[2]=10. That's equivalent to a integer division by 2²=4.
Same for shifting to the left: the result of 42<<2 is 10101000[2]=168=42*4=42*2².

If the shifts were done in the decimal representation of the number, it would be 10y, e.g. 23 shifted two digits to the left would be 2300, which is equivalent to a multiplication by 10²=100.
Last edited on May 17, 2012 at 2:27am
May 17, 2012 at 2:49am
Oh I think I get it(I think).
Thanks.

So in a base 2 system something like 0010000(4) <<= 2; is just another way of saying to move the 1 from that 0010000 two spaces to the right?(Basically in layman's term). So it would equal 00001000(16).
Last edited on May 17, 2012 at 3:18am
May 17, 2012 at 3:07am
Yes, except that the most significant digits are named first, so 00001002=4 and 4>>2 is 00000012=1.
Topic archived. No new replies allowed.