///////////////////////////////////// // AC 1718 - MIEI - FCT/UNL // fichas 01, 02 ///////////////////////////////////// // ficha 01 - 6 // public class BinOperations { static byte b; private static void binoper( byte b ) { int x1, x2; System.out.println("b= " + b + " " + Integer.toBinaryString(b)); // nota: em java o resultado dos operadores binários são sempre inteiros System.out.println("a) b | 2 : " + (b|0x2) + " " + Integer.toBinaryString(b | 0x2)); System.out.println("b) b & 0xF : " + (b & 0xF) + " " + Integer.toBinaryString(b & 0xF)); System.out.println("c) b & 0xFB : " + (b & 0xFB) + " " + Integer.toBinaryString(b & 0xFB)); System.out.println("d) b & 1 : " + (b & 1) + " " + Integer.toBinaryString(b & 1)); System.out.println("e) b << 4 : " + (b << 4) + " " + Integer.toBinaryString(b << 4)); System.out.println("f) b >> 2 : " + (b >> 2) + " " + Integer.toBinaryString(b >> 2)); x1 = b << 3; // b*2^3 x2 = b << 2; // b*2^2 System.out.println("g) b*8 : " + (x1) + " " + Integer.toBinaryString(x1)); System.out.println("g) b*4 : " + (x2) + " " + Integer.toBinaryString(x2)); System.out.println("g) b*12 : " + (x1+x2) + " " + Integer.toBinaryString(x1+x2)); } public static void main( String[] args) { b = 127; binoper(b); b = (byte)0x86; // tem bit mais signif a 1 binoper(b); b = 0x33; binoper(b); } } ///////////////////////////////////// // ficha 01 - 8 // ficha 02 - 2 // class PrintFPBin { static void printBin( int val ) { for (int i=0; i>>(Integer.SIZE-1)); val = val<<1; } System.out.println(""); } static int getExp( float f ) { int x = Float.floatToIntBits(f) >>23; return (x&0xff) - 127; } static public void main( String[] args ) { printBin( Float.floatToRawIntBits(0.1f) ); System.out.println( Integer.toBinaryString(Float.floatToIntBits(0.1f)) ); System.out.println( getExp(0.1f) ); System.out.printf("%.20f\n", 0.1f); } }