首页 > 其他分享 >计算机组成原理实验1

计算机组成原理实验1

时间:2022-10-04 21:55:51浏览次数:68  
标签:Rating return 计算机 ops int Max unsigned 实验 原理

莎比机组,单片机不是不学了吗

计算机系统基础实验一

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

相关文章

  • 实验一
    tastk1-1#include<iostream>#include<string>#include<vector>intmain(){usingnamespacestd;strings1;strings2{"cplusplus"};strings3{s2}; str......
  • 反弹Shell小实验
    反弹Shell实验环境攻击机:kali(IP:192.168.40.132)目标机:CentOS(IP:192.168.40.135)实验步骤首先在攻击机监听一个随机端口:nc-lvp6767第二步在目标机输入命令:bash......
  • 实验4:开源控制器实践——OpenDaylight
    一、基本要求1、Mininet拓扑生成并连接控制器的结果2、Mininet中ping测试截图二、进阶要求1、ODL提供的文档链接:http://127.0.0.1:8181/apidoc/explorer/index.html......
  • 实验4:开源控制器实践——OpenDaylight
    实验4:开源控制器实践——OpenDaylight第一部分:基本实验1.利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight控制器执行结果截图2.通过Postman工具调用OpenDayl......
  • 实验1
    #include<iostream>#include<string>#include<vector>intmain(){usingnamespacestd;strings1;strings2{"cplusplus"};strings3{s2}......
  • 实验4:开源控制器实践——OpenDaylight
    一.基本要求1.利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight控制器2.通过Postman工具调用OpenDaylight提供的API下发流表,实现拓扑内主机h1和h3网络中断10s......
  • 实验4:开源控制器实践——OpenDaylight
    一、实验目的1.能够独立完成OpenDaylight控制器的安装配置;2.能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境Ubuntu20.04Desktopamd64三、实......
  • 实验一
    task2:#include<iostream>usingstd::cout;usingstd::endl;//定义Point类classPoint{public:Point(intx0=0,inty0=0);Point(constPoint&p......
  • 实验一
        #include<iostream>#include<cstring>#include<vector>usingnamespacestd;intmain(){  strings1;  strings2{"cplusplus"};  strings3......
  • 实验1 类和对象
    实验任务1task1_1.cpp#include<iostream>#include<string>#include<vector>intmain(){usingnamespacestd;strings1;strings2{"cplusplu......