diff --git a/LAB1/bits.c b/LAB1/bits.c index 9177e85..28dec5b 100644 --- a/LAB1/bits.c +++ b/LAB1/bits.c @@ -335,7 +335,18 @@ int howManyBits(int x) { * Rating: 4 */ unsigned floatScale2(unsigned uf) { - return 2; + unsigned sign = (0x80000000)&uf; + unsigned exp = (0x7f800000)&uf; + unsigned frac = (0x007fffff)&uf; + if(exp == 0x7f800000) + return uf; //如果是exp全为255,frac全是0就是无穷,不是就是NaN,都直接return + if(exp == 0x00000000){ + if(frac == 0x00000000) + return uf; //exp全为0,frac全为0,则为0,0*2=0,原样不动 + return (frac<<1)|sign|exp;//exp全为0,frac不全为0,注意到非规格化极小和规格化之间是连续的,即frac第一位为1时,左移会把exp变为非全0,回到规格化 + } + return (exp+0x00800000)|sign|frac; + } /* * floatFloat2Int - Return bit-level equivalent of expression (int) f diff --git a/README.md b/README.md index 14eebc4..8e95ceb 100644 --- a/README.md +++ b/README.md @@ -25,4 +25,7 @@ A CSAPP LAB 7. Update Makefile ### 2023/10/9 Update: -1. Finish howManyBits \ No newline at end of file +1. Finish howManyBits + +### 2023/10/9 Update: +1. Finish floatScale2 \ No newline at end of file