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>

<output>
n:      11110000111100001111000011110000
n>>8:   11111111111100001111000011110000
n>>>8:  11111111111100001111000011110000
n<<8:   11110000111100001111000000000000
n:      00001111000011110000111100001111
n>>8:   00000000000011110000111100001111
n>>>8:  00000000000011110000111100001111
n<<8:   10001111000011110000111100000000
</output>

Cheers,

J.

+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+

www:    http://www.elephant.org/~azazel/
mail:   [EMAIL PROTECTED]

+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+

Reply via email to