提交 e6b4f4e5 创建 作者: 赵鹏翀's avatar 赵鹏翀

init template

上级
/Debug
/Release
<?xml version="1.0" encoding="gb2312"?>
<ASMProject Version="1.00" Name="Lab002" TemplatePath="console(c)\Project" ProjectID="7ea2146b-ee89-437e-aa2b-20e3a77bb32f" IsSubmitWork="0">
<Configurations>
<Configuration Name="Debug" DebuggerFlavor="0">
<Tool Name="PreBuildEventTool"/>
<Tool Name="CustomBuildTool"/>
<Tool Name="GCCCompilerTool" PreprocessorDefinitions="_DEBUG" GenerateDebugInformation="-1" LanguageStandard="c99"/>
<Tool Name="PreLinkEventTool"/>
<Tool Name="GCCLinkerTool"/>
<Tool Name="PostBuildEventTool"/>
</Configuration>
<Configuration Name="Release" DebuggerFlavor="0">
<Tool Name="PreBuildEventTool"/>
<Tool Name="CustomBuildTool"/>
<Tool Name="GCCCompilerTool" PreprocessorDefinitions="NDEBUG" LanguageStandard="c99"/>
<Tool Name="PreLinkEventTool"/>
<Tool Name="GCCLinkerTool" StripDebugInfo="-1"/>
<Tool Name="PostBuildEventTool"/>
</Configuration>
</Configurations>
<Files>
<Filter Name="ͷļ" Filter="h;hpp;hxx">
<File RelativePath=".\console.h">
</File>
</Filter>
<Filter Name="Դļ" Filter="cpp;c;cc;cxx">
<File RelativePath=".\console.c">
</File>
</Filter>
</Files>
</ASMProject>
添加文件
#include "console.h"
// 位操作
int lsbZero(int x) {
//x右移一位再左移一位实现把最低有效位置0
x = x>>1;
x = x<<1;
return x;
}
int getByteNot(int x, int n) {
//x第n个字节每位都和1异或实现取反
int y = 0xff;
n = n<<3;
y = y<<n;
x = (x^y);
return x;
}
int logicalAnd(int x, int y) {
//把x和y分别转化为逻辑的0和1,再相与
x = !x;
x = !x;
y = !y;
y = !y;
x = x & y;
return x;
}
// ****************************************************************************
int logicalOr(int x, int y) {
// 此处将代码补充完整
// 函数的功能是:把x和y分别转化为逻辑的0和1,再相或
return 0;
}
int getByteXor(int x, int y, int n) {
// 此处将代码补充完整
// 函数的功能是:将x和y的第n个字节比较,若不同,则返回1;若相同,则返回0
return 0;
}
int rotateLeft(int x, int n) {
// 此处将代码补充完整
// 函数的功能是:将x循环左移n位
return x;
}
int parityCheck(int x) {
// 此处将代码补充完整
// 函数的功能是:若x有奇数个1,则返回1;否则,返回0
return 0;
}
// ****************************************************************************
// 补码运算
int absVal(int x) {
//x最高位为0时就是x,最高位为1时是~x+1
int y = x>>31;
int k = (~y)&x;
x = ~x + 1;
x = y&x + k;
return x;
}
int mul2OK(int x) {
//把x第31位和30位分别和1做按位与,再异或,再和1异或
int m;
int p = (x>>31)&0x1;
int q = (x>>30)&0x1;
m = p^q;
return m^0x1;
}
// ****************************************************************************
int mult3div2(int x) {
// 此处将代码补充完整
// 函数的功能是:计算(x*3)/2,朝零方向取整
return x;
}
int subOK(int x, int y) {
// 此处将代码补充完整
// 函数的功能是:计算x–y,如果不溢出,则返回1,否则,返回0
return 0;
}
// ****************************************************************************
int main()
{
// 调用位操作函数
printf("调用位操作函数\n");
printf("***************************************************************\n");
printf("最低有效位(LSB)清零\n");
int x = 0;
x = lsbZero(0x87654321);
printf("lsbZero(0x87654321) = 0x%x\n\n", x);
printf("将x的第n个字节取反\n");
x = getByteNot(0x1234ff78,1);
printf("getByteNot(0x1234ff78,1) = 0x%x\n\n", x);
printf("把x和y分别转化为逻辑的0和1,再相与\n");
x = logicalAnd(0x12345678, 0);
printf("logicalAnd(0x12345678, 0) = %d\n\n", x);
printf("把x和y分别转化为逻辑的0和1,再相或\n");
x = logicalOr(0x12345678, 0);
printf("logicalOr(0x12345678, 0) = %d\n\n", x);
printf("比较x和y的第n个字节,若不同,则返回1;若相同,则返回0\n");
x = getByteXor(0x12345678, 0x87654321, 1);
printf("getByteXor(0x12345678, 0x87654321, 1) = %d\n\n", x);
printf("将x循环左移n位\n");
x = rotateLeft(0x87654321,4);
printf("rotateLeft(0x87654321,4) = 0x%x\n\n", x);
printf("若x有奇数个1,则返回1;否则,返回0\n");
x = parityCheck(7);
printf("parityCheck(7) = %d\n\n", x);
// 调用补码运算函数
printf("调用补码运算函数\n");
printf("***************************************************************\n");
printf("求绝对值\n");
x = absVal(-123);
printf("absVal(-123) = %d\n\n", x);
printf("计算2*x,如果不溢出,则返回1,否则,返回0\n");
x = mul2OK(0x80000000);
printf("mul2OK(0x80000000) = %d\n\n", x);
printf("计算(x*3)/2,朝零方向取整\n");
x = mult3div2(-9);
printf("mult3div2(-9) = %d\n\n", x);
printf("计算x–y,如果不溢出,则返回1,否则,返回0\n");
x = subOK(0x80000000,0x70000000);
printf("subOK(0x80000000,0x70000000) = %d\n\n", x);
return 0;
}
#ifndef _CONSOLE_H_
#define _CONSOLE_H_
#include <stdio.h>
/* TODO: 在此处引用程序需要的其他头文件 */
#endif /* _CONSOLE_H_ */
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论