DanielV18 de Febrero del 2010
PreguntaTengo un problema. Supuestamente este programa pasa de un numero decimal a binario puro, complemento a 1 y complemento a dos, pero no se por que narices no funciona. Si me pudierais hechar una mano os estaria muy agradecido.
import java.io.IOException;
import java.util.Scanner;
/**
*
*
* This program calculates some binary representations of a given integer.
*/
public class Repr {
private static int[] numbers = new int[10];
public static void main(String[] args) throws IOException{
readNumber();
print();
}
/**
* Read an array of bytes and convert it to an integer. This integer is the one we will use to generate the numbers we need.
* @throws java.io.IOException
*/
public static void readNumber() throws java.io.IOException{
Scanner sc = new Scanner (System.in);
RepresentacionBinaria rb = new RepresentacionBinaria();
System.out.println("Please, type a number:");
int number = sc.nextInt();
}
/**
* This method return a String with the int converted to binary.
*/
public static String pureBinary(int number){
String pureBin = "0";
StringBuffer buf= new StringBuffer();
if(number == 0){
return pureBin;
}
int remaining = Math.abs(number);
while(remaining > 0){
if(remaining%2 == 0){
buf.append("0");
}else{
buf.append("1");
}
remaining = remaining / 2;
}
return pureBin = buf.reverse().toString();
}
/**
* This method return a String with the int converted to 1s complement binary.
* @param number The number we want to convert.
* @return comp1 String with the number in 1s complement binary.
*/
public static String complement1(int number){
String comp1 = "";
StringBuffer buf = new StringBuffer("0000000000000000000000000000000");
comp1 = pureBinary(number);
buf.replace(buf.length() - comp1.length() +1, buf.length(), comp1);
if(number >= 0)
return comp1 = buf.toString();
for(int i = 0; i < buf.length(); i++){
if(buf.charAt(i) == '0'){
buf.replace(i, i+1, "1");
}else{
buf.replace(i, i+1, "0");
}
}
return comp1 = buf.toString();
}
/**
* This method return a String with the int converted to 2s complement binary.
* @param number The number we want to convert.
* @return comp2 String with the number in 2s complement binary.
*/
public static String complement2(int number){
String comp2 = "";
StringBuffer buf = new StringBuffer(complement1(number));
if (number >= 0)
return comp2 = buf.toString();
for(int i = buf.length() -1; i > 0; i--){
if(buf.charAt(i) == '0'){
buf.replace(i, i+1, "1");
return comp2 = buf.toString();
}else{
buf.replace(i, i+1, "0");
}
}
return comp2 = buf.toString();
}
/**
* Prints the results
*/
public static void print(){
System.out.println("DecimalttPure Binarytt1's Complementtttt2's Complement");
System.out.println("=======tt===========tt===============tttt===============");
for(int i = 0; i < numbers.length; i++){
System.out.println(numbers[i]+"tt"+pureBinary(numbers[i])+"tt"+
complement1(numbers[i])+"t"+complement2(numbers[i]));
}
}
}
import java.io.IOException;
import java.util.Scanner;
/**
*
*
* This program calculates some binary representations of a given integer.
*/
public class Repr {
private static int[] numbers = new int[10];
public static void main(String[] args) throws IOException{
readNumber();
print();
}
/**
* Read an array of bytes and convert it to an integer. This integer is the one we will use to generate the numbers we need.
* @throws java.io.IOException
*/
public static void readNumber() throws java.io.IOException{
Scanner sc = new Scanner (System.in);
RepresentacionBinaria rb = new RepresentacionBinaria();
System.out.println("Please, type a number:");
int number = sc.nextInt();
}
/**
* This method return a String with the int converted to binary.
*/
public static String pureBinary(int number){
String pureBin = "0";
StringBuffer buf= new StringBuffer();
if(number == 0){
return pureBin;
}
int remaining = Math.abs(number);
while(remaining > 0){
if(remaining%2 == 0){
buf.append("0");
}else{
buf.append("1");
}
remaining = remaining / 2;
}
return pureBin = buf.reverse().toString();
}
/**
* This method return a String with the int converted to 1s complement binary.
* @param number The number we want to convert.
* @return comp1 String with the number in 1s complement binary.
*/
public static String complement1(int number){
String comp1 = "";
StringBuffer buf = new StringBuffer("0000000000000000000000000000000");
comp1 = pureBinary(number);
buf.replace(buf.length() - comp1.length() +1, buf.length(), comp1);
if(number >= 0)
return comp1 = buf.toString();
for(int i = 0; i < buf.length(); i++){
if(buf.charAt(i) == '0'){
buf.replace(i, i+1, "1");
}else{
buf.replace(i, i+1, "0");
}
}
return comp1 = buf.toString();
}
/**
* This method return a String with the int converted to 2s complement binary.
* @param number The number we want to convert.
* @return comp2 String with the number in 2s complement binary.
*/
public static String complement2(int number){
String comp2 = "";
StringBuffer buf = new StringBuffer(complement1(number));
if (number >= 0)
return comp2 = buf.toString();
for(int i = buf.length() -1; i > 0; i--){
if(buf.charAt(i) == '0'){
buf.replace(i, i+1, "1");
return comp2 = buf.toString();
}else{
buf.replace(i, i+1, "0");
}
}
return comp2 = buf.toString();
}
/**
* Prints the results
*/
public static void print(){
System.out.println("DecimalttPure Binarytt1's Complementtttt2's Complement");
System.out.println("=======tt===========tt===============tttt===============");
for(int i = 0; i < numbers.length; i++){
System.out.println(numbers[i]+"tt"+pureBinary(numbers[i])+"tt"+
complement1(numbers[i])+"t"+complement2(numbers[i]));
}
}
}
