• 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 frm Mock Test abt Bit shift operator

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I got one ques. frm Marcus Green Mock Test that says
Given the following variables which of the following lines will compile without error?
String s = "Hello";
long l = 99;
double d = 1.11;
int i = 1;
int j = 0;
1) j= i <<s;
2) j= i<<j;
3) j=i<<d;
4)j=i<<l;

ans given is
2)j= i<<j;
4)j=i<<l;
I can understand option 2)
but in option 4) how a int(32 bits) can be shifted to 99 places?
is it something wrong with my understanding please correct me?
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To answer your question, it is valid to do something like say " 8 << 97 ".
This is the same as writing 8 << 1 . What you do is take the mod of 97 % 32 (which gives you the remainder of 1) .. and you left shift by the remainder value. Try writing a sample program, and you'll see.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought bitshifts had to be with integers or those types that can be implicitly cast UP to integers (32 bits). In that case, l = 99 is a long, so shouldn't it croak? After all, it doesn't actually check to see that long l fits in an int bucket...or is the long actually converted to 32 bits (chopping off anything left of 32 bits)?
Ross
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ross:
Quot from JLS:

The type of each of the operands of a shift operator must be a primitive integral
type, or a compile-time error occurs.


long will be integral type.
Thanks
Barkat
 
I don't always make ads but when I do they're tiny
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic