首页 > 其他分享 >csapp-datalab(菜鸟小白版)

csapp-datalab(菜鸟小白版)

时间:2024-05-04 15:57:16浏览次数:11  
标签:Rating csapp return ops int 菜鸟 31 bit 白版

第1题:

/* 
 * 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题:

二进制补码表示的最小值为0x80000000

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

第3题:

二进制补码表示的最大值为0x7FFFFFFF
tmax+1的相反数是本身,!!x_1是为了消除x=-1的影响

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

第4题:

0xAAAAAAAA正好对应所有奇数位是1的情况
因为题目限制常数只能使用0到255,所以不能直接使用0xAAAAAAAA
通过(0xAA<<8)+0xAA创造0x0000AAAA
通过(half_bits<<16)+half_bits创造0xAAAAAAAA
先用(x&all_bits)清除偶数位

/* 
 * allOddBits - return 1 if all odd-numbered bits in word set to 1
 *   where bits are numbered from 0 (least significant) to 31 (most significant)
 *   Examples allOddBits(0xFFFFFFFD) = 0, allOddBits(0xAAAAAAAA) = 1
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 12
 *   Rating: 2
 */
int allOddBits(int x) {
  int half_bits = (0xAA<<8)+0xAA;
  int all_bits = (half_bits<<16)+half_bits;
  return !((x&all_bits)^all_bits);
}

第5题:

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

第6题:

x-0x30,如果结果的符号位为1,说明x-0x30<0
0x39-x,如果结果的符号位为1,说明0x39-x<0
目标是得到,当x-0x30<0为0,0x39-x<0也为0时,最终结果才为1
001101011000

/* 
 * 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 left_bool = ((x+(~0x30)+1)>>31)&1;
  int right_bool = ((0x39+(~x)+1)>>31)&1;
  return !((left_bool^right_bool)|(left_bool&right_bool));
}

第7题:

~0为0xFFFFFFFF
当x!=0时,!x=0, (!(x)+(~0)=0xFFFFFFFF, (j&y)=y, (~j)&z=0
当x==0时,!x=1, (!(x)+(~0)=0, (j&y)=0, (~j)&z=z

/* 
 * 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 j = !(x)+(~0);
  return (j&y)|((~j)&z)
}

第8题:

一开始的想法:未考虑x,y异号,在x、y最大和最小时,相减会溢出
改正后的想法:考虑两种情况,
异号时直接看x的符号,x为正数则x>y,否则x<y
同号时相减,看结果的符号

/* 
 * 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 x_sign = (x>>31)&1;
  int y_sign = (y>>31)&1;
  int same_or_dif = (x_sign^y_sign);
  return (same_or_dif&x_sign)|(!same_or_dif&!((y+(~x)+1)>>31)&1);
}

第9题:

0和0x80000000的相反数是其本身
利用了0+1=1,0xFFFFFFFF+1=0的规律
如果x==0,则x和x的相反数的符号移位|的结果仍为0
如果x!=0,则x和y的相反数的符号移位|的结果为0xFFFFFFFF

/* 
 * 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>>31)|(((~x)+1)>>31))+1;
}

第10题:

原文链接:https://blog.csdn.net/m0_51335239/article/details/125849757
我想得到第一行的异或统一正负数和之后的分而治之思想,但是不会具体落实,水平还是不够

/* 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) {
  int temp = x ^ (x << 1);
    int bit_16,bit_8,bit_4,bit_2,bit_1;
  bit_16 = !!(temp >> 16) << 4;//0或16
  temp = temp >> bit_16;
  bit_8 = !!(temp >> 8) << 3;//0或8
  temp = temp >> bit_8;
  bit_4 = !!(temp >> 4) << 2;//0或4
  temp = temp >> bit_4;
  bit_2 = !!(temp >> 2) << 1;//0或2
  temp = temp >> bit_2;
  bit_1 = !!(temp >> 1);//0或1
    return 1 + bit_1 + bit_2 + bit_4 + bit_8 + bit_16;                     
}

第11题:

首先判断是不是+0.0和-0.0
new_f是uf的绝对值,我也不知道为什么题目说是unsigned int,测试数据会出现负数0x80000000
然后判断是不是NaN
最后分别处理非规格化和规格化,非规格化数左移一位,规格化数的阶码部分加1
s是uf的符号,|s为了还原uf的符号

//float
/* 
 * floatScale2 - 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 floatScale2(unsigned uf) {
    int new_f = uf & (~(1<<31));
    if (!new_f)
    {
        if(uf>>31)
        {
            return uf;    
        }
        return 0;  
    }
    else 
    {
        int exp = new_f>>23;
        int s = uf&(1<<31);
        if(!(exp^0xFF)&&(new_f<<8))
        {
            return uf;                
        }
        else if(!exp)
        {
            return (new_f<<1)|s;     
        }else
        {
            return (new_f+(1<<23))|s;
        }        
    } 
}

第12题:

https://www.cnblogs.com/zhiyiYo/p/16242033.html

/* 
 * floatFloat2Int - 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 floatFloat2Int(unsigned uf) {
    // 计算阶码
    unsigned exp = (uf & 0x7f800000) >> 23;
    int e = exp - 127;

    // 0或小数直接返回 0
    if (e < 0) {
        return 0;
    }

    // NaN 或者 无穷大
    if (e > 31) {
        return 0x80000000;
    }

    // 尾数
    int frac = (uf & 0x7fffff) | 0x800000;

    // 移动小数点
    if (e > 23) {
        frac <<= (e - 23);
    } else {
        frac >>= (23 - e);
    }

    // 符号位不变
    if (!((uf >> 31) ^ (frac >> 31))) {
        return frac;
    }

    // 符号位变化,且当前符号为负,说明溢出
    if (frac >> 31) {
        return 0x80000000;
    }

    // 符号变化,返回补码
    return ~frac + 1;
}

第13题:

参考原书第三版P82页图2-36内容

/* 
 * floatPower2 - Return bit-level equivalent of the expression 2.0^x
 *   (2.0 raised to the power x) for any 32-bit integer x.
 *
 *   The unsigned value that is returned should have the identical bit
 *   representation as the single-precision floating-point number 2.0^x.
 *   If the result is too small to be represented as a denorm, return
 *   0. If too large, return +INF.
 * 
 *   Legal ops: Any integer/unsigned operations incl. ||, &&. Also if, while 
 *   Max ops: 30 
 *   Rating: 4
 */
unsigned floatPower2(int x) {
    if(x<-149){
        return 0;
    }else if (x>127)
    {
        return 0x7f800000;
    }else
    {
        if(x<-126)
        {
            return 1<<(23+x+126);//非规格化数
        }
        return (x+127)<<23;//规格化数
    }
}

标签:Rating,csapp,return,ops,int,菜鸟,31,bit,白版
From: https://www.cnblogs.com/changyunyan/p/18172392

相关文章

  • CSAPP Lab 7 Malloc Lab
    本次实验的内容也比较清晰,只需要完成一个手写的malloc动态内存分配器即可。书上第\(9\)章第\(9\)节介绍了这样的分配器的实现方法。实验要求本次实验提供了基本的框架,需要完成下面几个函数:intmm_init(void);void*mm_malloc(size_tsize);voidmm_free(void*ptr);v......
  • CSAPP Lab 8 Proxy lab
    终于到最后一个Lab啦!这个Lab的任务是实现一个代理服务器,将客户端发送的请求转发到服务端。这个Lab分为三个任务,第一个任务需要实现这个代理服务,第二个任务支持处理并发请求,第三个任务需要实现缓存。PartI:Implementingasequentialwebproxy这个部分其实很好写,很多......
  • CSAPP Lab-1 DATALAB
    本文原发于2023-09-0215:32:57于我的hexo博客,现迁移至此。最近看完了CSAPP整本书,发现官网上还有11次实验可以做。UPD:好像只有9个,因为有两个是旧版本的,可以被新版的替代掉。UPD:好像只有8个,performance也算是旧的实验了,但是没有明确指出。Lab地址:http://csapp.cs......
  • CSAPP Lab-3 ATTACKLAB
    书接上回,这次做到了第三个Lab啦。任务描述这一个Lab的任务就更有意思了,实验给了我们两个程序,每个程序都会让我们输入一行字符串,而它们是通过下面这个函数来读取的:unsignedgetbuf(){ charbuf[BUFFER_SIZE]; Gets(buf); return1;}其中,Gets函数和C库的gets函数......
  • CSAPP Lab-4 Architecture Lab
    本次实验是有关书上第四章设计的Y86-64处理器的,实验分为三个部分,分别是编写几个简单的Y86-64程序、使用一条新指令扩展SEQ模拟器以及优化Y86-64的基准测试程序和处理器设计。实验准备需要简单复习一下Y86-64的指令集架构以及处理器架构呢。指令集架构指令集:指令功......
  • CSAPP Lab5 Cache Lab
    到实验5啦!这次的实验是有关高速缓存的。让我们先来复习一下高速缓存的基础知识吧!复习高速缓存的结构在一个存储器地址有\(m\)位的系统上,一共有\(M=2^m\)个地址。假设高速缓存被组织成一个有\(S=2^s\)个高速缓存组的数组,其中每个组包括\(E\)个高速缓存行,每行存......
  • CSAPP Lab6 Shell Lab
    本次实验的任务很清晰,实现一个简单的UnixShell。需要用到基础的进程控制、信号处理等知识。简单来说,实验已经提供了一些简单的功能,我们需要在此基础上,实现下面的功能:eval:解析和解释命令行的主例程。[70行]builtin_cmd:识别并解释内置命令quit(退出)、fg(前台运行某个作业)、bg(后......
  • CSAPP Lab-2 BOMBLAB
    第二个Lab就比较有趣了。这一个Lab的任务是,我们得到了一个bomb炸弹程序,这个炸弹程序有\(6\)个phase,每个phase都会读取我们的输入,判断我们的输入是否符合要求,如果正确这个phase的炸弹就会被拆除,否则炸弹就会爆炸。我们需要借助各种工具,对程序进行反汇编等等,获得能够......
  • 2024年幻兽帕鲁/Palworld专用服务器搭建指南:小白版,轻松上手!
    对于热爱《幻兽帕鲁》的玩家们来说,能够与好友一同联机冒险,无疑是增添游戏乐趣的一大方式。但如何轻松搭建一个稳定的联机服务器,却常常成为困扰大家的难题。今天,我将为大家带来一篇简单易懂的服务器搭建攻略,让你从此告别烦恼,与好友尽享游戏时光!首先,我们要访问的是阿里云的游戏......
  • 菜鸟记录:c语言实现PAT甲级1010--Radix
    很长时间没做,忙于考研和实习,久违的的拾起了算法。做了很长时间,其实总体思路还是很简单的,但满分不知道为什么就是到不了,又因为网上很多答案包括柳神的都是c++,无法参透,姑且只能这样了。Givenapairofpositiveintegers,forexample,6and110,canthisequation6=110bet......