Thinking in Java Fourth Edition Bruce Eckel Chapter 2 Exercise 8

Exercise 8:  Show that hex and octal notations work with long values. Use Long.toBinaryString( ) to display the results.

Solution:

public class LongValues
{
    public static void main(String[] args)
    {
        long n1 = 0xffff; // hexadecimal
        long n2 = 077777; // octal
        System.out.println("long n1 in hex = " + Long.toBinaryString(n1));
        System.out.println("long n2 in oct = " + Long.toBinaryString(n2));
    }
}

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *