Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

>> operator

 
denish mehta
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey ppl

In which case >>> will return a -ve value ,if ever?
regards Denish
 
Art Metzer
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since >>> replaces leftmost bits with zeros, I can only conceive of a returned negative value if, in a >>> b, ( ( a < 0 ) && ( ( b % 32 ) == 0 ) ), which in effect means no shifting really takes place at all.
Art
 
Ravindra Mohan
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I too agree with Art.
I was just laudly thinking why do need to shift the signed bit
by >>> when we already have a >> signed shift in java.
Rgds,
Ravindra Mohan.
 
Junilu Lacar
Bartender
Pie
Posts: 7783
62
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
byte b = (byte)(-1 >>> x) // where (x % 32) is from 1 to 24
will result in a negative value (-1)
In general, it is possible to get a negative result when a narrowing conversion is done on the result of the >>> operation since operands will first be promoted. When the narrowing conversion is done, the sign of the result will depend on what is in the sign bit position of the shorter type: negative if 1, positive if 0.
Junilu

[This message has been edited by JUNILU LACAR (edited May 03, 2001).]
 
Kenneth Ho
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the shift rule of x%32 where x is the number of bits shift, an 32-bit integer remains no change if u shift it 32 or 64 bits. For a 64-bit long type, shifting 64 or 128 bits has no effect.
// The shift operator cant change the value of number 1
(int) 1 >> 32;
(int) 1 >> 64;
(long) 1 >> 64;
(long) 1 >> 128;
 
denish mehta
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx a lot all u guys but please tell me one more thing
When ever "x%32" for int comes in to picture or please mention some link on ineternet where i can get complete info about this particular rule.
Regards Densih
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Denish,
See JLS §15.19 Shift Operators
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic