Browse Source

finish quiz5-6

master
吴冕志 3 years ago
parent
commit
1d1f778fa1
1 changed files with 9 additions and 2 deletions
  1. +9
    -2
      lab2/datalab-handout/bits.c

+ 9
- 2
lab2/datalab-handout/bits.c View File

@ -206,7 +206,9 @@ int allOddBits(int x) {
* Rating: 2 * Rating: 2
*/ */
int negate(int x) { int negate(int x) {
return 2;
int notx = ~x;
int res = notx + 1;// since ~x + x = 1111...111 = -1, ~x + x + 1 = 0
return res;
} }
//3 //3
/* /*
@ -219,7 +221,12 @@ int negate(int x) {
* Rating: 3 * Rating: 3
*/ */
int isAsciiDigit(int x) { int isAsciiDigit(int x) {
return 2;
int neg0x3a = ~(0x3a) + 1;
int neg0x30 = ~(0x30) + 1;
int signofminus0x3a = (x + neg0x3a) >> 31; // should be all 1
int signofminus0x30 = (x + neg0x30) >> 31; // should be all 0
int res = !(signofminus0x30 | (~signofminus0x3a));
return res;
} }
/* /*
* conditional - same as x ? y : z * conditional - same as x ? y : z

Loading…
Cancel
Save