当前位置:文档之家› 计算一个二进制或者十进制的数的原码,反码和补码

计算一个二进制或者十进制的数的原码,反码和补码

import java.util.Scanner;
public class RadixChange
{
static int byteNumber = 8;
// 将输入的操作数转换成八位二进制有符号整数,即得到这个数的原码;
public static String decimalToBinary(String operand, int choice) {
String opeString = " " + operand;
String returnVal = null;
opeString = opeString.trim();
// char [] src={'0','0','0','0','0','0','0'};
if (choice == 0)
{
returnVal = Integer.toString(
new Integer(operand).intValue(), 2);
}
else
{
returnVal = opeString;
}
/**
* 将二进制数据用八位二进制原码表示
*/
// 如果是有符号二进制
if (choice == 1)
{
if (returnVal.codePointAt(0) == 48)
for (int i = returnVal.length(); i < byteNumber; i++)
{
returnVal = "0" + returnVal;
}
else
{
returnVal = returnVal.substring(1);
for (int i = returnVal.length(); i < byteNumber; i++)
{
if (i == byteNumber - 1)
returnVal = "1" + returnVal;
else
{
returnVal = "0" + returnVal;
}
}
}
return returnVal;
}
// 如果是十进制
else
{
if (Integer.parseInt(operand) > 0)
for (int i = returnVal.length(); i < byteNumber; i++)
{
returnVal = "0" + returnVal;
}
else
{
returnVal = returnVal.substring(1);
System.out.println(returnVal);
for (int i = returnVal.length(); i < byteNumber; i++)
{
if (i == byteNumber - 1)
{
returnVal = "1" + returnVal;
continue;
}
else
{
returnVal = "0" + returnVal;
}
}
}
}
return returnVal;
}
public static String getComplement(String trueForm)
{
String complement = " ";
String charSequence = " ";
// 暂时存放得到的反码值
int[] temp = new int[byteNumber];
int flag = 0;
/**
* 如果操作数为整数,原值返回
*/
// 字符型常量'0'所对应的unicode码是48
if (trueForm.codePointAt(0) == 48)
{
System.out.println("反码是" + trueForm);
return trueForm;
}
else
{
for (int i = 0; i < trueForm.length(); i++)
{
if (i == 0)
{
temp[i] = trueForm.codePointAt(i) - 48;
continue;
}
else
{
if (trueForm.codePointAt(i) == 48)
temp[i] = 1;
else
{
temp[i] = 0;
}
}
}
}
for (int i = temp.length - 1; i >= 0;)
{
complement = temp[i] + complement;
i--;
}
// 此时complement的值是反码
complement = complement.trim(); // 去掉空格
System.out.println("反码是" + complement);
for (int i = 0; i < byteNumber; i++)
{
if (i == 0)
charSequence = "1" + charSequence;
else
{
charSequence = "0" + charSequence;
}
}
charSequence = charSequence.trim();
complement = stringLineAdd(complement, charSequence);
return complement;
}
// 两个二进制进行相加运算
public static String stringLineAdd(String complement,
String charSequence)
{
String resultString = " ";
int flag = 0; // 标志进位符号,为0表示不进位,1表示进位
int i = complement.length() - 1;
while (i >= 0)
{
if (flag == 0)
{
int temp = complement.codePointAt(i)
+ charSequence.codePointAt(i) - 96;
if (temp >= 2)
{
temp = temp - 2;
flag = 1;
}
else
{
flag = 0;
}
resultString = temp + resultString;
}
else
{
int temp = complement.codePointAt(i)
+ charSequence.codePointAt(i) + 1 - 96;
if (temp >= 2)
{
flag = 1;
temp = temp - 2;
}
else
{
flag = 0;
}
resultString = temp + resultString;
}
i--;
}
resultString = resultString.trim();
return resultString;
}
public static void main(String[] args)
{
System.out.println("输入你要计算的操作数,同时标注你所输入的操作数是十进制还是二进制,"
+ "如果是十进制紧随操作数后跟上" + ",0反之为,1");
System.out.println("示例输入:121,0");
String inputString = new Scanner(System.in).next();
String[] arrayStrings = inputString.split(",");
String returnVal = decimalToBinary(arrayStrings[0],
new Integer(arrayStrings[1]).intValue());
System.out.println("原码是" + returnVal);
System.out.println("补码是" + getComplement(returnVal));
}
}。

相关主题