This week's book giveaway is in the Design forum.We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!

# Questions about "?:" and "shift"

YingShiuan Chang
Greenhorn
Posts: 6
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.

Carlos Failde
Ranch Hand
Posts: 84
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

Vicken Karaoghlanian
Ranch Hand
Posts: 522

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