• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

shift operators

 
dhana rangu
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can any one help me in this
What is the result that will be printed out here ?


a)0
b)-256
c)127
d)-127
e)-1
I feel that the answer for the above question is 0.but the answer is -256.
128:0000 0000 1000 0000
i= -128 :1111 1111 1000 0000
byte(i) =1000 0000
byte(i)<< 1 =0000 0000
can any one explain this
dhana
[ January 08, 2004: Message edited by: dhana rangu ]
 
Rama Kumar PV
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dhana,
If you shift a char, byte, or short, it will be promoted to int before the
shift takes place, and the result will be an int.
Try changing the code to
byte i =-128;
System.out.println(i<<=1);
to get the value of 0.
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The casting to byte has absolutly no effect on the result. The result is always -256 whether we kept the (byte) or removed it.
 
dhana rangu
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot vicken and ramkumar..
What i understood is this...
Exlicit casting donot effect the result after shifting ...But implicit casting effect the result after shifting
byte b=-128;
if we shift this we get 0.means after shifting the result is being implicitly casted to byte since b here is a byte..(If i am not wrong)
dhana.
 
Ray Stojonic
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After some testing I note the following:
in the line: System.out.println( (byte)i << 1 );
1) i is cast to byte (casting occurs before shifting in the order of operations)
2) i is promoted to int for shifting
3) i is shifted
4) -256 is printed
if the line is changed to:
System.out.println( (byte)( i << 1 ) );
1) i is shifted
2) result is cast to byte
3) 0 is printed
hth
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ray Stojonic:

if the line is changed to:
System.out.println( (byte)( i << 1 ) );
1) i is shifted
2) result is cast to byte
3) 0 is printed
hth

Ray, here you are casting the result of the shift operation, that is why the answer differs.
 
Ray Stojonic
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ray, here you are casting the result of the shift operation, that is why the answer differs.

Yes, that may be why step 2 says "result is cast to byte"...
example 1 shows what happens in the original example, example 2 shows how it would need to be written to get the 'expected' result
 
dhana rangu
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot ray and vicken finally i understood
 
Shilpi M Agarwal
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i<<1 is same as i*2 and so it is -256.. is this explaination correct?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's correct, but you need to be careful.
In general, you can think of a left or right shift as a multiplication or division by a power of 2, but you need to be careful in that thinking. What happens if a data bit "falls off" the end? Let me change your example just a little:

Hmmm...certainly, -128 * 2 is not 0, but that's what it outputs. Why?
The reason for this is that a byte of data just fell off the end during the shift. Here's what -128 looks like in binary:
10000000
If we left shift that by 1 position, we're left with this:
00000000
That's 0, not -256. You see, left and right shifting is equivalent to multiplication and division only as long as you do not have overflow of some sort. In this case, -256 doesn't fit in a byte (it's range is -128 to 127) so we have overflow. Therefore, our bit operation no longer matches conventional mathematics.
I hope that helps,
Corey
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic