• 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

Questions about "?:" and "shift"

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have two questions:
1.This is a part of my program.

int i ;
i<=5?1:0;
System.out.println(i);

When it is compiled, the screen prints "not a statement".
i<=5?1:0;
^
What does it mean? Or where is the error?

2.I am confused with the usage of "shift".
If I shift a negative number, what's the meaning?
for example:
int x>>-4;
How to calculate it?

Thanks.
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) You need to do something with the expression such as assign it to create a statement. For instance
i = i<=5?1:0;
2) I believe shifting by a negative distance has no meaning
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


int i ;
i<=5?1:0;
System.out.println(i);
When it is compiled, the screen prints "not a statement".
i<=5?1:0;
^
What does it mean? Or where is the error?


First, the local variable 'i' is not initialized; you must explicitly initialize it
int i=0;
Secondly, the ternary operator returns a value and that value must be assigned to a variable
int i = 0;
int result = 0;
result = i<=5?1:0;
or you can print the result directly
System.out.println(i<=5?1:0);


byte i=125;
i<<-4


This shift expression is equivalent to i<<124. To learn more about negative shifting you can check out MM post here
reply
    Bookmark Topic Watch Topic
  • New Topic