aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Shift & promotion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Shift & promotion" Watch "Shift & promotion" New topic
Author

Shift & promotion

Amandeep Waraich
Ranch Hand

Joined: Jul 14, 2000
Posts: 56
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

Joined: Jul 24, 2000
Posts: 15
Aman,
State your question clearly, options are unreadable.
Hemant.


hem@nt
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
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


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: May 31, 2000
Posts: 116
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

Joined: Jul 24, 2000
Posts: 15
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

Joined: May 08, 2000
Posts: 45
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

Joined: May 31, 2000
Posts: 116
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

Joined: Mar 17, 2000
Posts: 5782
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

Joined: May 08, 2000
Posts: 45
( 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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: May 08, 2000
Posts: 45
(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

Joined: Jun 28, 2000
Posts: 477
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

Joined: May 08, 2000
Posts: 45
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

Joined: Jun 28, 2000
Posts: 477
Thanks Vivek, I�ll read all Chapter 15 carefully
Amandeep Waraich
Ranch Hand

Joined: Jul 14, 2000
Posts: 56
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Shift & promotion
 
Similar Threads
Marcus Exam2 #46
Bit shifting?
MockExam2 Question
Sorry for the misprint of my previous question
Marcus Q46