/* * Main.java * * Created on 3 Απρίλιος 2008, 3:26 μμ * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ import javax.swing.JOptionPane; /** * * @author students */ public class Main { static String input(){ return JOptionPane.showInputDialog(null, "Enter number: "); } static String input2(){ return JOptionPane.showInputDialog(null, "Enter number of digit to find: "); } public static void showbits(int n, int i){ int m=1; m = m << i-1; int r = m&n; r = r >> i-1; JOptionPane.showMessageDialog(null, "The digit you asked is: " + r); } public static void showbits2(int n){ for (int i=31;i>=1;i--){ int m=1; m = m << i-1; int r = m&n; r = r >> i-1; System.out.print(r); } } public Main() { } public static void main(String[] args) { String in=input(); String y; int x=Integer.parseInt(in); y=Integer.toBinaryString(x); int z=Integer.parseInt(y); System.out.print(y + " \n\n"); JOptionPane.showMessageDialog(null,"The binary form of " + x + " is " + z); String biv=input2(); int biv2=Integer.parseInt(biv); showbits(x,biv2); showbits2(x); } }