From ab35332d552f2cdfd46ec95a3c5b48d3d189e992 Mon Sep 17 00:00:00 2001 From: reisen <2863896505@qq.com> Date: Sun, 8 Oct 2023 12:34:53 +0800 Subject: [PATCH] Finish negate and isAsciiDigit --- LAB1/bits.c | 10 ++++++++-- README.md | 4 ++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/LAB1/bits.c b/LAB1/bits.c index 1ef38e4..2500357 100644 --- a/LAB1/bits.c +++ b/LAB1/bits.c @@ -225,7 +225,7 @@ int allOddBits(int x) { * Rating: 2 */ int negate(int x) { - return 2; + return ~x+1; } //3 /* @@ -238,7 +238,13 @@ int negate(int x) { * Rating: 3 */ int isAsciiDigit(int x) { - return 2; + //下边界为加够了就溢出,比这个大 + //上边界为加不够就不溢出 + int downstream = ~0x30+1; + int upstream = ~0x39; + int leftside = !((downstream+x)>>31); //超过0x30就符号变为0 + int rightside = !!((upstream+x)>>31); //小于0x39就符号仍然为1 + return leftside&rightside; } /* * conditional - same as x ? y : z diff --git a/README.md b/README.md index d7e86ba..176c0e2 100644 --- a/README.md +++ b/README.md @@ -13,3 +13,7 @@ A CSAPP LAB 1. Finish isTmax 2. Finish allOddBits + +3. Finish negate + +4. Finish isAsciiDigit