/*
 * CS:APP Data Lab
 *
 * bits.c - ��ʵ������Ҫ�޸ı��ļ���
 *          ����Ҫ�Ѵ��ļ��ύ����ʦ/�̸���
 *
 */

#include "btest.h"
#include <limits.h>

/*
 * ѧ����ʵ��ָ����
 *
 * ����1����������֡�ѧ����д������Ľṹ�У�
 */
team_struct team = {
    /* Replace this with your full name */
    "程景愉",

    /* Replace this with your Andrew login ID */
    "202302723005"};

#if 0
/*
 * ����2������ϸ�Ķ������˵����
 */

// �������ʵ�飬��ͨ���༭���ļ���bits.c���еĸ�������������ύ�����

���Ҫ��

	����return������滻��һ�л��߶���ʵ�ֺ������ܵ�C���롣
	������/�޸ĵĴ�������������ķ��

  int Funct(arg1, arg2, ...) {
      /* ��Ҫ�����������ʵ�ָú������ܵ� */
      int var1 = Expr1;
      ...
      int varM = ExprM;

      varJ = ExprJ;
      ...
      varN = ExprN;
      return ExprR;
  }

	ÿһ����Expr����һ������ʹ���������ݵı���ʽ��
  1. 0~255��0xFF��������������������ʹ�ô�ĳ���������0xffffffff��
  2. �����Ĳ����;ֲ�������������ʹ��ȫ�ֱ�������
  3. ��Լ�����������" ! ~ "
  4. ����������������� & ^ | + << >> ��

	һЩ������ʵ�֣��������ܹ�ʹ�õ��������
	ÿһ����Expr������ʽ���ܰ�������������û��Ҫ�������ÿ��ֻ��һ���������

	��������ϸ��ֹ��
  1. ʹ���κο��������䣬����if, do, while, for, switch, �ȵȡ�
  2. �������ʹ���κκꡣ
  3. �ڴ��ļ������������κζ���ĺ�����
  4. �����κκ�����
  5. ʹ���κ������������������ &&, ||, -, ���� ?:
  6. ʹ���κ���ʽ��ǿ������ת����

  ����Ҫ������ĵ��ԣ�
  1. ʹ��2�Ĳ��룬32λ��ʾ��������
  2. ���������������������㡣
  3. �����λ�����������ֳ�����ô�����������Ԥ��Ľ����

// ���Խ��ܵı�̷���ʾ����
  /*
   * pow2plus1 - returns 2^x + 1, where 0 <= x <= 31
   */
  int pow2plus1(int x) {
     /* exploit ability of shifts to compute powers of 2 */
     return (1 << x) + 1;
  }

  /*
   * pow2plus4 - returns 2^x + 4, where 0 <= x <= 31
   */
  int pow2plus4(int x) {
     /* exploit ability of shifts to compute powers of 2 */
     int result = (1 << x);
     result += 4;
     return result;
  }


ע�⣺
  1. ����ʹ��dlc.exe������bits.c�Ƿ���ϱ��Ҫ��
  2. Each function has a maximum number of operators (! ~ & ^ | + << >>)
     that you are allowed to use for your implementation of the function.
     The max operator count is checked by dlc. Note that '=' is not
     counted; you may use as many of these as you want without penalty.
  3. ʹ��btest�����ĺ����Ƿ�����ȷ��
  4. The maximum number of ops for each function is given in the
     header comment for each function. If there are any inconsistencies
     between the maximum ops in the writeup and in this file, consider
     this file the authoritative source.
#endif

/*
 * ����3:
 * ��������ı��Ҫ���޸�����ĺ�����
 *
 *   ��Ҫ��Ϊ�˱���ܲ�ĳɼ���
 *   1. ʹ��dlc.exe�����ı�̷���Ƿ����Ҫ��
 *   2.
 * ʹ��btest�����ĺ����Ƿ�����ȷ����ע����Tmin��Tmax�����������Ƿ���ȷ��
 */

/*
 * bitAnd - x&y using only ~ and |
 *   Example: bitAnd(6, 5) = 4
 *   Legal ops: ~ |
 *   Max ops: 8
 *   Rating: 1
 */
int bitAnd(int x, int y) {
  // 实现x&y的位与运算
  return x & y;
}

/*
 * bitXor - x^y using only ~ and &
 *   Example: bitXor(4, 5) = 1
 *   Legal ops: ~ &
 *   Max ops: 14
 *   Rating: 2
 */
int bitXor(int x, int y) { return 2; }

/*
 * evenBits - return word with all even-numbered bits set to 1
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 8
 *   Rating: 2
 */
int evenBits(void) { return 2; }

/*
 * getByte - Extract byte n from word x
 *   Bytes numbered from 0 (LSB) to 3 (MSB)
 *   Examples: getByte(0x12345678,1) = 0x56
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 6
 *   Rating: 2
 */
int getByte(int x, int n) { return 2; }

/*
 * bitMask - Generate a bitmask consisting of all 1's
 *   from lowbit to highbit and 0's everywhere else.
 *   Examples: bitMask(5,3) = 0x38
 *   Assume 0 <= lowbit <= 31, and 0 <= highbit <= 31
 *   If lowbit > highbit, then mask should be all 0's
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 16
 *   Rating: 3
 */
int bitMask(int highbit, int lowbit) {
  // 生成从lowbit到highbit的位掩码
  if (lowbit > highbit)
    return 0;
  else {
    int res = 0;
    for (int i = lowbit; i <= highbit; i++) {
      res |= (1 << i);
    }
    return res;
  }
}

/*
 * reverseBytes - reverse the bytes of x
 *   Example: reverseBytes(0x01020304) = 0x04030201
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 25
 *   Rating: 3
 */
int reverseBytes(int x) {
  // 提取每个字节并移动到正确的位置
  int byte0 = (x >> 24) & 0xFF;    // 提取最高字节并移动到最低字节位置
  int byte1 = (x >> 8) & 0xFF00;   // 提取第二字节并移动到第二字节位置
  int byte2 = (x << 8) & 0xFF0000; // 提取第三字节并移动到第三字节位置
  int byte3 = (x << 24);           // 提取最低字节并移动到最高字节位置

  // 将各个字节合并
  return byte0 | byte1 | byte2 | byte3;
}

/*
 * leastBitPos - return a mask that marks the position of the
 *               least significant 1 bit. If x == 0, return 0
 *   Example: leastBitPos(96) = 0x20
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 6
 *   Rating: 4
 */
int leastBitPos(int x) {
  // 根据补码是取反再加一的性质
  return x & (~x + 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 2; }

/*
 * minusOne - return a value of -1
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 2
 *   Rating: 1
 */
int minusOne(void) {
  // 1的补码0直接取反
  return ~0;
}

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

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

/*
 * isPositive - return 1 if x > 0, return 0 otherwise
 *   Example: isPositive(-1) = 0.
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 8
 *   Rating: 3
 */
int isPositive(int x) { return 2; }

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

/*
 * sm2tc - Convert from sign-magnitude to two's complement
 *   where the MSB is the sign bit
 *   Example: sm2tc(0x80000005) = -5.
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 15
 *   Rating: 4
 */
int sm2tc(int x) { return 2; }

/*
 * getHighBit - return a mask that marks the position of the
 *               most significant 1 bit. If x == 0, return 0
 *   Legal ops: ! ~ & ^ | + << >>
 */
int getHighBit(int x) {
  // 使用位运算找到最高位
  return x & (~(x - 1));
}

/*
 * getLowBit - return a mask that marks the position of the
 *              least significant 1 bit. If x == 0, return 0
 *   Legal ops: ! ~ & ^ | + << >>
 */
int getLowBit(int x) {
  // 使用位运算找到最低位
  return x & (~x + 1);
}

/*
 * isNegative - return 1 if x < 0, return 0 otherwise
 *   Legal ops: ! ~ & ^ | + << >>
 */
int isNegative(int x) {
  // 判断符号位
  return (x >> 31) & 1;
}

/*
 * isGreater - if x > y then return 1, else return 0
 *   Legal ops: ! ~ & ^ | + << >>
 */
int isGreater(int x, int y) {
  // 判断x是否大于y
  return (x - y) > 0;
}

/*
 * tmin - return minimum two's complement integer
 *   Legal ops: ! ~ & ^ | + << >>
 */
int tmin(void) {
  // 返回最小的二补数
  return 1 << 31;
}

/*
 * plusOne - return x + 1
 *   Legal ops: ! ~ & ^ | + << >>
 */
int plusOne(int x) {
  // 实现加1操作
  return x + 1;
}
