• Post Reply Bookmark Topic Watch Topic
  • New Topic

Operators  RSS feed

 
Raef Kandeel
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which two are equivalent?

A.16>4
B.16/2
C.16*4
D.16>>2
E.16/2^2
F.16>>>2

Apparently D and F are both equivalent. I am familiar with the >> operator but I am not familiar with the >>> operator. What does it do? From my experimentation, they are exactly equivalent.
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Raef Kandeel:
but I am not familiar with the >>> operator. What does it do? From my experimentation, they are exactly equivalent.
Try it with negative numbers.
 
srinivas sridaragaddi
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

with >> shift operator the bits are moved to right by the number which you mentioned and the left most bit would be added by zero's.

But >>> shift operator works the same for positive values but for negative values the left most bits would be replaced by 1's and not zero's

sorry i cannot explain with example as i dont have material right now, but i am sure about the concept that >>> shift operator on a negative numbers shifts the bits as requested and the left most bits would be filled by 1's instead of 0'2.
i will just try and find a link for you give 5 min
 
srinivas sridaragaddi
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops i was very wrong forgive me i got it wrong here is an example hope it helps

Unsigned right-shift >>> (JLS �15.19)
identical to the right-shift operator only the left-bits are zero filled
because the left-operand high-order bit is not retained, the sign value can change
if the left-hand operand is positive, the result is the same as a right-shift
if the left-hand operand is negative, the result is equivalent to the left-hand operand right-shifted by the number indicated by the right-hand operand plus two left-shifted by the inverted value of the right-hand operand
For example: -16 >>> 2 = (-16 >> 2 ) + ( 2 << ~2 ) = 1,073,741,820
Decimal 16 00000000000000000000000000010000

Right-shift 2 00000000000000000000000000010000
fill left 00000000000000000000000000000100
discard right 00000000000000000000000000000100 -> Decimal 4

Decimal -16 11111111111111111111111111110000

>>> 2 11111111111111111111111111110000
fill left 0011111111111111111111111111110000
discard right 00111111111111111111111111111100
 
srinivas sridaragaddi
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
much more details are provided here please go through

http://www.janeg.ca/scjp/oper/shift.html

its clearly explained here
hope i solved your problem
 
srinivas sridaragaddi
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a link which explains shift operators clearly

http://www.janeg.ca/scjp/oper/shift.html

hope it did help your problem
 
Raef Kandeel
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot srinivas. You did solve my problem. Take care
 
Dave Walsh
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All this is fascinating...

Didn't I read somewhere that shifting operators are not on the exam ?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dave Walsh:
..,Didn't I read somewhere that shifting operators are not on the exam ?

They are on the 1.4 exam, but not on the 1.5 exam.
 
Ameen khan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer is D and E
because
16>>2 = 4 moving bit right decreases power of 2
here 2^4 =16 and right shift by 2 means 2^2 =4
16/2^2 = 4
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ameen khan:
16/2^2 = 4
Not correct, since '^' is the XOR operator.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!