只能说没有一道题是自己写的,都是在网上查资料抄别人的,也不知道这有什么意思。也不知道自己学到了什么,怎么说呢,Emmmm......对了,文末最后的几段话是为了凑字数,大家简单忽略掉就好。
/* * tmin - return minimum two's complement integer * Legal ops: ! ~ & ^ | + << >>* Max ops: 4* Rating: 1*/
int tmin(void) {return 1<<31;
}
/* * absVal - absolute value of x* Example: absVal(-1) = 1.* You may assume -TMax <= x <= TMax* Legal ops: ! ~ & ^ | + << >>* Max ops: 10* Rating: 4*/
int absVal(int x) {int s = x>>31;return (x+s)^s;
}
/* * bitAnd - x&y using only ~ and | * Example: bitAnd(6, 5) = 4* Legal ops: ~ |* Max ops: 8* Rating: 1*/
int bitAnd(int x, int y) {return ~(~x|~y);
}
/* * replaceByte(x,n,c) - Replace byte n in x with c* Bytes numbered from 0 (LSB) to 3 (MSB)* Examples: replaceByte(0x12345678,1,0xab) = 0x1234ab78* You can assume 0 <= n <= 3 and 0 <= c <= 255* Legal ops: ! ~ & ^ | + << >>* Max ops: 10* Rating: 3*/
int replaceByte(int x, int n, int c) {int s = n<<3;return (c<<s)|(x&~(0xff<<s));
}
/** mult3div2 - multiplies by 3/2 rounding toward 0,* Should exactly duplicate effect of C expression (x*3/2),* including overflow behavior.* Examples: mult3div2(11) = 16* mult3div2(-9) = -13* mult3div2(1073741824) = -536870912(overflow)* Legal ops: ! ~ & ^ | + << >>* Max ops: 12* Rating: 2*/
int mult3div2(int x) {x += (x<<1);return (x+((x>>31)&1))>>1;
}
/** multFiveEighths - multiplies by 5/8 rounding toward 0.* Should exactly duplicate effect of C expression (x*5/8),* including overflow behavior.* Examples: multFiveEighths(77) = 48* multFiveEighths(-22) = -13* multFiveEighths(1073741824) = 13421728 (overflow)* Legal ops: ! ~ & ^ | + << >>* Max ops: 12* Rating: 3*/
int multFiveEighths(int x) {x += (x<<2);return (x+((x>>31)&7))>>3;
}
/* * addOK - Determine if can compute x+y without overflow* Example: addOK(0x80000000,0x80000000) = 0,* addOK(0x80000000,0x70000000) = 1, * Legal ops: ! ~ & ^ | + << >>* Max ops: 20* Rating: 3*/
int addOK(int x, int y) {return (((x^y)>>31)|~(((x+y)^x)>>31))&1;
}
/** bitCount - returns count of number of 1's in word* Examples: bitCount(5) = 2, bitCount(7) = 3* Legal ops: ! ~ & ^ | + << >>* Max ops: 40* Rating: 4*/
int bitCount(int x) {int m2 = (0x55<<8)|0x55;int m4 = (0x33<<8)|0x33;int m8 = (0x0f<<8)|0x0f;m2 |= (m2<<16);m4 |= (m4<<16);m8 |= (m8<<16);x += ~((x>>1)&m2)+1;x = ((x>>2)&m4)+(x&m4);x = (x+(x>>4))&m8;x += (x>>8);x += (x>>16);return x&0x3f;
}
/* * 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) {int ny = ~y;return ((((x+ny+1)&(x^ny))|(x&ny))>>0x1f)&1;
}
/* * 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 ny = ~y;return ((((((x+ny+1)&(x^ny))|(x&ny))>>0x1f))&1)|!(x^y);
}
/** trueFiveEighths - multiplies by 5/8 rounding toward 0,* avoiding errors due to overflow* Examples: trueFiveEighths(11) = 6* trueFiveEighths(-9) = -5* trueFiveEighths(0x30000000) = 0x1E000000 (no overflow)* Legal ops: ! ~ & ^ | + << >>* Max ops: 25* Rating: 4*/
int trueFiveEighths(int x) {
int const eights = x >> 3;
int const rem = x & 7;return eights + (eights << 2) + (rem + (rem << 2) + (x >> 31 & 7) >> 3);
}/*int s = (x>>31)&1;int c = (s<<3)+~s+1;int h = ((x&(0xFF<<24))+c)>>3;int l = (x&~(0xFF<<24));return (h<<2)+h+((((l<<2)+l)+c)>>3);*/
/** parityCheck - returns 1 if x contains an odd number of 1's* Examples: parityCheck(5) = 0, parityCheck(7) = 1* Legal ops: ! ~ & ^ | + << >>* Max ops: 20* Rating: 4*/
int parityCheck(int x) {x ^= (x>>16);x ^= (x>>8);x ^= (x>>4);x ^= (x>>2);x ^= (x>>1);return x&1;
}
/* * rempwr2 - Compute x%(2^n), for 0 <= n <= 30* Negative arguments should yield negative remainders* Examples: rempwr2(15,2) = 3, rempwr2(-35,3) = -3* Legal ops: ! ~ & ^ | + << >>* Max ops: 20* Rating: 3*/
int rempwr2(int x, int n) {int s = x>>31;x = (x+s)^s;x &= ((~0)+(1<<n));return (x^s)+~s+1;
}
/* 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;temp = temp >> bit_16;bit_8 = !!(temp >> 8) << 3;temp = temp >> bit_8;bit_4 = !!(temp >> 4) << 2;temp = temp >> bit_4;bit_2 = !!(temp >> 2) << 1;temp = temp >> bit_2;bit_1 = !!(temp >> 1);return 1 + bit_1 + bit_2 + bit_4 + bit_8 + bit_16;
}
/** ilog2 - return floor(log base 2 of x), where x > 0* Example: ilog2(16) = 4* Legal ops: ! ~ & ^ | + << >>* Max ops: 90* Rating: 4*/
int ilog2(int x) {int count = 0;count = (!!(x>>16)) << 4;count =count+((!!(x>>(8 + count)))<<3);count =count+((!!(x>>(4 + count)))<<2);count =count+((!!(x>>(2 + count)))<<1);count =count+((!!(x>>(1 + count)))<<0);return count;
}
计算机是一种能够按照程序自动进行数据处理和运算的电子设备。计算机的发展可以分为四个阶段:机械计算机、电子管计算机、晶体管计算机和集成电路计算机。
机械计算机是最早的计算机,它们使用齿轮和滑动杆等机械装置进行计算。电子管计算机是第一代计算机,它们使用电子管代替了机械装置,大大提高了计算速度。晶体管计算机是第二代计算机,它们使用晶体管代替了电子管,使得计算机更加小型化和可靠化。集成电路计算机是第三代计算机,它们使用集成电路代替了晶体管,使得计算机更加高效和便携。
计算机的原理是基于二进制数制和逻辑电路。计算机中的所有数据都是以二进制形式存储和处理的,逻辑电路则负责对这些数据进行运算和控制。计算机的核心是中央处理器(CPU),它包含算术逻辑单元(ALU)、控制单元(CU)和寄存器等组件,负责执行指令和处理数据。
除了硬件,计算机还需要软件来实现各种功能。软件可以分为系统软件和应用软件两类。系统软件包括操作系统、编译器和驱动程序等,它们负责管理计算机的资源和提供基本的功能。应用软件则是为了满足用户的需求而开发的各种程序,例如办公软件、游戏和浏览器等。
总之,计算机是一种非常重要的电子设备,它的发展历程经历了多个阶段,从机械计算机到集成电路计算机,每一代计算机都有着自己的特点和优势。计算机的原理基于二进制数制和逻辑电路,它需要硬件和软件的配合才能实现各种功能。