This week's book giveaway is in the Other Languages forum.We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# Bit shifting

vinita Kh
Ranch Hand
Posts: 49
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;

Mag Hoehme
Ranch Hand
Posts: 194
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 ]

Manish Hatwalne
Ranch Hand
Posts: 2596
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
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

Travis Benning
Ranch Hand
Posts: 74
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
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 (=)?