• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question from RHE book

 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which of the following expressions results in a positive value in x
1. int x = -1; x = x >>> 5;
2. int x = -1; x = x >>> 32;
3. byte x = -1; x = x >>> 5;
4. int x = -1; x = x >> 5;
Which is the correct answer and how? Please all java gurus experts are needed.
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. int x = -1; x = x >>> 5;
binary representation of -1
11111111 11111111 11111111 11111111
-1>>>5
right shift replaces 1 with 0
00000111 11111111 11111111 11111111
value is large positive value

2. int x = -1; x = x >>> 32;
binary representation of -1
11111111 11111111 11111111 11111111
-1>>>32
shifts the bits 32 places hence value remains same negative
3. byte x = -1; x = x >>> 5;
results in compiler error loss of precision
4. int x = -1; x = x >> 5;
11111111 11111111 11111111 11111111
-1>>5
>> replaces 1 with 1
hence values remains large negative value
hope this helps
zarina
[ August 14, 2002: Message edited by: zarina mohammad ]
[ August 14, 2002: Message edited by: zarina mohammad ]
[ August 14, 2002: Message edited by: zarina mohammad ]
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
-1>>>32 is equivalent to -1>>>0 which is -1, not 0.
In general, if i is an int, then
i >>> j
is the same as
i >>> (j % 32)
The same is true for >> and << .
If , instead, i is a long, then
i >>> j
is the same as
i >>> (j % 64)
and again, the same is true for << and >> .
 
zarina mohammad
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ron,
i edited the error. hope the answer is correct now
zarina
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excuse me! May I ask something?
Why does a byte value loose precision?
If byte has 8 bits, I can say that byte x = -1 is:
1111 1111
right?
So,if you make x >>> 5, then it should be:
0000 0111
Can you explain it to me?
Thanks!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic