From 1a32e3302f2d01670c67648977170bd2396fba99 Mon Sep 17 00:00:00 2001 From: Wu Mianzhi <10215501411@stu.ecnu.edu.cn> Date: Sat, 17 Sep 2022 14:02:54 +0800 Subject: [PATCH] finish quiz8 --- lab2/datalab-handout/bits.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lab2/datalab-handout/bits.c b/lab2/datalab-handout/bits.c index 48d604c..5f11cce 100644 --- a/lab2/datalab-handout/bits.c +++ b/lab2/datalab-handout/bits.c @@ -259,7 +259,7 @@ int isLessOrEqual(int x, int y) { int absy = y & sign1; int diff = absy + (~absx) + 1; int signdiff = diff >> 31; - int res = !!isnegpos | (!isnegpos & !signdiff & !isposneg); + int res = !(!isnegpos) | (!isnegpos & !signdiff & !isposneg); return res; } //4 @@ -272,7 +272,14 @@ int isLessOrEqual(int x, int y) { * Rating: 4 */ int logicalNeg(int x) { - return 2; + int sign = (x ^ (~x + 1)) >> 31; + int one = 1; + int singlesign = sign & one; + int intmin = 1 << 31; + int isnotmin = (~x) >> 31; // if x is 00000, get 1111 and if x is 1000 get 00000 + int notmin = isnotmin & one; + int res = singlesign ^ one & notmin; + return res; } /* howManyBits - return the minimum number of bits required to represent x in * two's complement