a quizz question about java bit operation???  
Author Message
tyshanchn





PostPosted: 2006-5-10 11:04:00 Top

java-programmer, a quizz question about java bit operation??? hi ,


what is the value of

-8>>-1
-8<<-1


it will be like

public class BitOperation {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(-8 << -1);
System.out.println(-8 >> -1);
}

}

the result is
0
-1


but if not -8, what will be the value?and why???

 
James McGill





PostPosted: 2006-5-10 11:31:00 Top

java-programmer >> a quizz question about java bit operation??? On Tue, 2006-05-09 at 20:03 -0700, tyshanchn wrote:
> 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.

 
Matt Atterbury





PostPosted: 2006-5-10 12:04:00 Top

java-programmer >> a quizz question about java bit operation??? "tyshanchn" <email***@***.com> writes:

> what is the value of
>
> -8>>-1
> -8<<-1
>
>
> it will be like
>
> public class BitOperation {
>
> /**
> * @param args
> */
> public static void main(String[] args) {
> // TODO Auto-generated method stub
> System.out.println(-8 << -1);
> System.out.println(-8 >> -1);
> }
>
> }
>
> the result is
> 0
> -1
>
>
> 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.
 
 
Thomas Schodt





PostPosted: 2006-5-10 17:11:00 Top

java-programmer >> a quizz question about java bit operation??? tyshanchn wrote:
> hi ,
>
>
> what is the value of
>
> -8>>-1
> -8<<-1
>
>
> it will be like
>
> public class BitOperation {
>
> /**
> * @param args
> */
> public static void main(String[] args) {
> // TODO Auto-generated method stub
> System.out.println(-8 << -1);
> System.out.println(-8 >> -1);
> }
>
> }
>
> the result is
> 0
> -1
>

int bitshift only uses the last 5 bits of the right hand side argument
(long bitshift uses the last 6 bits).

-1 & 0x1f is 31

the rest is obvious.