Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Rules Roundup #305

 
Yuri Sforza
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the unsigned right-shift operator
do to a negative number?
- keep it negative
- change its sign

Supposed answer is "change its sign", but what about
the following:
x >>> 0 (or 32, 64,...) ?

--
Y. Sforza
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Yuri,

What does the unsigned right-shift operator
do to a negative number?
- keep it negative
- change its sign
Supposed answer is "change its sign", but what about
the following:
x >>> 0 (or 32, 64,...) ?

The unsigned rightshift operator would take a negative and make it positive.
e.g.
If you have bits like 1111 1111 and shift with >>> 4 you would get an output in bits like 0000 1111 because the unsigned rightshift moves the bits along the line by what ever many places and replaces all of the bits at the left hand side with 0's.
But the signed rightshift >> will replace the left most bits with whatever the signed bit (which is the left most bit) was so if you replace the above 1111 1111 >> 4 you get 1111 1111 because the laft most bit was a 1,
or 0111 1111 >> 4 will be 0000 0111 because left most bit was 0.
<< replaces all right bits with 0's.
I hope this helps.
Davy
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would be the result?

Hint: try writing out the bits.
Davy
 
Yuri Sforza
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the topic was not about right-shift operator,
but about mock-exam error. Applying right-shift with
values like 0, 32, 64, ... will NOT change int variables,
so the imposed answer "change its sign" is not correct
as this is NOT ALWAYS the case.
--
Y. Sforza
 
Yuri Sforza
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would be the result?

To me more interesting would be:

What is the result (i.e. value of b after executing)?
--
Y. Sforza
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yuri,
Please forgive me, i have never thought about trying this out, until now, I am going to have to go over my bitshift operators again to figure out why this happens.
in the above threads that i sent, this is what i was taught, and i tested them but not intensively.
sorry again.
Davy
 
Yuri Sforza
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... have never thought about trying this out, until now, I am going to have to go over my bitshift operators again to figure out why this happens. ...

Well, sometimes not so obvious things happen "behind the scene".
As JLS (�15.19) says "... [for int type of the left-hand operand] only the five lowest-order bits of the right-hand operand are used as the shift distance... The shift distance actually used is therefore always in the range 0 to 31, inclusive."
Also you should be very carefull when using types "smaller" than int.
Example:
byte b = -128; // So bits are 10000000
byte >>>= 2; // 10000000 >>>2 -> 00100000 that is 32
Correct isn't it? No, the result is -32.
It was quite confusing to me first time (I think it would be
confusing for everyone with C/C++ background). The trick here
is byte being promoted to int.
--
Y. Sforza
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic