Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# shift operator

Anonymous
Ranch Hand
Posts: 18944
Has anybody tried shifting an integral number by negative number of places say, 9 >> -2. It does not give any error ?
But it gives some strange result. It seems that signed or unsigned right shift of any integral number by negative number gives 0 but the same with signed left shift gives some strange result. I wonder what's happening here !!!

Marcela Blei
Ranch Hand
Posts: 477

Has anybody tried shifting an integral number by negative number of places say, 9 >> -2. It does not give any error ?

add 32 to the negative value 32 - 2 = 30
Try: 9 << 30 and 9 << -2
They show the same result

[This message has been edited by Marcela Blei (edited July 26, 2000).]

Uvnik Gupta
Ranch Hand
Posts: 32
Hi Marcela,
I couldn't understand how does 32%2 gives 30. It should give 0 as 32 is completely divisible by 2.

Marcela Blei
Ranch Hand
Posts: 477
Sorry, that was confusing! (I edited my first message)
view: http://www.javaranch.com/ubb/Forum24/HTML/002679.html

[This message has been edited by Marcela Blei (edited July 26, 2000).]

Chris Cleverley
Greenhorn
Posts: 22
I got it Marcela. Thanks.
Chris