Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# 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