Azazel <[EMAIL PROTECTED]> writes:

> I'm out of patience and ideas.  Can somebody tell me why the following code gives the
> output it does.  The programme should take a given long number, shift it 8 bits each
> way (signed and unsigned), and print all the different bit-patterns.  As I see it, 
>one
> of two things is happening.
> 
> 1.    There's a bug in the JDK and bitwise operators don't work properly, which
>       seems unlikely.
> 2.    The dprint function is not printing the actual bit-strings but I can't see why.
> 
> <code>
> public class pubtest
>     {
>     public static void main(String args[])
>         {
>         long n;
> 
>         n=0xf0f0f0f0;
> 
>         System.out.print("n:\t");
>         dprint(n);
>         System.out.print("n>>8:\t");
>         dprint(n>>8);
>         System.out.print("n>>>8:\t");
>         dprint(n>>>8);
>         System.out.print("n<<8:\t");
>         dprint(n<<8);
> 
>         n=0x0f0f0f0f;
> 
>         System.out.print("n:\t");
>         dprint(n);
>         System.out.print("n>>8:\t");
>         dprint(n>>8);
>         System.out.print("n>>>8:\t");
>         dprint(n>>>8);
>         System.out.print("n<<8:\t");
>         dprint(n<<8);
>         }
> 
>     private static long dprint(long l)
>         {
>         for(int i=1<<31; i!=0; i>>>=1)
>             System.err.print((l&i)!=0?"1":"0");
>         System.err.print("\n");
> 
>         return l;
>         }
>     } 
> </code>
> 

long is 64 bit
int is 32 bit

If you want to use long:
Change n=0xf0f0f0f0 to n=0xf0f0f0f0L,
n=0x0f0f0f0f to n=0x0f0f0f0fL
and
        for(int i=1<<31; i!=0; i>>>=1)
           System.err.print((l&i)!=0?"1":"0");
to
        for(long i=1L<<63; i!=0; i>>>=1)   // Note the L behind the 1
           System.err.print((l&i)!=0?"1":"0");

If you want to use int:
Just change long n to int n.

By the way, you can use Integer.toBinaryString(int)/Long.toBinaryString(long)
to display int/longs in base 2 format.

        Jürgen

-- 
Juergen Kreileder, Universitaet Dortmund, Lehrstuhl Informatik V
Baroper Strasse 301, D-44221 Dortmund, Germany
Phone: ++49 231/755-5806, Fax: ++49 231/755-5802

Reply via email to