Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m trying to understand the following code. What this code is trying to do is

ID: 3888986 • Letter: I

Question

I'm trying to understand the following code.

What this code is trying to do is return 1 if 0x30 <= x <= 0x39 otherwise it returns 0.

How is the following code accomplishing the above? I want to understand how the code is checking whether x is between 0x30 and 0x39 using the code below.

int isAsciiDigit(int x) {

int a = x<<6;

int b = x<<5;

int c = x<<4;

int d = x<<3;

int e = x<<2;

int f = x<<1;

return (!(a))&((b)) & (c) & (~(d) | (~(e) & ~(f)));

}

Explanation / Answer

* isAsciiDigit - return 1 if 0x30 <= x <= 0x39 (ASCII codes for characters '0' to '9')
* Example: isAsciiDigit(0x35) = 1.
* isAsciiDigit(0x3a) = 0.
* isAsciiDigit(0x05) = 0.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 15
* Rating: 3
*/
int isAsciiDigit(int x) {
int lowerBound = 0x30;
int upperBound = 0x3a;
return !((x + (~lowerBound + 1)) >> 31) & (x + (~upperBound + 1)) >> 31;
}
/*
* conditional - same as x ? y : z
* Example: conditional(2,4,5) = 4
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 16
* Rating: 3
*/
int conditional(int x, int y, int z) {
return (y&(~((~(!x))+1))) + (z&((~(!x))+1));
}