• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Bit shifting

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marcus Green Test-II
nebody plz explain y 2 and 4 are correct and how
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;
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vinita,
This one is about primitive types.
The rule is very simple:
Both operands must be integral types (see JLS, � 15.19).
Therefore:
1.) second operand is a string - false
2.) both operands are integers - ok
3.) second operand is a double, which is not an integral - false
4.) both operands are integral types (first is integer, second is long) - ok
Integral types are:
  • byte
  • short
  • char
  • integer
  • long
  • All other data types are not integral types.
  • float and long are floating-point types
  • boolean is a type of its own
  • all other types are objects


  • Please correct me, if I've got something wrong.
    [ May 08, 2002: Message edited by: Mag Hoehme ]
     
    Ranch Hand
    Posts: 2596
    Android Firefox Browser Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    When LHS of the shift operator is an int only lower 5 bits of the RHS are used.
    Option 2 as it is is straight forward, and for option 4 if you need to apply the rule above.
    HTH,
    - Manish
     
    vinita Kh
    Ranch Hand
    Posts: 49
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi everybody,
    though its a silly question
    I understand option 1 and 3 are wrong.
    but i can't understand how 2 & 4 are correct.
    If i'm not wrong whenever we left shift a number it gets doubled, so in case of option 4---
    j = i<<1, result that means i<<1 should be 2 , then how its equal to j.
    in option 2--
    j = i<<j, how to calculate this?
    thanks,
    vinita
     
    Ranch Hand
    Posts: 74
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hi,
    i don't think it is saying j "equals" i<<l, its saying that j "becomes" i<<l.
    Hope this answers your problem. if not, I must have misunderstood you. Sorry.
    Travis B.
     
    Mag Hoehme
    Ranch Hand
    Posts: 194
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Vinita,
    the question of the Marcus Green exam is about types allowed in shift operations.
    Are you sure that you do not mix up EQUALS (==) and ASSIGNMENT operators (=)?
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic