莎比机组,单片机不是不学了吗
计算机系统基础实验一
1.1 实验概述
实验目的:更好地熟悉和掌握计算机中整数和浮点数的二进制编码表示。
实验目标:加深对数据二进制编码表示的了解。
实验要求:使用有限类型和数量的运算操作实现一组给定功能的函数。
实验语言:c
实验环境:linux、gcc
实验资料:datalab-handout.tar.gz
1.2参考答案
int lsbZero(int x) {
return x>>1<<1;
}
/*
* byteNot - bit-inversion to byte n from word x
* Bytes numbered from 0 (LSB) to 3 (MSB)
* Examples: getByteNot(0x12345678,1) = 0x1234A978
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 6
* Rating: 2
*/
int byteNot(int x, int n) {
return (x ^ (0xff<<(n<<3)));
}
/*
* byteXor - compare the nth byte of x and y, if it is same, return 0, if not, return 1
* example: byteXor(0x12345678, 0x87654321, 1) = 1
* byteXor(0x12345678, 0x87344321, 2) = 0
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 20
* Rating: 2
*/
int byteXor(int x, int y, int n) {
return (!((~((x^y)>>(n<<3))) & 1));
}
/*
* logicalAnd - x && y
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 20
* Rating: 3
*/
int logicalAnd(int x, int y) {
return (!(!x) & !(!y));
}
/*
* logicalOr - x || y
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 20
* Rating: 3
*/
int logicalOr(int x, int y) {
return (!(!x) | !(!y));
}
/*
* rotateLeft - Rotate x to the left by n
* Can assume that 0 <= n <= 31
* Examples: rotateLeft(0x87654321,4) = 0x76543218
* Legal ops: ~ & ^ | + << >> !
* Max ops: 25
* Rating: 3
*/
int rotateLeft(int x, int n) {
int left=(unsigned int)x>>(32+(~n+1));
return x<<n|left;
}
/*
* parityCheck - returns 1 if x contains an odd number of 1's
* Examples: parityCheck(5) = 0, parityCheck(7) = 1
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 20
* Rating: 4
*/
int parityCheck(int x) {
int y=x<<16;
y=y^x;
y=y^(y<<8);
y=y^(y<<4);
y=y^(y<<2);
y=y^(y<<1);
y=y>>31;
return !(!y);
}
/*
* mul2OK - Determine if can compute 2*x without overflow
* Examples: mul2OK(0x30000000) = 1
* mul2OK(0x40000000) = 0
*
* Legal ops: ~ & ^ | + << >>
* Max ops: 20
* Rating: 2
*/
int mul2OK(int x) {
return ((x>>30)&1)^1;
}
/*
* mult3div2 - multiplies by 3/2 rounding toward 0,
* Should exactly duplicate effect of C expression (x*3/2),
* including overflow behavior.
* Examples: mult3div2(11) = 16
* mult3div2(-9) = -13
* mult3div2(1073741824) = -536870912(overflow)
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 12
* Rating: 2
*/
int mult3div2(int x) {
int y=(x<<1)+x;
return (y>>1)+(((y>>31)&1)&(((y<<31)>>31)&1));
}
/*
* subOK - Determine if can compute x-y without overflow
* Example: subOK(0x80000000,0x80000000) = 1,
* subOK(0x80000000,0x70000000) = 0,
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 20
* Rating: 3
*/
int subOK(int x, int y) {
int x1=x>>31&1;
return !((x1^(x>>30&1))^(x1^(~y>>31&1)));
}
/*
* absVal - absolute value of x
* Example: absVal(-1) = 1.
* You may assume -TMax <= x <= TMax
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 10
* Rating: 4
*/
int absVal(int x) {
int n=x>>31;
return (n^x)+(n&1);
}
/*
* float_abs - Return bit-level equivalent of absolute value of 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 representations of
* single-precision floating point values.
* When argument is NaN, return argument..
* Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
* Max ops: 10
* Rating: 2
*/
unsigned float_abs(unsigned uf) {
int n=uf>>31;
return (n^uf)+(n&1);
}
/*
* float_f2i - 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 float_f2i(unsigned uf) {
return (int)uf;
}
参考:https://www.cnblogs.com/linlianghe/p/14269455.html
标签:Rating,return,计算机,ops,int,Max,unsigned,实验,原理 From: https://www.cnblogs.com/nolca/p/16754575.html