public class Binary { public static void main(String[] args) { int pixel = 0b10010010; System.out.print("original: " + pixel + " = "); System.out.println("0b" + Integer.toBinaryString(pixel)); int upper = pixel & 0b11110000; System.out.println("upper: 0b" + printBinary(upper)); upper = upper >> 4; System.out.println("upper: 0b" + printBinary(upper)); int lower = pixel & 0b00001111; System.out.println("lower: 0b" + printBinary(lower)); lower = lower << 4; System.out.println("lower: 0b" + printBinary(lower)); pixel = upper | lower; System.out.print("transformed: " + pixel + " = "); System.out.println("0b" + printBinary(pixel)); } public static String printBinary(int value) { return String.format("%8s", Integer.toBinaryString(value)).replace(" ", "0"); } }
© 2015 CS160 Colorado State University. All Rights Reserved.