> hi ,
>
> what is the value of
without considering java, -8 in 2's complement is (pretending it's a 16
bit value for the moment)
1111 1111 1111 1000
and -1 is
1111 1111 1111 1111
Then when you take these signed values and feed them to an unsigned
operation like shift,
> -8>>-1
You're asking to shift right by the maximum number that can be
represented in you bit width, and I would expect that to be a
sign-extended negative number, and if you evaluate it as a signed
result, I'd expect to see -1.
Likewise
> -8<<-1
You're asking to shift left by way more bits than exist in the value, so
you end up with all zeros, and I'd expect the answer to be zero.
> System.out.println(-8 << -1);
> System.out.println(-8 >> -1);
> the result is
> 0
> -1
Yep.
I don't understand your last question. "If not -8..."
There is a well defined behavior for this sort of operation and I don't
see anything strange or unexpected from your example.
> what is the value of
>
[quoted text clipped - 21 lines]
>
> but if not -8, what will be the value?and why???
Presumably "<< -1" means "left shift many times" and not "right shift
by 1", due to -1 being converted to an unsigned value (with all bits
set).
So, a negative number right shifted enough times must become -1,
and a negative number left shifted enough times must become 0.
m.