• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Shift & promotion

 
Amandeep Waraich
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is Marcus Green Test #2 Qn #46:
Given the following variables which of the following lines will compile without error?

The options given are -

My answer is 2, but the answer given is 2,4. Won't assignment of the result to j require casting, as i will be promoted to long?
Thanks,
Aman
[This message has been edited by Ajith Kallambella (edited July 25, 2000).]
 
hemant gome
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aman,
State your question clearly, options are unreadable.
Hemant.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just edited Aman's message for clarity.
Aman, you are right. In (4), i will be promoted to long. However since the resulting expression happens to fit within the range of int, no compiler error is raised.
Any takers on this one?
Ajith
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think, in x >> y, the result will be of the type of the operand which is to the left of >>. In the case of x >> y , the result will of type x.
( Ajith, even if the value is small enough to fit a bigger primitive type, you need to do the casting. For example
float f=0.0000001 will result in a compiler error ,because 0.0000001 is double and it does not matter 0.0000001 is small)
 
rajsim
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think, in case of shift operator:
if left hand operand is int/byte/char/short
then type of the result is int
if left hand operand is long
then the type of result is long
I tried the following program

It prints
80000000
0
It shows that the upper bit is lost during the shift.
It wouldn't have been lost if the result was promoted
to long.
Please correct me if I am wrong

[This message has been edited by rajsim (edited July 25, 2000).]
 
hemant gome
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am full agree with rajsim. Shift will be performed as long
but during assignment leftmost 16 bits will be discarded to fit
the result in int.
Hemant.
[This message has been edited by hemant gome (edited July 25, 2000).]
 
vivek rai
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i agree with srini, in shift operator, the type of the shift expression is the promoted type of the left-hand operand.
in this case both i ( the left hand operand) and j ( the variale being assigned to ) are int, so there is no conflict.
binary numeric promotion is NOT performed on the operands.
so 4 is correct as well.
The toHexString function prints a single zero if the magnitude of the operand is 0, which is the case in 0x80000000<<1
cheers,
vivek
[This message has been edited by vivek rai (edited July 25, 2000).]
 
rajsim
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hemant,
What I meant to say is that there is no truncation during
assignment. Shift operators themselves discard overflowing
bits.
I updated the above sample to show that.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vasansrini,
The rules governing assignment conversions involving floats and doubles is not exactly the same as the ones governing other primitive types. Since representing and retaining the precision of such numbers during conversions and assignments is governed by a concept called Value Sets.
If you are interested, you may want to refer to section 5.2 Assignment Conversion and section 4.2.3 Floating-Point Types, Formats, and Values of the JLS.
Ajith
 
vivek rai
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
( dunno why this < < l gets displayed in an odd way, maybe its taking my plaintext msg to be an html one substitute i < < l .. in the text for expr suitably)
well, i think its done this way,
int i,j
long l
j=expr

1. the left hand side of the shift, i is int, and that will be the datatype of the shift expression expr
2. since it is being assgned to an int, compiler doesnt object
3. when the left hand side is an int, only the 5 relevent bits of the righthandside ( the lowest order bits ) ( l) are considered for shifting.
note that this is why when we shift an int by more than 32, only modulo 32 value is considered.
( as 32 is the maximum number of bits by which an int can be shifted, and 2 raised to power 5 = 32 etc.. )
cheers,
vivek
[This message has been edited by vivek rai (edited July 25, 2000).]
[This message has been edited by vivek rai (edited July 25, 2000).]
[This message has been edited by vivek rai (edited July 25, 2000).]
[This message has been edited by vivek rai (edited July 25, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ajith,
Thanks for the reference.
I think, in that special category we need to add long & int too.
Because long longVar=10; int intVar=longVar does not compile.
{But char c=65 will compile even though 65 is an int by default).
Thanks,
vasansrini.
 
vivek rai
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(a slight digression from the actual topic there .. )
srini,
char c = 65 ; will compile even though 65 is an int literal bcoz its a constant and compiler knows at compile time that it wud fit.
if u change it to
int i = 65;
char c = i;
it wudnt compile.
anyway, i dont think these things are coming into play in the current threads discussion, as usual binary numeric promotion is NOT done in shift operators, its just promoted to the type of the lsft hand operand, and bits of the right hand operand are masked accordingly.
cheers,
vivek
 
Marcela Blei
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vivek, you said:

3. when the left hand side is an int, only the 5 relevent bits of the righthandside ( the lowest order bits ) ( l) are considered for shifting.
note that this is why when we shift an int by more than 32, only modulo 32 value is considered.
( as 32 is the maximum number of bits by which an int can be shifted, and 2 raised to power 5 = 32 etc.. )

Where do you take tis information from?
It�s very well but are you sure it�s right?
 
vivek rai
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Marcela,
took it from JLS ( 2nd edition section 15.19 ). It says:
"If the promoted type of the left-hand operand is int, only the five lowest-order bits of the right-hand operand are used as the
shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & with the mask value 0x1f. The shift distance actually used is therefore always in the range 0 to 31, inclusive.
If the promoted type of the left-hand operand is long, then only the six lowest-order bits of the right-hand operand are used as
the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & with the mask value 0x3f. The shift distance actually used is therefore always in the range 0 to 63, inclusive."
cheers,
vivek
 
Marcela Blei
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Vivek, I�ll read all Chapter 15 carefully
 
Amandeep Waraich
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everybody for their contribution. I was really confused about all this. So, I think the conclusion is that, in the case of shifting, the left-hand operand and the variable being assigned, collectively decide whether casting is needed or not. The right hand operand has no role to play in the decision of casting.
Thanks a lot once again,
Aman
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic