信息的表示和处理

在通用计算机中中,字节作为最为最小 的可寻址的内存单元,而不是访问内存中单独的位。

寻址和字节顺序

  • big endian (大端法),数据最高字节部分地址在地址处,和人的感觉逻辑相似
  • little endian (小端法),低字节部分在低地址处

布尔代数

  • 1 TRUE
  • 2 FALSE
  • ~ NOT
  • & AND
  • | OR
  • ^ EXCLUSIVE-OR(异或)
    • 1 ^ 0 = 1
    • 1 ^ 1 = 0
    • 0 ^ 0 = 0
    • 0 ^ 1 = 1

IEEE 754 浮点数

$ V = (-1)^s \times M \times 2^E$

  • 符号(sign) s(1)为负数, s(0)为非负数
  • 尾数(significand) M 是一个二进制小数, 范围为 $1 \sim 2 - \varepsilon $ 或者 \(0 \sim 1 - \varepsilon\)
  • 阶码(exponent) E的作用是对浮点数加权, 权重的范围为2的 E 次方幂

将浮点数的位划分位三个字段,分别对这些值赋值:

  • 一个单独的符号位 s 直接编码符号位 s, 1-bit
  • k 位的阶码字段 \(exp = e_{k-1} \cdots e_1 e_0\) 编码阶码 E, k=7(单精度), k=11(双精度)
  • n 位小数字段 \(frac = f_{n-1} \cdots f_1 f_0\) 编码尾数 M, 且编码的值依赖阶码字段的值是否等于 0, n=23(单精度), n=52(双精度)

浮点数的值:

  • e 为无符号整数,其位表示 \(e_{k-1} \cdots e_1 e_0\)
  • 小数字段 frac 被解释为描述小数值 \(f\), 其中 \(0 \le f \le 1\), 其二进制表示\(0.f_{n-1} \cdots f_1 f_0\)
  • Bias 是一个等于 $2^{k-1} -1 $ 的偏置值
  • 规格化\((exp !=0, exp != 2^{k}-1)\), 最常遇到的值
    • 阶码的值 \(E = exp - Bias\)
    • 尾数定义 \(M = 1 + f\)
  • 非规格化\((exp == 0)\), 提供表示数值 0 及逐渐接近 0 的方法
    • 阶码的值 $E = 1 - Bias $
    • 尾数定义 \(M = f\)
  • 非规格化\((exp == 2^{k}-1)\), 特殊值 NaN

舍入

表示方法限制了浮点数的范围和精度

偶数舍入(round-to-even) 为默认的舍入方式, 其将数字向上或向下舍入,使得结果的最低有效数字(保留位)是偶数(0)

只有是在两个可能的结果的中间值才考虑向偶数舍入, 大于 0.5 是直接进位的

向上舍入的情况,向下舍入可以不管(反正要丢弃了,不影响结果)

尾数 \(1.BBGRXXX\), 保留位(Guard bit)、近似位(Round bit) 和 粘滞位(Sticky bit)

  • Round = 1, Sticky = 1 > 0.5 进位
  • Guard = 1, Round = 1, Sticky = 0 -> 偶数向上舍入

实验部分

1. 只用 ~& 操作符求两个数的或

摩根定律: $ \neg(p \lor q) = \neg p \land \neg q \(
异或:\) p \oplus q = (\neg p \land q) \lor (p \land \neg q)$

所以展开即可

/*
* 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 ~(~(~x & y) & ~(x & ~y));
}

2. 最小的整形补码, 可用符号 ! ~ & ^ | + << >>

$ -2^{31} $ (0xF0000000)

/*
* tmin - return minimum two's complement integer
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 4
* Rating: 1
*/
int tmin(void) {
return 1 << 31;
}

3. 判断是否是最大的整形数,可用符号 ! ~ & ^ | +*

直接利用 INT_MAX + INT_MAX + 2 = 0 的结果并且排除0xFFFFFFFF,还要注意一个不能直接相加,只能 x+1+x+1

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

4. 判断所有的奇数位为1,可用符号 ! ~ & ^ | + << >>

排除偶数位的干扰得到奇数位的值,再与奇数位的 0xaaaaaaaa 做亦或运算,如果正确结果必为 0,这时做 非运算 就可以了

所有先得到 0xaaaaaaa

/*
* allOddBits - return 1 if all odd-numbered bits in word set to 1
* Examples allOddBits(0xFFFFFFFD) = 0, allOddBits(0xAAAAAAAA) = 1
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 12
* Rating: 2
*/
int allOddBits(int x) {
int bits0_15 = (0xAA << 8) + 0xAA;
int bits0_23 = (bits0_15 << 8) + 0xAA;
int bits0_31 = (bits0_23 << 8) + 0xAA; return !((bits0_31 & x) ^ bits0_31);
}

5.取负,可用符号 ! ~ & ^ | + << >>

/*
* negate - return -x
* Example: negate(1) = -1.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 5
* Rating: 2
*/
int negate(int x) { return ~x + 1; }

6. 判断是否是 ASCII 数字,可用符号 ! ~ & ^ | + << >>

  1. 判断高 6_31 位,必须是 0
  2. 判断 4 5 位,必须为 1
  3. 判断第四位,通过相加6判断是否有进位
/*
* 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) {
int bit6_31 = !((x >> 6) & (~0));
int bit_5 = (x & 0x20) >> 5;
int bit_4 = (x & 0x10) >> 4;
int bits0_3 = !(((x & 0xF) + 6) & 0x10);
return bits0_3 & bit_4 & bit_5 & bit6_31;
}

7. 条件判断,三目运算符, 可用字符 ! ~ & ^ | + << >>

思路:由于 X & 0xFFFFFFFF = X, X & 0x0 = 0, 将两个数和 0xFFFFFFFF, 0x0 做与操作,再相加

  1. 只需要找到什么时候为 0xFFFFFFFF 和 0x0, 注意这两者可通过 ~ 得到
/*
* 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) {
int flag = (!!x + ~0);
return (z & flag) + (y & ~flag);
}

8. 小于等于 可用字符 ! ~ & ^ | + << >>

思路:判断相等,同符号相减判断是否有进位,不同符号直接判断第一个数的符号是否为 正

/*
* isLessOrEqual - if x <= y then return 1, else return 0
* Example: isLessOrEqual(4,5) = 1.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 24
* Rating: 3
*/
int isLessOrEqual(int x, int y) {
int equal = !(x ^ y); // x == y
int same_sign = !((x ^ y) >> 31); // sign
int x_reduce_y = ~y + 1 + x;
return equal | (same_sign & (x_reduce_y >> 31)) | ((!same_sign) & (x >> 31) & 1);
}

9. 非运算符 可用字符 ! ~ & ^ | + << >>

思路:核心就是抓住 符号位判断

  1. 考虑取反还是取负,取反所有数字的符号位都改变,取负只有 0 和 0x80000000 符号位不变,且这两个符号位相反,所以用取负的方式
  2. 将数与其负数直接做与操作,只有 0x80000000,符号为 1 不变,不能筛选出 0 的情况
  3. 考虑别的情况,将数取反做与操作,符号相反的数与操作后仍然为 0, 0x800000000 取反(符号为0)与其负数(符号为0)相与也还为 0,只有0取反后符号为1,与操作后仍为1
/*
* 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 ((~x & ~(~x + 1)) >> 31) & 0x1;
}

10. 计算一个最少的补码位可以表达的位数 可用字符 ! ~ & ^ | + << >>

思路:将相邻位做亦或操作,找到最高的位为 1 所在的位

~(bits16 << 3) + 1) + (((bits16 ^ 1) & 0x1) << 3, bits* 为上一个移位的结果,利用这个结果判断是增加位移的大小

/* 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) {
// all assignment must be followed by a declaration.
int bits16, bits8, bits4, bits2, bits1;
int shift16, shift8, shift4, shift2, shift1; int shift_off = 16; // first shift offset
x ^= x << 1; // find the highest 1-bit after XOR adjacent bits bits16 = !(x >> shift_off);
shift16 = bits16 << 4; // binary search.
// if result of prev offset != 0, shift_off should be increasing half prev
// offset , else should be decreasing half.
shift_off = shift_off + (~(bits16 << 3) + 1) + (((bits16 ^ 1) & 0x1) << 3);
bits8 = (!(x >> shift_off));
shift8 = bits8 << 3; shift_off = shift_off + (~(bits8 << 2) + 1) + (((bits8 ^ 1) & 0x1) << 2);
bits4 = (!(x >> shift_off));
shift4 = bits4 << 2; shift_off = shift_off + (~(bits4 << 1) + 1) + (((bits4 ^ 1) & 0x1) << 1);
bits2 = (!(x >> shift_off));
shift2 = bits2 << 1; shift_off = shift_off + (~(bits2) + 1) + ((bits2 ^ 1) & 0x1);
bits1 = (!(x >> shift_off));
shift1 = bits1; return 32 + (~(shift1 + shift2 + shift4 + shift8 + shift16) + 1);
}

11. 计算浮点数 f * 2, 返回浮点数的二进制位表示, 可用符号不受限制

思路:由于尾数的值取决于 frac 和 exp,所以要对其分开处理

  • 对于规格数,exp + 1, 但要考虑 +1 后不能为 255
  • 对于非规格数
    • exp = 255, 直接返回参数
    • exp = 0, frac = 0 返回 0, 因为这就是个 0
    • exp = 0, frac != 0, frac 左移一位(尾数取值的问题),又要判断左移后是否溢出(0-22bit)
/*
* float_twice - 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 float_twice(unsigned uf) {
int exp = 0x7f800000 & uf;
int frac = 0x007FFFFF & uf;
int sign = 0x80000000 & uf;
int bias = (exp >> 23) - 127; if (uf == 0x0)
return 0;
if (bias == 128) // NaN return NaN, inf can't *2
return uf; // frac depends on exp, so exp could not add 1 alone.
if (exp == 0) { // (exp + frac) << 1
frac = (frac << 1) & 0x007FFFFF;
if (uf & 0x00400000)
exp = 0x00800000;
} else {
exp = (exp + 0x00800000) & 0x7F800000;
if (exp == 0x7F800000)
frac = 0;
}
uf = sign | exp | frac;
return sign | exp | frac;
}

12. 整数转浮点数,返回浮点数的二进制位表示, 可用符号不受限制

思路:核心在于发现该数的绝对值的最高位 1 对应浮点数隐式精度的 1, 然后最高位1后的23位排列在 frac 位置

  • 取数的绝对值,后面对非负数数进行操作
  • 取最少可以表达整数(最高位 1)的 k 位 inum,
  • 所在的位数 n 整数i转浮点数f 在位模式上为 将 k-1 .. k-2 .. 0 放置在浮点数的 frac 部分,非规格数有一个隐式 1, 代替数字有效最高位 1
  • 由上精度有限制,有效位的前23位充当尾数部分,要对后9位进行判断是否需要舍入
  • 将 exp = 127 + n
  • 符号位不变
  • 其他 0 等情况考虑
/*
* float_i2f - Return bit-level equivalent of expression (float) x
* Result is returned as unsigned int, but
* it is to be interpreted as the bit-level representation of a
* single-precision floating point values.
* Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
* Max ops: 30
* Rating: 4
*/
unsigned float_i2f(int x) {
unsigned abs_x = x;
unsigned sign = x & 0x80000000;
int flag = 0;
int n = 30; if (x == 0)
return x;
else if (x == 0x80000000)
return 0xcf000000; if (sign)
abs_x = -x; while (!(abs_x & (1 << n)))
n--;
abs_x <<= 32 - n; if ((abs_x & 0x01ff) > 0x0100)
flag = 1;
else if ((abs_x & 0x03ff) == 0x0300)
flag = 1;
else
flag = 0; return sign + ((n << 23) + 0x3F800000) + (abs_x >> 9) + flag;
}

13. 浮点数转整数,返回整数的二进制位表示, 可用符号不受限制

思路:有上面的 float_i2f() 做铺垫,

  • 集中在对精度的处理, 对于 exp

    • 大于 31,超过整形的表达范围
    • 小于 23,值不发生改变,右移 23 - exp
    • 大于 23 小于等于 31,值发生改变 左移 exp -23
  • 由于浮点数的正负只由符号位影响,所以可以最后做取负操作。
/*
* 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) {
unsigned sign = uf & 0x80000000;
unsigned exp = uf & 0x7F800000;
unsigned frac = uf & 0x007FFFFF; if (uf == 0x7F800000)
return 0x80000000;
else if (uf == 0)
return 0; if (exp == 0)
return 0; int m = 0x00800000 + frac;
int e = (exp >> 23) - 127; if (e < 0)
return 0;
else if (e > 31)
return 0x80000000;
else if (e < 23)
m >>= (23 - e);
else
m <<= (e - 23); if (sign)
m = -m;
return sign | m;
}

信息的表示和处理 及 CS:APP 15213 datalab的更多相关文章

  1. 深入理解计算机系统 (CS:APP) Lab2 - Bomb Lab 解析

    原文地址:https://billc.io/2019/04/csapp-bomblab/ 写在前面 CS:APP是这学期的一门硬核课程,应该是目前接触到最底层的课程了.学校的教学也是尝试着尽量和CMU ...

  2. CS:APP配套实验 Data Lab

    刚刚完成注册博客,想写一篇随笔,方便以后自己回顾.如果恰好也能帮助到你,是我的荣幸. 这次随笔是记载我的计算机系统(CS:APP,Computer Systems:A Programer's Pers ...

  3. 图文并茂-超详解 CS:APP: Lab3-Attack(附带栈帧分析)

    CS:APP:Lab3-ATTACK 0. 环境要求 关于环境已经在lab1里配置过了.lab1的连接如下 实验的下载地址如下 说明文档如下 http://csapp.cs.cmu.edu/3e/at ...

  4. 深入理解计算机系统 (CS:APP) 缓冲区漏洞实验 – Buffer Lab 解析

    原文地址:https://billc.io/2019/05/csapp-cachelab/ 写在前面 这是 CSAPP 官网上的第 4 个实验 buflab,也是学校要求的第三个实验.这个实验比上一个 ...

  5. ubuntu12.04 安装CS:APP Y86模拟器

    下的第一UBUNTU12.04下Y86模拟器的安装:(參考http://archive.cnblogs.com/a/1865627/ 作适当改动) 1.安装bison和flex词法分析工具 sudo ...

  6. 《CS:APP》二进制炸弹实验(phase_1-3)

    <深入理解计算机系统>第三章的bomb lab,拆弹实验:给出一个linux的可执行文件bomb,执行后文件要求分别进行6次输入,每一次输入错误都会导致炸弹爆炸,程序终止.需要通过反汇编来 ...

  7. 深入理解计算机系统 (CS:APP) - 高速缓存实验 Cache Lab 解析

    原文地址:https://billc.io/2019/05/csapp-cachelab/ 这个实验是这学期的第四个实验.作为缓存这一章的配套实验,设计得非常精妙.难度上来讲,相比之前的修改现成文件, ...

  8. CS:APP Chapter 3 程序的机器级表示-读书笔记

    3.1 程序的机器级表示 发展历史 Intel,AMD,ARM 等企业各有又是,CPU 从 8 位发展到 16 位,再到 32 位,近几年发展到 64 位,当下的 CPU 体系被称为 x86-64 体 ...

  9. CS:APP Chapter-6 存储器层次系统-读书笔记

    存储器层次系统 笔记,应该不是一个大而全的文件,笔记应该是提纲挈领,是对思想的汇总浓缩,如果追求详实的内容反而是丢了初心. 计算机是抽象的,它的设计者努力让计算机变得简单,在设计上高度抽象,而计算机的 ...

随机推荐

  1. SepicalJudge

    原文:http://www.cnblogs.com/chouti/p/5752819.html Special Judge:当正确的输出结果不唯一的时候需要的自定义校验器 首先有个框架 #includ ...

  2. java异常——重新抛出异常

    有时候希望把刚捕获的异常重新抛出,尤其是在使用Exception捕获所有异常的时候.既然已经得到了对当前异常对象的引用,可以直接把它重新抛出: catch(Exception e){ System.o ...

  3. Tensorflow学习笔记——占位符和feed_dict(二)

    创建了各种形式的常量和变量后,但TensorFlow 同样还支持占位符.占位符并没有初始值,它只会分配必要的内存.在会话中,占位符可以使用 feed_dict 馈送数据. feed_dict是一个字典 ...

  4. 将数据从数据仓库Hive导入到MySQL

    1.启动Hadoop,hive,mysql 2.在mysql中建表(需要导入数据的) mysql> CREATE TABLE `dbtaobao`.`user_log` (`user_id` v ...

  5. MySQL5.6 windows msi安装介绍

    200 ? "200px" : this.width)!important;} --> 一.功能介绍 1.MySQL Servers 该功能是mysql主要的服务,也是必须安 ...

  6. bzoj 1867: [Noi1999]钉子和小球【dp】

    设f[i][j]为掉到f[i][j]时的概率然后分情况随便转移一下就好 主要是要手写分数比较麻烦 #include<iostream> #include<cstdio> usi ...

  7. 洛谷 P1414 又是毕业季II(未完成)

    题目背景 “叮铃铃铃”,随着高考最后一科结考铃声的敲响,三年青春时光顿时凝固于此刻.毕业的欣喜怎敌那离别的不舍,憧憬着未来仍毋忘逝去的歌.1000多个日夜的欢笑和泪水,全凝聚在毕业晚会上,相信,这一定 ...

  8. JMeter配置MongoDB

    1.启动JMeter,右键添加->配置文件->MongoDB Source Config. 注意:JMeter 3.0以上版本去掉了此配置项,可以从低版本处拷贝. 2.设置MongoDB配 ...

  9. linux C编程 gdb的使用

    linux C编程 gdb的使用 通常来说,gdb是linux在安装时自带的,在命令行键入"gdb"字符并按回车键会启动gdb调试环境. 1.gdb的基本命令 命令 说明 file ...

  10. Oracle11gR2设置连接数process与会话session值

    近日构建的Web应用用户数量有所上升,后台总是打印无法打开数据库连接的错误信息: 000000a3 SystemOut O 9月 ::, ERROR - msg:打开数据库出错. 经查询发现需要更改数 ...