The Report of the Data Lab

本文详细介绍了在CS:APP课程中的数据实验室实践,通过WSL实验环境,深入探讨了位操作、补码算术、IEEE浮点数等概念。包括位与、获取字节、逻辑移位、计数位、非零检测、最小负数、位适配、除以2的幂、取反、正数检测、ilog2函数、浮点数转换和浮点数加倍的实现方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The Report of the Data Lab

The Lab1 in CS:APP

毒液哥 Fudan University

CS:APP Data Lab helps develops the student’s understanding of bit
representations, two’s complement arithmetic, and IEEE floating point.


Preface

This Lab is done in the WSL(Windows Subsystem for Linux) experiment, cost about 10 hours and 158 operators are used totally.

Result

Solution

bitAnd
int bitAnd(int x, int y) 
{
	return ~(~x | ~y);
}

For every bit in x, from De Morgan Laws we have the piece of code above.

getByte
int getByte(int x, int n) 
{
	x = x >> (n << 3);
	x = x & 0xFF;
	return x;
}

Because 1byte=8bits1 byte = 8 bits1byte=8bits, shift x to the right by n * 8 then & 0xFF to get the byte.

logicalShift
int logicalShift(int x, int n) 
{
	int y = x >> n;
	int Op = x >> 31;
	Op = Op << 31;
	Op = Op >> n;
	Op = Op << 1;
	x = y ^ Op;
	return x;
}

The right shift of a signed interger in C language is arithmetic shift, which fills the vacant bit-positions with the sign bit. While the arithmetic shift always fills the bit-positions with 0.

In this situation, we can simply reverse the filed bits if the sign bit of x is 1.

In the piece of code above, Op = x >> 31 << 31 distracts the sign bit of x into Op, Op >> n << 1 denotes the filled bits of x after the right shift, then Op ^ (x >> n) can set all the filled bits to 0.

bitCount
int bitCount(int x) 
{
	int y = 0x11;
	int Num;

	y = (y << 8) | 0x11;
	y = (y << 8) | 0x11;
	y = (y << 8) | 0x11;

	Num = (x & y);
	x = x >> 1;
	Num = Num + (x & y);
	x = x >> 1;
	Num = Num + (x & y);
	x = x >> 1;
	Num = Num + (x & y);
	x = x >> 1;

	y = 0;

	y = Num + (Num >> 12) + (Num >> 24);
	return (y & 0xF) + ((y >> 4) & 0xF) + ((y >> 8) & 0xF);
}

A simple way to count the number of 1s in x is that shifting every bit in x to the right most and then add it to answer after taking bitwise-and with 1, but it costs near 100 operator count.

An efficient approach is checking multiple bits in one operation. We simply construct a constant 0x11111111 using bitwise operations. By adding x to Num after taking bitwise-and with 0x11111111and right shifting it by 1, for 4 times, every nibble of Num denotes the number of 1s in the corresponding nibble, respectively. Finally we get the total answer in every nibble together in the right most nibble by right shift.

bang
int bang(int x) 
{
	x = x | (x >> 1);
	x = x | (x >> 2);
	x = x | (x >> 4);
	x = x | (x >> 8);
	x = x | (x >> 16);
	return x & 1 ^ 1;
}

In the piece of code above, we use the divide and conquer algorithm to get the or-sum of every bit into the right most bit instead of simply doing it one by one. But it isn’t the best approach if it is am optimization of brute force.

int bang(int x) 
{
	return (~x + 1 | x) >> 31 & 1 ^ 1;
}

We find that the sign bit of x and the two’s complement of x is 0 if and only if x=0x = 0x=0. Namely, the sign bit of x of the two’s compelement of x is 1 if and only if x≠0x \ne 0x̸=0.

Hence, we have the piece of the code above.

tmin
int tmin(void) 
{
	return 1 << 31;
}

The two’s complement of 0x80000000 is 0xFFFFFFFF.

fitsBits
int fitsBits(int x, int n) 
{
	int Op = x >> 31;
	x = x ^ Op;
	x = x >> (n + ~0);
	return !x;
}

Due to the rules of two’s complement, the significant digits of a negetive integer x is the 0s in x, that is, the 1s in the reverse of x.

x = x ^ (x >> 31) reverses every bit in x if x is negetive, then we check if all the 1s are in the first n−1n - 1n1 bits.

divpwr2
int divpwr2(int x, int n) 
{
	int Op = x + ~0;
	Op = Op >> 31;
	Op = Op & 1;
	x = ~(~x + Op);
	x = x >> n;
	x = x + Op;
	return x;
}

If x is a positive number, the value of x >> n changes when x increases if 2n∣x+12 ^ n | x + 12nx+1. In this case, x >> n is an equvalence of x/2nx / 2 ^ nx/2n.

If x is a negetive number, the value of x >> n changes when x decreases if 2n∣x2 ^ n | x2nx. Thus, we find that ((x - 1) >> n) + 1 is an equvalence of x/2nx / 2 ^ nx/2n, in this case.

In the piece of code above, Op = x >> 31 & 1 denotes the sign bit of x, ~(~x + Op) is an equavalence of x−Opx - OpxOp.

But there is an exception that when x=0x80000000x = 0x80000000x=0x80000000, x - 1 changes the sign bit of x. Op = x + ~x will regard 0x80000000 as positive and we can still get the correct answer.

negate
int negate(int x) 
{
	return ~x + 1;
}

Omitted.

inPositive
int isPositive(int x) 
{
	int temp = !x;
	x = x >> 31;
	x = x & 1;
	x = x | temp;
	x = x ^ 1;
	return x;
}

x is not positive if x is negative or x=0x = 0x=0.x is negative if and only if the sign bit of x is 1.

x >> 1 & 1 checks the former situation while !x checks the latter situation.

ilog2
int ilog2(int x) 
{
	int Ans;
	int Current_Ans;

	Current_Ans = !!(x >> 16) << 4;
	Ans += Current_Ans;
	x = x >> Current_Ans;

	Current_Ans = !!(x >> 8) << 3;
	Ans += Current_Ans;
	x = x >> Current_Ans;

	Current_Ans = !!(x >> 4) << 2;
	Ans += Current_Ans;
	x = x >> Current_Ans;

	Current_Ans = !!(x >> 2) << 1;
	Ans += Current_Ans;
	x = x >> Current_Ans;

	Ans += x >> 1;
	return Ans;
}

Our objective is to find the position of 1 with the highest bit position.

Every time we use divide and conquer algorithm, divide x into two parts and check if the left part is 0. If not, we don’t care about the value of the right part, thus replace it with the left part and the answer increases by the length of the right part. Till the length of x is 2, simply execute Ans += x >> 1.

float_i2f
unsigned float_neg(unsigned uf) 
{
	if(((uf & 0x7F800000u) == 0x7F800000u) && (uf << 9u) > 0u)
		return uf;
	return uf ^ (1u << 31u);
}

Let uf be the bit-level equivalece of expression f.

f is NaN(not a number) if and only if exp=255∧frac≠0exp = 255 \land frac \ne 0exp=255frac̸=0. If f is a number, simply reverse the sign bit of f.

Particularly, +0 and -0 is considered different in the IEEE floating-point standard.

float_i2f
unsigned float_i2f(int x) 
{
	unsigned Ans = 0;
	unsigned Now = 31;
	unsigned temp;
	unsigned temp1;
	unsigned temp2;
	unsigned X;
	if(x == 0x80000000)
		return 0xCF000000;
	if(x == 0)
		return 0;
	if(x < 0)
	{
		Ans += 0x80000000;
		X = -x;
	}
	else
		X = x;
	while((X >> Now) == 0)
		--Now;
	temp2 = Now - 24;
	temp = X >> temp2;
	temp1 = 1 << temp2;
	if(Now >= 24 && (temp & 1))
		if(X & (temp1 - 1) || (temp & 2))
			X += temp1;
	if(X >> Now + 1)
		++Now;
	if(Now > 23)
		X = X >> Now - 23;
	else
		X = X << 23 - Now;
	Ans = Ans + (X & 0x7FFFFF);
	Ans = Ans + (Now + 127 << 23);
	return Ans;
}

In the IEEE floating-point standard, the only difference between the representation of f and -f is in the sign bit. Thus, for a negative number x except 0x80000000, we do process with an unsigned interger -x.

First, we find Now, the highest bit-position with an 1 in it. Because the single precision floating-point number only has 23 significant digits, we need rounding. In general, we simply find the closest matching value. The only design decision if to determine the rounding values that are halfway between two possible results.

The default Round-to-even mode rounds the number such that the least significant digit of the result is even.

Second, we shift the 23 significant digits to the least 23 bits.

Finally, let exp be Now plus 127.

float_twice
unsigned float_twice(unsigned uf) 
{
	unsigned Frac;
	if((uf & 0x7F800000u) == 0x7F800000u)
		return uf;
	if(uf & 0x7F800000u)
		return uf + (1 << 23);
	uf += uf & 0x7FFFFF;
	return uf;
}

For the special values, return themselves.

For the normalized values, exp increases by 1.

For the denormalized values, double frac. If an overflow happens, exp exactly increases by 1 and then denotes a correct normalized value.

Summary

总结再用英文就成英语小作文大赛了

上课时金城老师讲的很快,感觉三节课200页书好,清楚地讲述了IEEE标准中带符号整数和浮点数的存储方式。

开始做Lab之前自以为是轻车熟路,没想到我对C语言中位运算和数据存储方式的了解是如此浅薄。arithmetic shift, logical shift, two’s complement, 4 modes in rounding, 位运算符之间的优先级以及如何用位运算实现四则运算等等,都是我之前不曾知道的。

通过通读课本加上完成Lab中的各个函数,我对上述知识有了深刻的记忆。

最后,下次Lab一定要早点完成。

内容概要:本文针对国内加密货币市场预测研究较少的现状,采用BP神经网络构建了CCi30指数预测模型。研究选取2018年3月1日至2019年3月26日共391天的数据作为样本,通过“试凑法”确定最优隐结点数目,建立三层BP神经网络模型对CCi30指数收盘价进行预测。论文详细介绍了数据预处理、模型构建、训练及评估过程,包括数据归一化、特征工程、模型架构设计(如输入层、隐藏层、输出层)、模型编译与训练、模型评估(如RMSE、MAE计算)以及结果可视化。研究表明,该模型在短期内能较准确地预测指数变化趋势。此外,文章还讨论了隐层节点数的优化方法及其对预测性能的影响,并提出了若干改进建议,如引入更多技术指标、优化模型架构、尝试其他时序模型等。 适合人群:对加密货币市场预测感兴趣的研究人员、投资者及具备一定编程基础的数据分析师。 使用场景及目标:①为加密货币市场投资者提供一种新的预测工具和方法;②帮助研究人员理解BP神经网络在时间序列预测中的应用;③为后续研究提供改进方向,如数据增强、模型优化、特征工程等。 其他说明:尽管该模型在短期内表现出良好的预测性能,但仍存在一定局限性,如样本量较小、未考虑外部因素影响等。因此,在实际应用中需谨慎对待模型预测结果,并结合其他分析工具共同决策。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值