Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
227 changes: 90 additions & 137 deletions lab2/datalab-handout/bits.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,163 +134,116 @@ You are expressly forbidden to:


#endif
//1
/*
* bitXor - x^y using only ~ and &
* Example: bitXor(4, 5) = 1
* Legal ops: ~ &
* Max ops: 14
* Rating: 1
*/
int bitXor(int x, int y) {
return 2;
int notx_y = ~x & y ;
int not_notx_y = ~notx_y;
int x_noty = x & ~y;
int not_x_noty = ~x_noty;
int x_bitxor_y = ~( not_notx_y & not_x_noty );
return x_bitxor_y;
}
/*
* tmin - return minimum two's complement integer
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 4
* Rating: 1
*/
int tmin(void) {

return 2;

return 1 << 31;
}
//2
/*
* isTmax - returns 1 if x is the maximum, two's complement number,
* and 0 otherwise
* Legal ops: ! ~ & ^ | +
* Max ops: 10
* Rating: 1
*/

int isTmax(int x) {
return 2;
int _x = 1;
int ans = _x << 31;
int _MAX = ans + (~_x + 1);
return !(_MAX - x);
}
/*
* allOddBits - return 1 if all odd-numbered bits in word set to 1
* where bits are numbered from 0 (least significant) to 31 (most significant)
* Examples allOddBits(0xFFFFFFFD) = 0, allOddBits(0xAAAAAAAA) = 1
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 12
* Rating: 2
*/

int allOddBits(int x) {
return 2;
int a = 0xaa;
int a_8 = a<<8;
int low_16 = a|a_8;
int high_16 = low_16<<16;
int num = low_16 | high_16;
int check = (x&num)^num;
return !check;
}
/*
* negate - return -x
* Example: negate(1) = -1.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 5
* Rating: 2
*/
int negate(int x) {
return 2;
int negate(int x){
return ~x+1;
}
//3
/*
* 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) {
return 2;
int x1 = 0x30;
int x2 = 0x39;
int a = x + ~x1 + 1;
int b = x2 + ~x + 1;
return !(a >> 31)&!(b >> 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 2;
}
/*
* isLessOrEqual - if x <= y then return 1, else return 0
* Example: isLessOrEqual(4,5) = 1.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 24
* Rating: 3
*/
int x1 =~(!!x)+1;
return (x1 & y) | (~x1 & z);
}
int isLessOrEqual(int x, int y) {
return 2;
int x1 , y1 , m , n;
x1 = x >> 31;
y1 = y >> 31;
m = x1 ^ y1;
n = y + ~x +1;
return( m & !!x1)|( !m & !(n>>31));
}
//4
/*
* logicalNeg - implement the ! operator, using all of
* the legal operators except !
* Examples: logicalNeg(3) = 0, logicalNeg(0) = 1
* Legal ops: ~ & ^ | + << >>
* Max ops: 12
* Rating: 4
*/
int logicalNeg(int x) {
return 2;
return ((x | (~x + 1)) >> 31) + 1;
}
/* howManyBits - return the minimum number of bits required to represent x in
* two's complement
* Examples: howManyBits(12) = 5
* howManyBits(298) = 10
* howManyBits(-5) = 4
* howManyBits(0) = 1
* howManyBits(-1) = 1
* howManyBits(0x80000000) = 32
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 90
* Rating: 4
*/
int howManyBits(int x) {
return 0;
int b16,b8,b4,b2,b1,b0;
int mask = x >> 31;
x = ( mask & ~x ) | ( ~mask & x);
b16 = !!(x>>16)<<4;
x >>= b16;
b8 = !!(x>>8)<<3;
x >>= b8;
b4 = !!(x>>4)<<2;
x >>= b4;
b2 = !!(x>>2)<<1;
x >>= b2;
b1 = !!(x>>1);
x >>= b1;
b0 = x;
return b16 + b8 + b4 + b2 + b1 + b0 + 1;
}
//float
/*
* floatScale2 - Return bit-level equivalent of expression 2*f for
* floating point argument f.
* Both the argument and result are passed as unsigned int's, but
* they are to be interpreted as the bit-level representation of
* single-precision floating point values.
* When argument is NaN, return argument
* Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
* Max ops: 30
* Rating: 4
*/
unsigned floatScale2(unsigned uf) {
return 2;
int exp = (uf & 0x7f800000) >> 23;
int sign = uf & (1 << 31);
if(exp == 255) return uf;
if(exp == 0) return (uf<<1) | sign;
exp++;
if(exp == 255) return 0x7f800000 | sign;
else return (uf & 0x807fffff) | (exp << 23);
}
/*
* floatFloat2Int - Return bit-level equivalent of expression (int) f
* for floating point argument f.
* Argument is passed as unsigned int, but
* it is to be interpreted as the bit-level representation of a
* single-precision floating point value.
* Anything out of range (including NaN and infinity) should return
* 0x80000000u.
* Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
* Max ops: 30
* Rating: 4
*/
int floatFloat2Int(unsigned uf) {
return 2;
unsigned flag = uf >> 31;
unsigned exp = ( uf >> 23) & 0xff;
unsigned last_num = uf & 0x7fffff;
int real_exp = exp - 127;
unsigned real_last_num = last_num | 0x800000;
if(exp == 0)
return 0;
if(exp == 255)
return 0x80000000;
if(real_exp >= 0)
{
if(real_exp>=31)
return 0x80000000;
real_last_num = real_last_num >> (23 - real_exp);
if(flag)
real_last_num= ~real_last_num+1;
return real_last_num;
}
return 0;
}
/*
* floatPower2 - Return bit-level equivalent of the expression 2.0^x
* (2.0 raised to the power x) for any 32-bit integer x.
*
* The unsigned value that is returned should have the identical bit
* representation as the single-precision floating-point number 2.0^x.
* If the result is too small to be represented as a denorm, return
* 0. If too large, return +INF.
*
* Legal ops: Any integer/unsigned operations incl. ||, &&. Also if, while
* Max ops: 30
* Rating: 4
*/
unsigned floatPower2(int x) {
return 2;
}
if(x>127)
return 0x7f800000;
if(x>=-126)
{
unsigned exp = x+127;
return exp<<23;
}
if(x>=-149)
return 0x1<<(x+149);
return 0;
}
8 changes: 8 additions & 0 deletions lab3/bomb-handout/12/answer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
We have to stand with our North Korean allies.
2 3 5 8 12 17
1 658
162 3
mfcdhg
6 1 3 2 5 4


6 changes: 6 additions & 0 deletions lab4/attack-handout/12/answer1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
8e 5e 55 55 55 55 00 00
11 changes: 11 additions & 0 deletions lab4/attack-handout/12/answer2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
48 f0 60 55 00 00 00 00
48 c7 c7 52 25 f8 70
48 bb c2 5e 55 55 55
55 00 00
53
c3
13 changes: 13 additions & 0 deletions lab4/attack-handout/12/answer3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
58 f0 60 55 00 00 00 00
37 30 66 38 32 35 35 32
00 00 00 00 00 00 00 00
48 c7 c7 48 f0 60 55
48 bb e7 5f 55 55 55
55 00 00
53
c3
9 changes: 9 additions & 0 deletions lab4/attack-handout/12/answer4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
9c 60 55 55 55 55 00 00
52 25 f8 70 00 00 00 00
c6 60 55 55 55 55 00 00
c2 5e 55 55 55 55 00 00