PDA

View Full Version : bit comparison help


viperv25
03-20-2005, 11:08 PM
Hello everyone,

I know this may be a easy program for you all, but I am struggling on it.

I need to provide two bit strings like 1011 0001 and 0010 0110 and display:
Bitwise | 1111 0111
Bitwise & 0010 0000
Bitwise ^ 1001 0111

I keep getting the decimal version of the number.

My Code:

public static void main(String args[]){
int x = 1011 0001;
int x2 = 0010 0110;

System.out.println("OR operator");
System.out.println(x|x2);
System.out.println("AND operator");
System.out.println(x&x2);
System.out.println("XOR operator");
System.out.println(x^x2);
}

I am not a programmer. Barely surviving this stuff.

Any advise or help would be much appreciated.

abbeyroadd
03-21-2005, 01:34 AM
Is this Java?

cfc
03-21-2005, 06:10 AM
Is this Java?
yes it is.

This will do the and operation and display it in the format you want, the rest you should be able to do from this example:

public class BinaryTest {
public static void main (String[] argv)
{
String x = "1000 0001";
String x2 = "1001 1011";

int bin1 = Integer.parseInt(x.substring(0, 4) + x.substring(5, 9), 2);
int bin2 = Integer.parseInt(x2.substring(0, 4) + x2.substring(5, 9), 2);

int andOp = bin1 & bin2;
String andStr = Integer.toString(andOp, 2);
System.out.println(andStr.substring(0, 4) + " " + andStr.substring(4, 8));
}
}


The second arguments to Integer.parseInt() and Integer.toString() (both static) are radixes. 10 (decimal) is assumed if the argument is not given, 2 is for binary. That's really all you needed to learn, so I don't think it's too much of a problem that I gave you an answer...