用Java编写程序实现能够实现定点整数的机器数表示、定点整数的变形补码加减运算、定点整数的原码一位乘法运算和浮点数的加减运算。
时间: 2024-03-12 19:47:57 浏览: 185
定点整数的机器数表示:
```java
public class FixedPointNumber {
private int bits; // 总位数
private int integerBits; // 整数位数
private int fractionBits; // 小数位数
private int value; // 值
public FixedPointNumber(int bits, int integerBits, int fractionBits, int value) {
this.bits = bits;
this.integerBits = integerBits;
this.fractionBits = fractionBits;
this.value = value;
}
public int getBits() {
return bits;
}
public int getIntegerBits() {
return integerBits;
}
public int getFractionBits() {
return fractionBits;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
// 将整数转为定点数
public static FixedPointNumber fromInteger(int integer, int integerBits, int fractionBits) {
int value = integer << fractionBits;
return new FixedPointNumber(integerBits + fractionBits, integerBits, fractionBits, value);
}
// 将小数转为定点数
public static FixedPointNumber fromFloat(float floatValue, int integerBits, int fractionBits) {
int value = (int) (floatValue * (1 << fractionBits));
return new FixedPointNumber(integerBits + fractionBits, integerBits, fractionBits, value);
}
// 将定点数转为浮点数
public float toFloat() {
return ((float) value) / (1 << fractionBits);
}
// 将定点数转为整数
public int toInteger() {
return value >> fractionBits;
}
// 将定点数转为字符串
public String toString() {
StringBuilder sb = new StringBuilder();
int mask = 1 << (bits - 1);
for (int i = 0; i < bits; i++) {
if ((value & (mask >> i)) != 0) {
sb.append("1");
} else {
sb.append("0");
}
if (i == integerBits - 1) {
sb.append(".");
}
}
return sb.toString();
}
}
```
定点整数的变形补码加减运算:
```java
public class FixedPointArithmetic {
private int bits; // 总位数
private int integerBits; // 整数位数
private int fractionBits; // 小数位数
private int mask; // 位掩码
public FixedPointArithmetic(int bits, int integerBits, int fractionBits) {
this.bits = bits;
this.integerBits = integerBits;
this.fractionBits = fractionBits;
this.mask = (1 << bits) - 1;
}
// 定点数变形补码加法
public FixedPointNumber add(FixedPointNumber a, FixedPointNumber b) {
int sum = (a.getValue() & mask) + (b.getValue() & mask);
if (sum >= (1 << bits)) {
sum -= (1 << bits);
}
return new FixedPointNumber(bits, integerBits, fractionBits, sum);
}
// 定点数变形补码减法
public FixedPointNumber sub(FixedPointNumber a, FixedPointNumber b) {
int diff = (a.getValue() & mask) - (b.getValue() & mask);
if (diff < 0) {
diff += (1 << bits);
}
return new FixedPointNumber(bits, integerBits, fractionBits, diff);
}
// 定点整数的原码一位乘法运算
public FixedPointNumber mul(FixedPointNumber a, FixedPointNumber b) {
int product = (a.getValue() * b.getValue()) >> b.getFractionBits();
return new FixedPointNumber(bits, integerBits, fractionBits, product);
}
}
```
浮点数的加减运算:
```java
public class FloatArithmetic {
private int bits; // 总位数
private int exponentBits; // 指数位数
private int fractionBits; // 小数位数
private int bias; // 偏移量
public FloatArithmetic(int bits, int exponentBits, int fractionBits, int bias) {
this.bits = bits;
this.exponentBits = exponentBits;
this.fractionBits = fractionBits;
this.bias = bias;
}
// 浮点数加法
public float add(float a, float b) {
// 将 a 和 b 转换为 IEEE 754 浮点数
int aBits = Float.floatToIntBits(a);
int bBits = Float.floatToIntBits(b);
// 提取符号位、指数位和尾数位
int aSign = (aBits >> 31) & 0x1;
int aExponent = (aBits >> 23) & 0xff;
int aFraction = aBits & 0x7fffff;
int bSign = (bBits >> 31) & 0x1;
int bExponent = (bBits >> 23) & 0xff;
int bFraction = bBits & 0x7fffff;
// 对阶
int shift = Math.abs(aExponent - bExponent);
if (aExponent > bExponent) {
bFraction |= 0x800000;
bFraction >>= shift;
bExponent = aExponent;
} else {
aFraction |= 0x800000;
aFraction >>= shift;
aExponent = bExponent;
}
// 溢出检查
if (aExponent == 0xff || bExponent == 0xff) {
return Float.NaN;
}
// 计算和的符号位、指数位和尾数位
int sumSign, sumExponent, sumFraction;
if (aSign == bSign) {
sumSign = aSign;
sumExponent = aExponent;
sumFraction = aFraction + bFraction;
if (sumFraction >= 0x800000) {
sumFraction >>= 1;
sumExponent++;
}
} else {
if (aFraction == bFraction) {
return 0.0f;
}
if (aFraction > bFraction) {
sumSign = aSign;
sumExponent = aExponent;
sumFraction = aFraction - bFraction;
} else {
sumSign = bSign;
sumExponent = bExponent;
sumFraction = bFraction - aFraction;
}
while ((sumFraction & 0x800000) == 0) {
sumFraction <<= 1;
sumExponent--;
}
}
// 将和转换为浮点数
int sumBits = (sumSign << 31) | (sumExponent << 23) | (sumFraction & 0x7fffff);
return Float.intBitsToFloat(sumBits);
}
// 浮点数减法
public float sub(float a, float b) {
// 将 b 取反后加上 a
int bBits = Float.floatToIntBits(b);
bBits ^= 0x80000000;
return add(a, Float.intBitsToFloat(bBits));
}
}
```
阅读全文