M Adawi
Posts: 9
Nickname: adawi
Registered: Mar, 2006
|
|
Re: java program problem..
|
Posted: May 1, 2006 4:17 AM
|
|
import java.io.*;
/* Notes:
-Read Long.parseLong(String s,radix) from java API.
-Remeber that int (signed integer) is in range negative 2^(to the power)31 and positive 2^32.
-Solution of Matthias Neumair is more correct and much more faster because I used Long.parseLong which will convert a String of (bits) to long which need alot of calculations with alot of cpu consuming multiplications, then reconvert long to BinaryString which also supose to be cpu consuming -I think you will got C if you write this program as it. so update it as Matthias Neumair told you.
-Best Wishes Mohammed Eladawi */
class Bitwise { public static String and (String s1, String s2) { return Long.toBinaryString(Long.parseLong(s1,2) & Long.parseLong(s2,2)); } public static void main (String arg[]) throws Exception { DataInputStream in1 = new DataInputStream (System.in); DataInputStream in2 = new DataInputStream(System.in); byte[] input1 = new byte[34]; byte[] input2 = new byte[34]; System.out.print("Input1>>"); int count1 = in1.read(input1);
System.out.print("Input2>>"); int count2 = in2.read(input2); String inputS1; String inputS2; if (count1 > 2) inputS1 = new String(input1 , 0 , count1 - 2); else inputS1 = new String("0"); if (count2 > 2) inputS2 = new String(input2 , 0 , count2 - 2); else inputS2 = new String("0"); System.out.println(inputS1 + "\n and \n" + inputS2 + "\n = \n" + (and (inputS1 , inputS2)));
in1.close(); in2.close(); } }
|
|